Cod sursa(job #1253343)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 1 noiembrie 2014 09:43:58
Problema Sate Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 0.66 kb
#include <fstream>
#include <queue>
#include <vector>

using namespace std;
const int MAXN = 30005;

int N, M, X, Y;
vector <pair<int, int> > g[MAXN];

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

int main() {
	fin >> N >> M >> X >> Y;
	while(M --) {
		int x, y, z;
		fin >> x >> y >> z;
		g[x].push_back(make_pair(y, z));
		g[y].push_back(make_pair(x, -z));
	}
	vector <int> dp(N + 1, 1 << 30);
	dp[X] = 0;
	queue <int> q;
	q.push(X);
	while(!q.empty()) {
		int node = q.front();
		for(auto it : g[node]) {
			if(dp[it.first] > dp[node] + it.second) {
				dp[it.first] = dp[node] + it.second;
				q.push(it.first);
			}
		}
		q.pop();
	}
	fout << dp[Y] << '\n';
}