This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
*/
#include<bits/stdc++.h>
#define int long long
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define eb emplace_back
#define PI 3.14159265359
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define BIT(x,i) (1&((x)>>(i)))
#define MASK(x) (1LL<<(x))
#define task "tnc"
using namespace std;
typedef long long ll;
const ll INF=1e18;
const int maxn=1e5+5;
const int mod=1e9+7;
const int mo=998244353;
using pi=pair<ll,ll>;
using vi=vector<ll>;
using pii=pair<pair<ll,ll>,ll>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n,m;
vector<pair<int,int>>G[maxn];
vector<int>G2[maxn];
int d[maxn];
int f[maxn];
int deg[maxn];
int du[maxn],dv[maxn],ds[maxn];
bool g1[maxn],g2[maxn];
int dp1[maxn],dp2[maxn];
int ok[maxn];
int s,t;
int u,v;
void DIJ2(int s,int d[]){
for(int i=1;i<=n;i++)d[i]=INF;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;
pq.push({0,s});
d[s]=0;
while(pq.size()){
pair<int,int>cha=pq.top();
pq.pop();
if(d[cha.se]!=cha.fi)continue;
for(auto canh:G[cha.se]){
if(d[cha.second]+canh.se<d[canh.fi]){
d[canh.fi]=d[cha.second]+canh.se;
pq.push({d[canh.fi],canh.fi});
}
}
}
}
void dfs1(int q){
if(q==t){
f[q]=1;
return ;
}
for(auto v1:G[q]){
if(ds[q]+v1.se==ds[v1.fi]){
if(!ok[q]){
dfs1(v1.fi);
ok[v1.fi]=1;
}
f[q]|=f[v1.fi];
}
}
}
void dfs2(int q,int dp[],int d[],bool g[]){
dp[q]=d[q];
if(q==t)return;
for(auto v1:G[q]){
if(ds[q]+v1.se==ds[v1.fi]){
if(f[v1.fi]){
if(!g[v1.fi]){
dfs2(v1.fi,dp,d,g);
g[v1.fi]=1;
}
dp[q]=min(dp[q],dp[v1.fi]);
}
}
}
}
signed main()
{
cin.tie(0),cout.tie(0)->sync_with_stdio(0);
//freopen(task".inp" , "r" , stdin);
//freopen(task".out" , "w" , stdout);
cin>>n>>m>>s>>t>>u>>v;
for(int i=1;i<=m;i++){
int x,y,w;
cin>>x>>y>>w;
G[x].pb({y,w});
G[y].pb({x,w});
}
DIJ2(s,ds);
// cout<<ds[s]<<" ";
DIJ2(u,du);
DIJ2(v,dv);
dfs1(s);
int ans=du[v];
dfs2(s,dp1,du,g1);
dfs2(s,dp2,dv,g2);
for(int i=1;i<=n;i++){
if(f[i]){
ans=min(ans,dp1[i]+dv[i]);
ans=min(ans,dp2[i]+du[i]);
}
}
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... |