This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int n, m;
int s, t;
int u, v;
vector<vector<pair<int,int>>> edg;
bool vis[100005];
ll distS[100005];
ll distT[100005];
ll distV[100005];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
cin >> s >> t;
cin >> u >> v;
edg.resize(n+5);
for (int i = 0; i < n; i++) {
int a, b, c;
cin >> a >> b >> c;
edg[a].push_back({b,c});
edg[b].push_back({a,c});
}
priority_queue<pair<int,int>> q;
q.push({0,s});
for (int i = 0; i <= n; i++) distS[i] = 1e18, vis[i] = 0;
distS[s] = 0;
while (!q.empty()) {
int f = q.top().second;
q.pop();
if (vis[f]) continue;
vis[f] = 1;
for (auto &[i,j] : edg[f]) {
if (!vis[i] && distS[i] > (ll) distS[f] + j) {
distS[i] = (ll) distS[f] + j;
q.push({-distS[i], i});
}
}
}
q.push({0,t});
for (int i = 0; i <= n; i++) distT[i] = 1e18, vis[i] = 0;
distT[t] = 0;
while (!q.empty()) {
int f = q.top().second;
q.pop();
if (vis[f]) continue;
vis[f] = 1;
for (auto &[i,j] : edg[f]) {
if (!vis[i] && distT[i] > (ll) distT[f] + j) {
distT[i] = (ll) distT[f] + j;
q.push({-distT[i], i});
}
}
}
q.push({0,v});
for (int i = 0; i <= n; i++) distV[i] = 1e18, vis[i] = 0;
distV[v] = 0;
while (!q.empty()) {
int f = q.top().second;
q.pop();
if (vis[f]) continue;
vis[f] = 1;
for (auto &[i,j] : edg[f]) {
if (!vis[i] && distV[i] > (ll) distV[f] + j) {
distV[i] = (ll) distV[f] + j;
q.push({-distV[i], i});
}
}
}
ll ans = 1e18;
for (int i = 1; i <= n; i++) {
if (distS[t] == distS[i] + distT[i]) {
ans = min(ans, distV[i]);
}
}
cout << ans << '\n';
//cerr << "S: "; for (int i = 1; i <= n; i++) { cerr << distS[i] << " \n" [i == n]; }
//cerr << "T: "; for (int i = 1; i <= n; i++) { cerr << distT[i] << " \n" [i == n]; }
//cerr << "V: "; for (int i = 1; i <= n; i++) { cerr << distV[i] << " \n" [i == n]; }
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |