Cod sursa(job #645326)

Utilizator toniobFMI - Barbalau Antonio toniob Data 9 decembrie 2011 11:04:59
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <fstream>
#include <queue>
#include <vector>
using namespace std;

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

int m,n,s,f,dist[30002];
vector <int> a[30002],d[30002];
queue <int> q;

void citire () {
	in >> n >> m >> s >> f;
	int x,y,euh;
	while (m--){
		in >> x >> y >> euh;
		a[x].push_back(y);
		a[y].push_back(x);
		d[y].push_back(euh);
		d[x].push_back(euh);
	}
	for (int i = 1; i <= n; ++i) {
		dist[i] = -1;
	}
}

void bfs () {
	q.push(s);
	dist[s] = 0;
	int x,y;
	while(!q.empty()) {
		x = q.front();
		q.pop();
		for (size_t i = 0; i < a[x].size(); ++i) {
			y = a[x][i];
			if (dist[y] != -1){
				continue;
			}
			q.push(y);
			if (x > y) {
				dist[y] = dist[x] - d[x][i];
			} else {
				dist[y] = dist[x] + d[x][i];
			}
		}
	}
}

int main () {
	citire();
	
	bfs();
	
	out << dist[f];
	
	return 0;
}