이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define st first
#define nd second
using namespace std;
using ll=long long;
const int N=1e5+5;
const ll INF=1e18;
vector<pair<int, int> > E[N];
int n;
vector<ll> dij(int s){
vector<ll> dist(n+1, INF);
vector<bool> vis(n+1);
dist[s]=0;
priority_queue<pair<ll, int> > Q;
for(int i=1; i<=n; i++){
Q.push({-dist[i], i});
}
while(Q.size()){
//cout<<"a";
int v=Q.top().nd;
Q.pop();
if(vis[v])continue;
vis[v]=1;
for(auto u:E[v]){
if(dist[v]+u.nd<dist[u.st]){
dist[u.st]=dist[v]+u.nd;
Q.push({-dist[u.st], u.st});
}
}
}
return dist;
}
vector<ll> ds,dt,du,dv;
ll opt[N];
int main(){
int m;
cin>>n>>m;
int s, t;
int U, V;
cin>>s>>t>>U>>V;
for(int i=0; i<m; i++){
int u, v, w;
cin>>u>>v>>w;
E[u].push_back({v, w});
E[v].push_back({u, w});
}
ds=dij(s), dt=dij(t), du=dij(U), dv=dij(V);
//for(auto i:ds)cout<<i<<" ";
ll ans=du[V];
vector<int> kol(n);
for(int i=0; i<n; i++)kol[i]=i+1;
sort(kol.begin(), kol.end(), [](int a, int b){return ds[a]<ds[b];});
for(int i=1; i<=n; i++)opt[i]=du[i];
for(int v:kol){
for(auto u:E[v]){
if(dt[v]+ds[u.st]+u.nd==ds[t]){
opt[v]=min(opt[v], opt[u.st]);
}
}
if(dt[v]+ds[v]==ds[t])ans=min(ans, opt[v]+dv[v]);
}
//reverse(kol.begin(), kol.end());
for(int i=1; i<=n; i++)opt[i]=dv[i];
for(int v:kol){
for(auto u:E[v]){
if(dt[v]+ds[u.st]+u.nd==ds[t]){
opt[v]=min(opt[v], opt[u.st]);
}
}
//cout<<v<<" "<<opt[v]<<"\n";
if(dt[v]+ds[v]==ds[t])ans=min(ans, opt[v]+du[v]);
}
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... |