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
const int INF = 1e18;
vector<int> dijkstra(int n, vector<tuple<int, int, int>> edges, int src) {
vector<vector<pair<int, int>>> g(n);
for (auto [x, y, w]: edges) {
g[x].emplace_back(y, w);
}
vector<int> dist(n, INF);
priority_queue<pair<int, int>> pq;
pq.push({0, src}); dist[src] = 0;
while (pq.size()) {
auto [ds, u] = pq.top(); pq.pop();
if (dist[u] != -ds) continue;
for (auto [v, w]: g[u]) {
if (dist[v] > dist[u]+w) {
dist[v] = dist[u]+w;
pq.push({-dist[v], v});
}
}
}
return dist;
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
int n, m, s, t, u, v;
cin >> n >> m >> s >> t >> u >> v;
s--, t--, u--, v--;
vector<tuple<int, int, int>> g1;
for (int i = 0; i < m; i++) {
int x, y, w; cin >> x >> y >> w; x--, y--;
g1.emplace_back(x, y, w);
g1.emplace_back(y, x, w);
}
vector<int> dists = dijkstra(n, g1, s);
vector<int> distt = dijkstra(n, g1, t);
vector<tuple<int, int, int>> g2;
for (int i = 0; i < n; i++) {
g2.emplace_back(i, n+i, 0);
g2.emplace_back(i, 2*n+i, 0);
g2.emplace_back(n+i, 3*n+i, 0);
g2.emplace_back(2*n+i, 3*n+i, 0);
}
for (auto [x, y, w]: g1) {
g2.emplace_back(x, y, w);
g2.emplace_back(3*n+x, 3*n+y, w);
if (dists[x]+distt[y]+w == dists[t]) {
g2.emplace_back(n+x, n+y, 0);
g2.emplace_back(2*n+y, 2*n+x, 0);
}
}
vector<int> ans = dijkstra(4*n, g2, u);
cout << ans[3*n+v] << '\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... |