Pagini recente » Cod sursa (job #2711539) | Cod sursa (job #1404694) | Cod sursa (job #3289777) | Cod sursa (job #2861959) | Cod sursa (job #1391173)
#include <iostream>
#include <vector>
#include <queue>
#include <fstream>
using namespace std;
int viz[30000],tata[30000],s=0;
/*void cauta(int a1,int a2)
{
ifstream h("sate.in");
int n1,m1,x1,y1,c1,i;
h>>n1>>m1>>x1>>y1;
for(i=1;i<=m1;i++)
{
h>>x1>>y1>>c1;
if(((x1==a1)&&(a2==y1))||((y1==a1)&&(a2==x1)))
{
if(a1<a2)
s-=c1;
else
s+=c1;
break;
}
}
h.close();
}*/
int main()
{
int x,m,n,x1,y1,e,b,c,y,i,k=0,j;
vector <int> v[10000];
vector <int> a[10000];
queue <int> q;
ifstream f("sate.in");
ofstream g("sate.out");
f>>n>>m>>x>>y;
for(i=1;i<=m;i++)
{
f>>e>>b>>c;
v[e].push_back(b);
v[b].push_back(e);
a[e].push_back(c);
a[b].push_back(c);
}
q.push(x);
viz[x]=1;
while(q.size()>0)
{
x=q.front();
q.pop();
for(i=0;i<v[x].size();i++)
{
if(viz[v[x][i]]==0)
{
q.push(v[x][i]);
viz[v[x][i]]=1;
tata[v[x][i]]=x;
}
if(v[x][i]==y)
{
k=1;
break;
}
}
if(k)
break;
}
f.close();
s=0;
while(tata[y])
{
for(i=0;i<v[tata[y]].size();i++)
if(y==v[tata[y]][i])
{
if(tata[y]>y)
s-=a[tata[y]][i];
else
s+=a[tata[y]][i];
break;
}
y=tata[y];
}
g<<s;
g.close();
return 0;
}