Cod sursa(job #786209)

Utilizator danalex97Dan H Alexandru danalex97 Data 10 septembrie 2012 18:10:41
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#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';
}