이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pii;
const ll MXN = 1e5+1, INF = 1e18;
ll n,m,s,t,u,v,disS[MXN],disU[MXN],disV[MXN],ans;
vector<pii> adj[MXN];
pii cache[MXN][2];
void dijk(int s, ll* dis) {
priority_queue<pii, vector<pii>, greater<pii>> pq;
fill(dis, dis+n+1, INF);
pq.emplace(0, s);
dis[s] = 0;
while (pq.size()) {
pii u = pq.top(); pq.pop();
if (u.first != dis[u.second]) continue;
for (pii e: adj[u.second]) {
ll nw = e.second + u.first;
if (nw < dis[e.first]) {
dis[e.first] = nw;
pq.emplace(nw, e.first);
}
}
}
}
pii dp(int u, int dir) {
if (cache[u][dir].first < 0) {
cache[u][dir] = {INF, INF};
for (pii e: adj[u]) {
ll nw = disS[u] + (dir ? e.second : -e.second);
if (nw == disS[e.first]) {
pii res = dp(e.first, dir);
cache[u][dir].first = min(cache[u][dir].first, res.first);
cache[u][dir].second = min(cache[u][dir].second, res.second);
}
}
if (cache[u][dir].first != INF || u == (dir ? t : s)) {
cache[u][dir].first = min(cache[u][dir].first, disU[u]);
cache[u][dir].second = min(cache[u][dir].second, disV[u]);
}
}
return cache[u][dir];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> s >> t >> u >> v;
for (int i = 0,a,b,c; i < m; ++i) {
cin >> a >> b >> c;
adj[a].emplace_back(b,c);
adj[b].emplace_back(a,c);
}
dijk(s, disS);
dijk(u, disU);
dijk(v, disV);
fill(&cache[0][0], &cache[n+1][0], make_pair(-1,-1));
ans = disU[v];
for (int i = 1; i <= n; ++i) {
pii be = dp(i,1), ab = dp(i,0);
ans = min({ans, ab.first + be.second, ab.second + be.first});
}
cout << ans;
}
# | 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... |