이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 100'005;
vector<array<int, 2>> adj[N];
int n, m;
ll dist[N];
void dijkstra(int src) {
for (int i = 1; i <= n; i++) {
dist[i] = 1e18;
}
priority_queue<array<ll, 2>> pq;
pq.push({0, src});
dist[src] = 0;
while (pq.size()) {
int node = pq.top()[1];
ll cost = -pq.top()[0];
pq.pop();
if (cost > dist[node]) continue;
for (auto [nxt, w] : adj[node]) {
if (cost + w < dist[nxt]) {
dist[nxt] = cost + w;
pq.push({-(cost + w), nxt});
}
}
}
}
array<ll, 2> dist2[N];
ll solve(int s, int e, int u, int v) {
dijkstra(e);
for (int i = 1; i <= n; i++) {
dist2[i][0] = dist[i];
}
ll d = dist[s];
dijkstra(v);
for (int i = 1; i <= n; i++) {
dist2[i][1] = dist[i];
}
priority_queue<array<ll, 3>> pq;
for (int i = 1; i <= n; i++) {
pq.push({-dist2[i][0], -dist2[i][1], i});
}
while (pq.size()) {
array<ll, 2> c = {-pq.top()[0], -pq.top()[1]};
int node = pq.top()[2];
pq.pop();
if (c > dist2[node]) continue;
for (auto [nxt, w] : adj[node]) {
array<ll, 2> newc = c;
newc[0] += w;
if (newc < dist2[nxt]) {
dist2[nxt] = newc;
pq.push({-newc[0], -newc[1], nxt});
}
}
}
dijkstra(s);
ll dist3[n + 1]{};
for (int i = 1; i <= n; i++) {
dist3[i] = dist[i];
}
dijkstra(u);
ll ans = dist[v];
for (int i = 1; i <= n; i++) {
if (dist3[i] + dist2[i][0] == d) {
ans = min(ans, dist[i] + dist2[i][1]);
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
int s, e;
cin >> s >> e;
int u, v;
cin >> u >> v;
for (int i = 0, x, y, w; i < m; i++) {
cin >> x >> y >> w;
adj[x].push_back({y, w});
adj[y].push_back({x, w});
}
cout << min(solve(s, e, u, v), solve(s, e, v, u)) << '\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... |