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 int long long
vector<long long> path(int n, int s, vector<vector<pair<int, long long>>>&adj) {
vector<long long> dist(n + 5, 1e18);
dist[s] = 0;
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
pq.push({0, s});
while (pq.size()) {
long long p = pq.top().first;
int u = pq.top().second;
pq.pop();
if (p != dist[u]) continue;
for (auto&x : adj[u]) {
int v = x.first;
long long c = x.second;
if (dist[v] < dist[u] + c) continue;
dist[v] = dist[u] + c;
pq.push({dist[v], v});
}
}
return dist;
}
void solve(int n, int s, int t, int u, int v, vector<vector<pair<int, long long>>>&adj, vector<long long>&f, vector<long long>&se) {
vector<long long> dist(n + 5, 1e18), mn(n + 5, 1e18), ans(n + 5, 1e18), mn2(n + 5, 1e18);
dist[s] = 0;
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
pq.push({0, s});
while (pq.size()) {
long long p = pq.top().first;
int u = pq.top().second;
pq.pop();
if (p != dist[u]) continue;
mn[u] = min(mn[u], f[u]);
mn2[u] = min(mn2[u], se[u]);
for (auto&x : adj[u]) {
int v = x.first;
long long c = x.second;
if (dist[v] + c != dist[u]) continue;
mn[u] = min(mn[u], mn[v]);
mn2[u] = min(mn2[u], mn2[v]);
ans[u] = min(ans[u], ans[v]);
}
ans[u] = min({ans[u], se[u] + mn[u], f[u] + mn2[u]});
for (auto&x : adj[u]) {
int v = x.first;
long long c = x.second;
if (dist[v] < dist[u] + c) continue;
dist[v] = dist[u] + c;
pq.push({dist[v], v});
}
}
cout << min({ans[t], f[v]}) << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
int s, t;
cin >> s >> t;
int u, v;
cin >> u >> v;
vector<vector<pair<int, long long>>> adj(n + 5);
for (int i = 1;i <= m; i++) {
int u, v;
long long c;
cin >> u >> v >> c;
adj[u].push_back({v, c});
adj[v].push_back({u, c});
}
vector<long long> f = path(n, u, adj), se = path(n, v, adj);
solve(n, s, t, u, v, adj, f, se);
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... |