Pagini recente » Utilizatori inregistrati la Infoarena Monthly 2012 - Runda 5 | Cod sursa (job #2550788)
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
const int NMAX = 30005;
bool viz[NMAX];
int dist[NMAX];
vector <pair<int, int>> vec[NMAX];
queue <int> q;
int main() {
int n, m, x, y;
freopen("sate.in", "r", stdin);
freopen("sate.out", "w", stdout);
scanf("%d%d%d%d", &n, &m, &x, &y);
for(int i = 1; i <= m; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
if(a > b) {
c = -c;
}
vec[a].push_back({b, c});
vec[b].push_back({a, -c});
}
viz[x] = 1;
q.push(x);
while(!q.empty()) {
int u = q.front();
q.pop();
for(auto v : vec[u]) {
if(viz[v.first] == 0) {
dist[v.first] = dist[u] + v.second;
viz[v.first] = 1;
q.push(v.first);
}
}
}
if(dist[y] < 0) {
dist[y] = -dist[y];
}
printf("%d\n", dist[y]);
return 0;
}