이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
using T = pair<int, int>;
const int N = 100005;
int n, m;
vector<pair<int, int>> g[N];
vector<long long> dijk (int src) {
priority_queue<T, vector<T>, greater<T>> pq;
vector<long long> cost(n + 1, 1e15);
pq.emplace(0, src);
cost[src] = 0;
while (!pq.empty()) {
auto [c, v] = pq.top();
pq.pop();
if (cost[v] < c) {
continue;
}
for (auto [u, w] : g[v]) {
if (c + w < cost[u]) {
cost[u] = c + w;
pq.emplace(cost[u], u);
}
}
}
return cost;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
int s, t;
cin >> s >> t;
int uu, vv;
cin >> uu >> vv;
for(int i = 0; i < m; ++i) {
int x, y, w;
cin >> x >> y >> w;
g[x].emplace_back(y, w);
g[y].emplace_back(x, w);
}
auto cu = dijk(uu);
auto cv = dijk(vv);
long long ans = cu[vv];
for (int it = 0; it < 2; ++it) {
priority_queue<T, vector<T>, greater<T>> pq;
auto ct = dijk(t);
vector<long long> cs(n + 1, 1e15);
vector<long long> dp(n + 1, 1e15);
dp[s] = cu[s];
pq.emplace(0, s);
cs[s] = 0;
while (!pq.empty()) {
auto [c, v] = pq.top();
pq.pop();
if (cs[v] < c) {
continue;
}
for (auto [u, w] : g[v]) {
if (c + w < cs[u]) {
cs[u] = c + w;
pq.emplace(cs[u], u);
}
if (c + w == cs[u]) {
dp[u] = min({dp[u], dp[v], cu[u]});
}
}
}
for (int i = 1; i <= n; ++i) {
if (ct[i] + cs[i] == ct[s]) {
ans = min(ans, cv[i] + dp[i]); //u -> x -> i -> v
}
}
swap(s, t);
}
cout << ans << '\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... |