이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |