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 tii tuple<int, int, int>
#define pii pair<int, int>
#define ff first
#define ss second
const int maxn = 1e5 + 5, INF = 0x3f3f3f3f3f3f3f3f;
vector<pii> adj[maxn];
vector<int> g[maxn];
vector<tii> edges;
vector<int> dijkstra(int ini){
priority_queue<pii, vector<pii>, greater<pii>> pq;
vector<int> dist(maxn, INF);
dist[ini] = 0;
pq.push({0, ini});
while(pq.size()){
auto[d, u] = pq.top();
pq.pop();
if(d > dist[u]) continue;
for(auto [v, w] : adj[u]) if(dist[u]+w < dist[v]){
dist[v] = dist[u] + w;
pq.push({dist[u]+w, v});
}
}
return dist;
}
int solve(int ini, int fim){
int ans = -1;
priority_queue<tii, vector<tii>, greater<tii>> pq;
int dist[maxn][3];
//dist[u][0,1,2] = didnt enter sp, in sp, out of sp | sp = short path
for(int i = 0; i < maxn; i++){
dist[i][0] = dist[i][1] = dist[i][2] = INF;
}
dist[ini][0] = 0;
pq.push({0, ini, 0});
while(pq.size()){
auto[d, u, tp] = pq.top();
pq.pop();
if(d > dist[u][tp]) continue;
if(u==fim) {ans = d; break;}
//enter/continue in sp ---> 0 or 1 (didnt or in)
if(tp==0 || tp==1) for(int v : g[u]) if(dist[v][1] > d){
dist[v][1] = d;
pq.push({d, v, 1});
}
//get out ---> 1 (in)
if(tp==1){
for(auto [v, w] : adj[u]) if(dist[v][2] > d+w){
dist[v][2] = d+w;
pq.push({d+w, v, 2});
}
} // continue out ---> 0 or 2 (didnt or out)
else for(auto [v, w] : adj[u]) if(dist[v][tp] > d+w){
dist[v][tp] = d+w;
pq.push({d+w, v, tp});
}
}
return ans;
}
int32_t main(){
int n, m; cin >> n >> m;
int S, T; cin >> S >> T;
int U, V; cin >> U >> V;
for(int i = 0; i < m; i++){
int a, b, c; cin >> a >> b >> c;
adj[a].push_back({b, c});
adj[b].push_back({a, c});
edges.push_back({a, b, c});
edges.push_back({b, a, c});
}
vector<int> distS = dijkstra(S), distT = dijkstra(T);
int minpath = distS[T];
for(auto[a, b, c] : edges){
if(distS[a]+c+distT[b] == minpath){
g[a].push_back(b);
}
}
int ans = solve(U,V);
for(int i = 1; i <= n; i++) g[i].clear();
for(auto[a, b, c] : edges){
if(distS[a]+c+distT[b] == minpath){
g[b].push_back(a);
}
}
ans = min(ans, solve(U,V));
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... |