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 forw(i,a,b) for(ll i=a;i<=b;i++)
#define forb(i,a,b) for(ll i=a;i>=b;i--)
#define pu push
#define pb push_back
#define fi first
#define se second
#define all(a) a.begin(),a.end()
#define minimize(a,b) a=min(a,b)
#define maximize(a,b) a=max(a,b)
typedef long long int ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ll,pair<ll,ll> > plll;
const int maxN=2e5+5;
const ll oo=1e18;
ll n,m,U,V,s,t,d[maxN][4],dp[maxN][2],father[maxN];
vector <pll> edge[maxN];
vector <ll> new_edge[maxN];
vector <plll> pend;
void dijk(int s, int id)
{
forw(i,1,n) d[i][id]=oo;
d[s][id]=0;
priority_queue<pll, vector <pll> , greater<pll> > p;
p.pu({0,s});
while (p.size())
{
ll u=p.top().se;
ll val=p.top().fi;
p.pop();
if (val>d[u][id]) continue;
for(auto tmp:edge[u])
{
ll v=tmp.se;
ll add=tmp.fi;
if (val+add<d[v][id])
{
d[v][id]=val+add;
p.pu({d[v][id],v});
}
}
}
return;
}
void solve()
{
cin>>n>>m;
cin>>s>>t>>U>>V;
forw(i,1,m)
{
ll u,v,z; cin>>u>>v>>z;
edge[u].pb({z,v});
edge[v].pb({z,u});
pend.pb({z,{u,v}});
}
dijk(s,0);
dijk(t,1);
dijk(U,2);
dijk(V,3);
for(auto tmp:pend)
{
ll val=tmp.fi; ll u=tmp.se.fi; ll v=tmp.se.se;
if (d[u][0]+val+d[v][1]==d[t][0])
{
father[v]++;
new_edge[u].pb(v);
}
if (d[v][0]+val+d[u][1]==d[t][0])
{
father[u]++;
new_edge[v].pb(u);
}
}
ll ans=d[V][2];
forw(i,1,n)
{
dp[i][0]=d[i][2]; // from U
dp[i][1]=d[i][3]; // from V
}
queue<ll> p;
p.pu(s);
while (p.size())
{
ll u=p.front(); p.pop();
ans=min(ans,dp[u][0]+d[u][3]); // to V
ans=min(ans,dp[u][1]+d[u][2]); // to U
for(int v:new_edge[u])
{
dp[v][0]=min(dp[v][0],dp[u][0]);
dp[v][1]=min(dp[v][1],dp[u][1]);
if (--father[v]==0) p.pu(v);
}
}
cout<<ans;
return;
}
int main()
{
ios::sync_with_stdio();
cin.tie(0);
//freopen("bruh.inp","r",stdin);
//freopen("bruh.out","w",stdout);
solve();
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... |