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>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
using namespace std;
const int N=1e5+5;
int n,m,s,t,u,v,deg[N];
long long d[3][N],dp[N];
vector<int>g[N],topolist,rg[N];
vector<pii>adj[N];
void dijkstra(int i,int u)
{
priority_queue<pair<long long,int>,vector<pair<long long,int>>,greater<pair<long long,int>>>pq;
d[i][u]=0;
pq.push(mp(0,u));
while(!pq.empty())
{
int x=pq.top().se;
long long dist=pq.top().fi;
pq.pop();
if(d[i][x]!=dist) continue;
for(auto&to:adj[x])
{
if(d[i][to.fi]==-1||d[i][to.fi]>dist+to.se)
{
d[i][to.fi]=dist+to.se;
pq.push(mp(d[i][to.fi],to.fi));
}
}
}
}
void topo()
{
queue<int>pq;
pq.push(s);
while(!pq.empty())
{
int x=pq.front();
pq.pop();
topolist.push_back(x);
for(auto&to:g[x])
{
deg[to]--;
if(deg[to]==0) pq.push(to);
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n>>m>>s>>t>>u>>v;
while(m--)
{
int u,v,w;
cin>>u>>v>>w;
adj[u].push_back(mp(v,w));
adj[v].push_back(mp(u,w));
}
memset(d,-1,sizeof d);
dijkstra(0,u);
dijkstra(1,v);
dijkstra(2,s);
queue<int>cur;
cur.push(t);
while(!cur.empty())
{
int x=cur.front();
cur.pop();
for(auto&to:adj[x])
{
if(d[2][x]==d[2][to.fi]+to.se)
{
g[to.fi].push_back(x);
rg[x].push_back(to.fi);
deg[x]++;
cur.push(to.fi);
}
}
}
topo();
long long res=d[0][v];
for(auto&x:topolist)
{
dp[x]=1e18;
if(d[0][x]!=-1) dp[x]=d[0][x];
for(auto&pre:rg[x]) dp[x]=min(dp[x],dp[pre]);
if(d[1][x]!=-1) res=min(res,d[1][x]+dp[x]);
}
cout<<res;
}
# | 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... |