이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pi;
#ifdef DEBUG
#define D(x...) printf(x);
#else
#define D(x...)
#endif
const ll MAXN = 1e5 + 5;
const ll INF = 1ll<<62;
ll N,M,S,T,U,V,UD[MAXN],VD[MAXN],SD[MAXN],vis[MAXN],ans;
pi dp[MAXN];
vector<pi> edge[MAXN];
pi dfs(int i){
D("%d: ud,vd = %d, %d\n", i, UD[i], VD[i])
if(i == S) return {UD[S],VD[S]};
if(vis[i]) return dp[i];
vis[i] = 1;
ll udist = INF/2, vdist = INF/2;
for(auto e : edge[i]){
if(SD[e.first] + e.second == SD[i]){
auto res = dfs(e.first);
if(min(UD[i], res.first) + min(VD[i], res.second) < udist + vdist){
udist = min(UD[i], res.first);
vdist = min(VD[i], res.second);
}
}
}
dp[i] = {udist, vdist};
return {udist, vdist};
}
void djikstras(int src, ll *d, ll s){
priority_queue<pi, vector<pi>, greater<pi>> pq;
fill(d, d+s+1, INF);
pq.push({d[src] = 0, src});
while(!pq.empty()){
auto t = pq.top(); pq.pop();
if(d[t.second] != t.first) continue;
for(auto e : edge[t.second]){
if(d[t.second] + e.second < d[e.first]){
pq.push({d[e.first] = d[t.second] + e.second, e.first});
}
}
}
}
int main(){
cin >> N >> M >> S >> T >> U >> V;
for(int i = 0; i < M; i++){
ll ai, bi, ci;
cin >> ai >> bi >> ci;
edge[bi].push_back({ai, ci});
edge[ai].push_back({bi, ci});
}
djikstras(S, SD, N);
djikstras(U, UD, N);
djikstras(V, VD, N);
ans = UD[V];
auto res = dfs(T);
ans = min(ans, res.first + res.second);
cout << ans << "\n";
}
# | 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... |