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;
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 dfs(int v, int p, int t){
dp[v][0]=min(dp[p][0], dist[v][2]);
dp[v][1]=min(dp[p][1], dist[v][3]);
ans=min(ans, dp[v][0]+dp[v][1]);
for(auto to : adj[v]){
if(to.F!=p){
if(dist[v][t]+dist[to.F][t^1]+to.S==sp && dist[v][t]<dist[to.F][t]){
dfs(to.F, v, t);
}
}
}
}
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;
dfs(s, 0, 0);
dfs(t, 0, 1);
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... |