이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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);
long long dist[n + 5], mn[n + 5], mn2[n + 5], ans[n + 5];
for (int i = 1; i <= n; i++) dist[i] = mn[i] = mn2[i] = ans[i] = 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]});
if (u == t) break;
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... |