이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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[2][N];
vector<int>g[N],topolist,rg[N];
vector<pii>adj[N];
bool vis[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();
if(vis[x]) continue;
vis[x]=true;
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[0][x]=d[0][x];
dp[1][x]=d[1][x];
for(auto&pre:rg[x])
{
dp[0][x]=min(dp[0][x],dp[0][pre]);
dp[1][x]=min(dp[1][x],dp[1][pre]);
}
res=min(res,d[1][x]+dp[0][x]);
res=min(res,d[0][x]+dp[1][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... |