Pagini recente » Cod sursa (job #2044601) | Cod sursa (job #1116367) | Sandbox (cutiuţa cu năsip) | Cod sursa (job #797230) | Cod sursa (job #1503935)
#include <iostream>
#include <vector>
#include <string.h>
#include <fstream>
#include <queue>
using namespace std;
const int maxn = 30005;
int n, m, x, y, dist[maxn];
vector<pair<int, int> > g[maxn];
int main()
{
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 x, y, z;
scanf("%d %d %d", &x, &y, &z);
g[x].push_back(make_pair(y, z));
g[y].push_back(make_pair(x,-z));
}
memset(dist, -1, sizeof(dist));
queue <int> q;
q.push(x);
dist[x] = 0;
while(!q.empty()) {
int node = q.front();
q.pop();
for(auto el : g[node]) {
if(dist[el.first] == -1) {
dist[el.first] = dist[node] + el.second;
q.push(el.first);
}
}
}
printf("%d\n", dist[y]);
return 0;
}