이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9+7;
const int N =1e5+5;
const ll INF = 1e18;
int n,m,T;
bool vis[N];
ll du[N],dv[N],ds[N],dt[N],ans;
vector<pair<int,ll> >v[2*N];
priority_queue<pair<ll,int> >pq;
multiset<ll>st;
void Dijkstra(ll *d,int s){
for(int i=1;i<=N;i++)d[i]=INF;
d[s]=0;
pq.push({-d[s],s});
while(!pq.empty()){
auto cur=pq.top();
pq.pop();
for(auto e:v[cur.second]){
if(d[cur.second]+e.second<d[e.first]){
d[e.first]=d[cur.second]+e.second;
pq.push({-d[e.first],e.first});
}
}
}
}
void dfs(int cur,int c){
vis[cur]=true;
if(!st.empty()){
if(!c)ans=min(ans,*(st.begin())+dv[cur]);
else ans=min(ans,*(st.begin())+du[cur]);
}
for(auto e:v[cur]){
if(ds[cur]+e.second+dt[e.first]==ds[T]&&!vis[e.first]){//if e is part of shortest path
if(!c)st.insert(du[cur]);
else st.insert(dv[cur]);
dfs(e.first,c);
if(!c)st.erase(st.find(du[cur]));
else st.erase(st.find(dv[cur]));
}
}
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int s,t,a,b,x,y,z;
cin>>n>>m;
cin>>s>>t;
cin>>a>>b;
T=t;
for(int i=1;i<=m;i++){
cin>>x>>y>>z;
v[x].push_back({y,z});
v[y].push_back({x,z});
}
Dijkstra(du,a);
Dijkstra(dv,b);
Dijkstra(ds,s);
Dijkstra(dt,t);
ans=INF;
dfs(s,0);
if(!st.empty())st.clear();
memset(vis,0,sizeof(vis));
dfs(s,1);
ans=min(ans,du[b]);//if u and v are outside of shortest path from s to v, then cost is shortest path from u to v
cout<<ans;
return 0;
}
# | 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... |