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>
using namespace std;
using ll = long long;
const int maxn=1e5+5;
int n,m,a[4];
bool vis[maxn];
ll dp[maxn];
ll dis[4][maxn],ans;
vector<int>v;
vector<pair<int,int>>E[maxn];
void f(int cp,int sg){
for(int i = 1;i <= n;i++) dp[i] = 1e9;
for(auto i : v){
dp[i] = dis[cp][i];
for(auto j : E[i]){
if(dis[0][j.first] + j.second == dis[0][i] and j.second + dis[0][j.first] + dis[1][i] == dis[0][a[1]]) dp[i] = min(dp[i],dp[j.first]);
}
// if(dp[i] + dis[sg][i] == 0) cout<<i<<' '<<dis[0][i]<<' '<<dis[1][i]<<' '<<dis[2][i]<<' '<<dis[3][i]<<'\n';
ans = min(ans,dp[i] + dis[sg][i]);
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> a[0] >> a[1] >> a[2] >> a[3];
for(int i = 1;i <= m;i++){
int x,y,w; cin >> x >> y >> w;
E[x].push_back({y,w});
E[y].push_back({x,w});
}
for(int i = 0;i < 4;i++){
for(int j = 1;j <= n;j++) dis[i][j] = 1e18,vis[j] = 0;
dis[i][a[i]] = 0;
priority_queue<pair<ll,int>>q;
q.push({0,a[i]});
while(!q.empty()){
int x = q.top().second; q.pop();
if(vis[x]) continue;
if(i == 0) v.push_back(x);
vis[x] = 1;
for(auto j : E[x]){
if(dis[i][j.first] > dis[i][x] + j.second){
dis[i][j.first] = dis[i][x] + j.second;
q.push({-dis[i][j.first],j.first});
}
}
}
}
ans = dis[2][a[3]];
// for(auto i : v) cout<<i<<' ';
// cout<<'\n';
// cout<<ans<<'\n';
f(2,3);
f(3,2);
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... |