이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
const ll inf = 1e16;
vector<ll> dijsktra(vector<vector<pair<int, ll>>> v, int s){
int n = v.size();
vector<ll> dis(n, inf);
vector<bool> bl(n, 0);
priority_queue<pair<ll, int>> pq;
dis[s] = 0;
pq.push({0, s});
while(!pq.empty()){
int nd = pq.top().sc;
pq.pop();
if(bl[nd]) continue;
bl[nd] = 1;
for(auto [x, w]: v[nd]) if(dis[nd]+w < dis[x]){
dis[x] = dis[nd]+w;
pq.push({-dis[x], x});
}
}
return dis;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
int S, T, U, V; cin >> S >> T >> U >> V; S--, T--, U--, V--;
vector<vector<pair<int, ll>>> v(n);
for(int i = 0; i < m; i++){
int a, b; ll w; cin >> a >> b >> w; a--, b--;
v[a].pb({b, w});
v[b].pb({a, w});
}
vector<ll> ds = dijsktra(v, S), du = dijsktra(v, U), dv = dijsktra(v, V);
ll ans = du[V];
vector<pair<ll, ll>> dp1(n, {inf, inf}), dp2(n, {inf, inf});
function<pair<pair<ll, ll>, pair<ll, ll>>(int)> DP = [&](int nd){
if(dp1[nd].f != inf){
return make_pair(dp1[nd], dp2[nd]);
}
ans = min(ans, du[nd]+dv[nd]);
for(auto [x, w]: v[nd]){
if(ds[x]+w != ds[nd]) continue;
auto [p1, p2] = DP(x);
if(p1.f < dp1[nd].f) dp1[nd] = p1;
if(p2.sc < dp2[nd].sc) dp2[nd] = p2;
}
dp1[nd].f = min(dp1[nd].f, du[nd]);
dp1[nd].sc = min(dp1[nd].sc, dv[nd]);
dp2[nd].f = min(dp2[nd].f, du[nd]);
dp2[nd].sc = min(dp2[nd].sc, dv[nd]);
ans = min(ans, dp1[nd].f+dp1[nd].sc);
ans = min(ans, dp2[nd].f+dp2[nd].sc);
return make_pair(dp1[nd], dp2[nd]);
};
DP(T);
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... |