Pagini recente » Cod sursa (job #13049) | Solutia problemei shoturi | Cod sursa (job #3177553) | Cod sursa (job #2005935) | Cod sursa (job #1202368)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f ("sate.in");
ofstream g ("sate.out");
const int NMAX = 30000 + 1;
int n, x, y;
vector < pair <int, int> > v[NMAX];
queue <int> q;
char viz[NMAX];
int d[NMAX];
void citeste () {
int m, a, b, c;
f >> n >> m >> x >> y;
for (int i = 1; i <= n; i++) f >> a >> b >> c, v[a].push_back(make_pair(b, c)), v[b].push_back(make_pair(a, -c));
}
void rezolva () {
q.push(x);
viz[x] = 1;
int a, b, l;
while (!q.empty()) {
a = q.front();
q.pop();
if (a != y) {
l = v[a].size();
for (int i = 0; i < l; i++) {
b = v[a][i].first;
if (viz[b] == 0) {
viz[b] = 1;
d[b] = d[a] + v[a][i].second;
q.push(b);
}
}
}
}
g << d[y] << '\n';
}
int main () {
citeste ();
rezolva ();
return 0;
}