Cod sursa(job #369468)

Utilizator ilincaSorescu Ilinca ilinca Data 28 noiembrie 2009 14:19:34
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include <stdio.h>
#include <vector>
#include <queue>

using namespace std;

#define nmax 30005

typedef pair <int, int> ii;

int n, m, x, y, d [nmax];
vector <ii> g [nmax];
queue <int> q;

void scan ()
{
	int i, a, b, c;
	scanf ("%d%d%d%d", &n, &m, &x, &y);
	for (i=1; i <= m; ++i)
	{
		scanf ("%d%d%d", &a, &b, &c);
		g [a].push_back (ii (b, c));
		g [b].push_back (ii (a, c));
	}
}

void bfs ()
{
	vector <ii> :: iterator it;
	int nod, v;
	q.push (x);
	d [x]=1;
	while (!q.empty ())
	{
		nod=q.front ();
		v=d [nod];
		q.pop ();
		for (it=g [nod].begin (); it != g [nod].end (); ++it)
			if (d [it->first] == 0)
			{
				if (it->first < nod)
					d [it->first] = d [nod] - it->second;
				else
					d [it->first] = d [nod] + it->second;
				if (it->first == y)
					return ;
				q.push (it->first);
			}				
	}
}

int main ()
{
	freopen ("sate.in", "r", stdin);
	freopen ("sate.out", "w", stdout);
	scan ();
	bfs ();
	printf ("%d\n", d [y]-1);
	return 0;
}