Cod sursa(job #891551)

Utilizator fhandreiAndrei Hareza fhandrei Data 25 februarie 2013 17:46:49
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
// Include
#include <fstream>
#include <utility>
#include <vector>
#include <queue>
using namespace std;

// Definitii
#define mp make_pair
#define pb push_back

#define edge pair<int, int>
#define cost first
#define node second

// Constante
const int sz = (int)3e4+1;

// Variabile
ifstream in("sate.in");
ofstream out("sate.out");

int nodes, edges;
int startNode, endNode;
int dist[sz];

vector<edge> graph[sz];

// Main
int main()
{
	in >> nodes >> edges;
	in >> startNode >> endNode;
	
	int rFrom, rTo, rCost;
	while(edges--)
	{
		in >> rFrom >> rTo >> rCost;
		
		graph[rFrom].pb(mp(rCost, rTo));
		graph[rTo].pb(mp(-rCost, rFrom));
	}
	
	queue<int> q;
	q.push(startNode);
	
	while(!q.empty())
	{
		int current = q.front();
		q.pop();
		
		vector<edge>::iterator it, end=graph[current].end();
		for(it=graph[current].begin() ; it!=end ; ++it)
		{
			if(!dist[it->node])
			{
				dist[it->node] = dist[current] + it->cost;
				q.push(it->node);
			}
		}
		if(dist[endNode])
			break;
	}
	
	out << dist[endNode];
	
	in.close();
	out.close();
	return 0;
}