This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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... |