Cod sursa(job #559638)

Utilizator alexdmotocMotoc Alexandru alexdmotoc Data 17 martie 2011 22:38:34
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 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];
bool viz[maxn];
vector <int> graf[maxn];
vector <int> cost[maxn];
deque <int> coada;

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