Cod sursa(job #1454429)

Utilizator andrei_diaconuAndrei Diaconu andrei_diaconu Data 26 iunie 2015 16:22:47
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <fstream>
#include <vector>
#include <queue>

#define NMax 30010
#define INF 2000000000

using  namespace std;

ifstream f("sate.in");
ofstream g("sate.out");

int nodes, edges, d[NMax], bg, ed;
vector< pair<int, int> > G[NMax];
queue<int> Q;
bool mark[NMax];

void BFS()
{
	for (int i = 1; i <= nodes; i++)
		if (i != bg)
			d[i] = INF;

	Q.push(bg);

	while (!Q.empty()) {
		int crtNode = Q.front();
		Q.pop();

		for (vector< pair<int, int> >::iterator it = G[crtNode].begin(); it != G[crtNode].end(); it++) {
			if (!mark[it->first]) {
				mark[it->first] = true;
				Q.push(it->first);
			}

			if (d[it->first] > d[crtNode] + it->second)
				d[it->first] = d[crtNode] + it->second;
		}
	}
}

int main()
{

	f >> nodes >> edges >> bg >> ed;

	int n1, n2, c;
	for (int i = 1; i <= edges; i++) {
		f >> n1 >> n2 >> c;

		G[n1].push_back(make_pair(n2, c));
		G[n2].push_back(make_pair(n1, -c));
	}

	BFS();

	g << d[ed];

	return 0;
}