Cod sursa(job #584110)

Utilizator alexdmotocMotoc Alexandru alexdmotoc Data 23 aprilie 2011 23:10:09
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.14 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <deque>
using namespace std;

#define maxn 30005

long long n , m , x , y , d , a , b , dist[maxn];
vector < pair <int , int> > graf[maxn];
deque <int> coada;

int main()
{
	ifstream f ("sate.in");
	ofstream g ("sate.out");
	
	int ok = 0;
	
	f >> n >> m >> x >> y;
	
	if (x == y)
	{
		g << 0;
		return 0;
	}
	
	for (int i = 1 ; i <= m ; ++i)
	{
		f >> a >> b >> d;
		graf[a].push_back (make_pair (b , d));
		graf[b].push_back (make_pair (a , d));
	}
	
	coada.push_back (x);
	dist[x] = 0;
	
	while (coada.size() && !ok)
	{
		int nod = coada[0];
		
		for (unsigned int i = 0 ; i < graf[nod].size() ; ++i)
		{
			if (dist[graf[nod][i].first] || graf[nod][i].first == x)
				continue;
			
			if (nod < graf[nod][i].first)
				dist[graf[nod][i].first] = dist[nod] + graf[nod][i].second;
			else
				dist[graf[nod][i].first] = dist[nod] - graf[nod][i].second;
			
			if (nod == y || graf[nod][i].first == y)
			{
				g << dist[y];
				return 0;
			}
			
			coada.push_back(graf[nod][i].first);
			
		}
		coada.pop_front();
	}
	
	return 0;
}