이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
vector<long long> 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<long long> dist(n, INF);
priority_queue<pair<long long, 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<long long> dists = dijkstra(n, g1, s);
vector<long long> 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<long long> 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... |