Cod sursa(job #786209)
#include <fstream>
#include <vector>
using namespace std;
const int Nmax = 30010;
const int Mmax = 100030;
int N,M,X,Y;
vector< pair<int,long> > V[Nmax];
int Nod[Nmax],Size;
long Cost[Nmax];
ifstream F("sate.in");
ofstream G("sate.out");
int main()
{
F>>N>>M>>X>>Y;
for (int i=1;i<=M;++i)
{
int x,y,cost;
F>>x>>y>>cost;
if ( x>y ) cost=-cost;
V[x].push_back( make_pair(y,cost) );
V[y].push_back( make_pair(x,-cost) );
}
Nod[++Size]=X;
while ( !Cost[Y] )
{
int Act=Nod[Size];
int Sz=V[Act].size();
--Size;
for (int i=0;i<Sz;++i)
if ( ! Cost[ V[Act][i].first ] )
Cost[ V[Act][i].first ]=Cost[Act]-V[Act][i].second,
Nod[++Size]=V[Act][i].first;
}
G<<max(Cost[Y],-Cost[Y])<<'\n';
}