Cod sursa(job #1396838)

Utilizator theprdvtheprdv theprdv Data 23 martie 2015 06:13:10
Problema Sate Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#include <stack>

using namespace std;

fstream fin("sate.in", ios::in);
fstream fout("sate.out", ios::out);

#define MAXDIM 30005
pair <int, int> exe;
vector <pair<int, int>> list[MAXDIM];
bool used[MAXDIM];

void read()
{
	int x, y, m, c, n;
	fin >> n >> m >> x >> y;
	exe.first = x; exe.second = y;
	for (int i = 1; i <= m; i++){
		fin >> x >> y >> c,
		list[x].push_back(make_pair(y, c)),
		list[y].push_back(make_pair(x, c));
	}
	fin.close();
}
int BFS()
{
	queue <pair<int, int>> q;
	q.push(make_pair(exe.first, 0));
	int node = exe.first, dist;

	while (!q.empty()){
		node = q.front().first;
		dist = q.front().second;
		q.pop();
		used[node] = true;
		if (node == exe.second) return dist;

		for (int i = 0, size = list[node].size(); i < size; i++){
			if (!used[list[node][i].first])
				list[node][i].first < node ? dist -= list[node][i].second : dist += list[node][i].second;
				q.push(make_pair(list[node][i].first, dist));
		}
	}
	return 0;
}

int main()
{
	read();
	fout << BFS();

	fout.close();
	return 0;
}