Pagini recente » Cod sursa (job #586248) | Cod sursa (job #1453720) | Cod sursa (job #401480) | Cod sursa (job #3196958) | Cod sursa (job #2814365)
/* [A][M][C][B][N] / [K][R][I][P][6][8] */
#include <bits/stdc++.h>
using namespace std;
const char sp = ' ', nl = '\n';
const int MOD = 1000000007;
ifstream fin("sate.in");
ofstream fout("sate.out");
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, m, x, y;
fin >> n >> m >> x >> y;
if (x > y) x ^= y ^= x ^= y;
vector<vector<pair<int, int>>> edgeList(n + 1);
for (int q = 0; q < m; ++q) {
int i, j, d;
fin >> i >> j >> d;
if (i > j) i ^= j ^= i ^= j;
edgeList[i].push_back(make_pair(j, d));
edgeList[j].push_back(make_pair(i, -d));
}
edgeList[y].push_back(make_pair(0, 0));
queue<pair<int, int>> q;
q.push(make_pair(x, 0));
int cur_idx, cur_dist;
while (!q.empty()) {
if (!q.front().first) {
fout << q.front().second;
return 0;
}
cur_idx = q.front().first, cur_dist = q.front().second, q.pop();
if (edgeList[cur_idx].empty()) continue;
for (const pair<int, int>& pr : edgeList[cur_idx]) {
q.push(make_pair(pr.first, cur_dist + pr.second));
}
edgeList[cur_idx].clear();
}
fout << -1;
}