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 int long long
#define pb push_back
#define F first
#define S second
const int N = 1e5+5;
const int inf = 1e16;
vector<pair<int, int>> adj[N];
int dist[N][4];
bool mark[N];
int dp[N][2];
int sp;
int ans;
vector<int> dag[N];
int indeg[N];
int hv[N];
void dijkstra(int s, int t){
for(int i=0; i< N; i++){
mark[i]=0;
dist[i][t]=inf;
}
dist[s][t]=0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, s});
while(pq.size()){
auto p = pq.top();
pq.pop();
if(mark[p.S] || dist[p.S][t]<p.F)continue;
mark[p.S]=1;
for(auto to : adj[p.S]){
if(!mark[to.F] && to.S+dist[p.S][t]<dist[to.F][t]){
dist[to.F][t]=to.S+dist[p.S][t];
pq.push({dist[to.F][t], to.F});
}
}
}
}
void calcdp(int s){
for(int i=0; i<N; i++){
dp[i][0]=dp[i][1]=inf;
hv[i]=0;
}
queue<int> q;
dp[s][0]=dist[s][2];
dp[s][1]=dist[s][3];
q.push(s);
while(q.size()){
int v = q.front();
q.pop();
ans=min({ans, dp[v][0]+dist[v][3], dp[v][1]+dist[v][2]});
for(int to : dag[v]){
dp[to][0]=min(dp[v][0], dist[to][2]);
dp[to][1]=min(dp[v][1], dist[to][3]);
hv[to]++;
if(hv[to]==indeg[to])q.push(to);
}
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, s, t, u, v;
cin >> n >> m >> s >> t >> u >> v;
for(int i=0; i< m; i++){
int a, b, c;
cin >> a >> b >> c;
adj[a].pb({b, c});
adj[b].pb({a, c});
}
dijkstra(s, 0);
dijkstra(t, 1);
dijkstra(u, 2);
dijkstra(v, 3);
sp=dist[s][1];
ans=dist[v][2];
dp[0][0]=inf;
dp[0][1]=inf;
for(int i=1; i<=n; i++){
for(auto to : adj[i]){
if(dist[i][0]+dist[to.F][1]+to.S==sp){
dag[i].pb(to.F);
indeg[to.F]++;
}
}
}
calcdp(s);
for(int i=1; i<=n; i++){
indeg[i]=0;
dag[i].clear();
}
for(int i=1; i<=n; i++){
for(auto to : adj[i]){
if(dist[i][1]+dist[to.F][0]+to.S==sp){
dag[i].pb(to.F);
indeg[to.F]++;
}
}
}
calcdp(t);
cout << ans << '\n';
}
# | 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... |