Cod sursa(job #994878)
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
vector <int> v[30001];
int c[30001], cost[30001], deg[30001];
int n,m,x,y;
void bfs()
{
int i,j=1,k;
c[0]=x;
for(i=0; i<j; i++)
for(k=0; k<deg[c[i]]; k+=2)
{
if(cost[v[c[i]][k]]==0)
{
c[j]=v[c[i]][k];
cost[c[j]]=cost[c[i]]+v[c[i]][k+1];
j++;
}
if(cost[y]!=0)
break;
}
}
int main()
{
int i,a,b,dist;
ifstream in("sate.in");
ofstream out("sate.out");
in>>n>>m>>x>>y;
for(i=0; i<m; i++)
{
in>>a>>b>>dist;
v[a].push_back(b);
v[a].push_back(dist);
v[b].push_back(a);
v[b].push_back(dist*(-1));
}
in.close();
for(i=1; i<=n; i++)
{
deg[i]=v[i].size();
}
bfs();
out<<cost[y];
out.close();
return 0;
}