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;
#define N 100005
struct A{
long long a,w,st,tr;
bool operator<(const A& o)const{
return w>o.w;
}
};
vector<vector<long long>> dis(2,vector<long long>(N,1e18)),dp(3,vector<long long>(N,1e18));
vector<A> g[N];
bitset<N> in;
priority_queue<A> q;
void dijk(int idx,int s){
q.push({s,0});
dis[idx][s]=0;
while(!q.empty()){
auto [a,w,st,tr]=q.top();
q.pop();
if(w>dis[idx][a])continue;
for(auto x:g[a]){
if(w+x.w<dis[idx][x.a])q.push({x.a,w+x.w}),dis[idx][x.a]=w+x.w;
}
}
}
int main(){
int n,m,s,t,u,v,i,a,b;
long long w,mi;
scanf("%d %d %d %d %d %d",&n,&m,&s,&t,&u,&v);
for(i=1;i<=m;i++){
scanf("%d %d %lld",&a,&b,&w);
g[a].push_back({b,w});
g[b].push_back({a,w});
}
dijk(0,s);
dijk(1,t);
mi=dis[0][t];
for(i=1;i<=n;i++)printf("i=%d %d\n",i,dis[0][i]);
for(i=1;i<=n;i++)if(dis[0][i]+dis[1][i]==mi)in[i]=true;
q.push({u,0,0,0});
dp[0][u]=0;
while(!q.empty()){
auto [a,w,st,tr]=q.top();
q.pop();
if(w>dp[st][a])continue;
if(st==0&&in[a]&&w<dp[1][a])q.push({a,w,1,0}),dp[1][a]=w;
for(auto x:g[a]){
if(st!=1&&w+x.w<dp[st][x.a])q.push({x.a,w+x.w,st,tr}),dp[st][x.a]=w+x.w;
if(st==1&&w+x.w<dp[2][x.a])q.push({x.a,w+x.w,2,tr}),dp[2][x.a]=w+x.w;
if(st==1&&in[x.a]&&abs(dis[0][a]-dis[0][x.a])==x.w&&w<dp[1][x.a]){
if(!tr)q.push({x.a,w,1,(dis[0][a]-dis[0][x.a]>0)?1:-1}),dp[1][x.a]=w;
else if(a!=s&&a!=t&&tr==1&&dis[0][a]-dis[0][x.a]>0)q.push({x.a,w,1,1}),dp[1][x.a]=w;
else if(a!=s&&a!=t&&tr==-1&&dis[0][a]-dis[0][x.a]<0)q.push({x.a,w,1,-1}),dp[1][x.a]=w;
}
}
}
printf("%lld",min({dp[0][v],dp[1][v],dp[2][v]}));
return 0;
}
Compilation message (stderr)
commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:42:36: warning: format '%d' expects argument of type 'int', but argument 3 has type '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} [-Wformat=]
42 | for(i=1;i<=n;i++)printf("i=%d %d\n",i,dis[0][i]);
| ~^
| |
| int
| %lld
commuter_pass.cpp:33:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | scanf("%d %d %d %d %d %d",&n,&m,&s,&t,&u,&v);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:35:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | scanf("%d %d %lld",&a,&b,&w);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |