Pagini recente » Profil andrici_cezar | Cod sursa (job #1419920) | Cod sursa (job #2766840) | Cod sursa (job #1808502) | Cod sursa (job #3258386)
#include <bits/stdc++.h>
using namespace std;
int n, m, x, y;
vector<vector<pair<int, int>>> v(30001);
int cost[30001];
void bfs() {
memset(cost, -1, sizeof(cost));
queue<int> q;
q.push(x);
cost[x] = 0;
while (!q.empty()) {
int node = q.front();
if (node == y) {
return;
}
q.pop();
for (int i = 0; i < v[node].size(); ++i) {
if (cost[v[node][i].first] == -1) {
cost[v[node][i].first] = cost[node] + v[node][i].second;
q.push(v[node][i].first);
}
}
}
}
int main() {
ifstream cin("sate.in");
ofstream cout("sate.out");
cin >> n >> m >> x >> y;
for (int i = 1; i <= m; ++i) {
int a, b, c;
cin >> a >> b >> c;
v[a].push_back({b, c});
v[b].push_back({a, -c});
}
bfs();
cout << cost[y];
}