Cod sursa(job #1053931)

Utilizator GaborGabrielFMI - GabrielG GaborGabriel Data 13 decembrie 2013 01:04:10
Problema Sate Scor 35
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 0.93 kb
#include <fstream>
#include <queue>
#include <vector>
using namespace std;

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

#define MAXN 30005
int N, M, X, Y, viz[MAXN];
vector<pair<int, int> > graph[MAXN];
queue<pair<int, int>> q;

int main()
{
	int i, a, b, c;
	
	f >> N >> M >> X >> Y;
	for (i = 1; i <= M; ++i)
		f >> a >> b >> c, graph[a].push_back(make_pair(b,c)), graph[b].push_back(make_pair(a,c));

	viz[X] = 1;
	q.push(make_pair(X, 0));
	while (!q.empty())
	{
		a = q.front().first;
		b = q.front().second;
		q.pop();
		for (i = 0; i < graph[a].size(); ++i)
		if (!viz[graph[a][i].first])
		{
			viz[graph[a][i].first] = 1;
			if (graph[a][i].first == Y) {
				g << b + graph[a][i].second << '\n';
				return 0;
			}
			if (graph[a][i].first > a) q.push(make_pair(graph[a][i].first, b + graph[a][i].second));
			else q.push(make_pair(graph[a][i].first, b - graph[a][i].second));
		}
	}

	return 0;
}