Cod sursa(job #497918)

Utilizator AndreiRSStatescu Andrei Rares AndreiRS Data 3 noiembrie 2010 16:22:51
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <stdio.h>
#include <list>
using namespace std;
#define DIM 30001
#define INF 1073741824

list <int> L[DIM][2], C;
list <int> :: iterator It, Jt;

int N, M, X, Y;
int D[DIM];

void cit ()
{
	int aux, x, y, d;
	
	scanf ("%d%d%d%d", &N, &M, &X, &Y);
	if (X > Y) aux=X, X=Y, Y=aux;
	
	for (int i = 0; i < M; ++i)
	{
		scanf ("%d%d%d", &x, &y, &d);
		L[x][0].push_back (y);
		L[x][1].push_back (d);
		L[y][0].push_back (x);
		L[y][1].push_back (d);
	}
	
	for (int i = 1; i <= N; D[i++] = INF);
}

void rez ()
{
	int Nod, Nodv, dist;
	C.push_back (X);
	D[X] = 0;
	
	while ( !C.empty () )
	{
		Nod = C.front ();
		Jt = L[Nod][1].begin ();
		for (It = L[Nod][0].begin (); It != L[Nod][0].end (); ++It, ++Jt)
		{
			Nodv = *It;
			if (Nod < Nodv) dist = *Jt;
			else dist = -*Jt;
			if ( D[Nodv] == INF )
			{	
				D[Nodv] = D[Nod] + dist; 
				C.push_back (Nodv);
			}
		}
		C.pop_front ();
	}	
}

void afs ()
{
	printf ("%d", D[Y]);
}

int main ()
{
	freopen ("sate.in", "r", stdin);
	freopen ("sate.out", "w", stdout);
	
	cit ();
	rez ();
	afs ();
	
	return 0;
}