Cod sursa(job #826189)

Utilizator antonioteoZait Teodor Antonio antonioteo Data 30 noiembrie 2012 13:39:08
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <fstream>
#include <vector>
#include <queue>
#define INF 24000000
using namespace std;
const char iname[] = "sate.in";
const char oname[] = "sate.out";
ifstream fin(iname);
ofstream fout(oname);
int i , j , x , y , D;
int N , M , X , Y;
int cost[ 30008 ];
struct sat
{
	int val;
	int c;
};
vector <sat> v[ 30008 ];
queue <int> Q;
sat nod;
int main()
{
	fin >> N >> M >> X >> Y;
	for ( i = 1; i <= N; ++i ) cost[ i ] = INF;
	for ( i = 1; i <= M; ++i )
	{
		fin >> x >> y >> nod.c;
		nod.val = y; v[ x ].push_back( nod );
		nod.c *= -1;
		nod.val = x; v[ y ].push_back( nod );
	}
	Q.push( X );
	cost[ X ] = 0;
	while ( !Q.empty() )
	{
		x = Q.front();
		Q.pop();
		for ( i = 0; i < v[ x ].size(); ++i )
		{
			if ( cost[ x ] + v[ x ][ i ].c < cost[ v[ x ][ i ].val ] )
			{
				cost[ v[ x ][ i ].val ] = cost[ x ] + v[ x ][ i ].c;
				Q.push( v[ x ][ i ].val );
			}
		}
	}
	fout << cost[ Y ] << '\n';
	return 0;
}