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 ss second
#define ff first
typedef long long ll;
// #define int ll
int mod = 1e9+7;
// int inf = 1e18;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// ifstream cin ("shortcut.in");
// ofstream cout ("shortcut.out");
int n, m, s, t, u, v;
cin >> n >> m >> s >> t >> u >> v;
--s; --t; --u; --v;
vector<vector<pair<int,int>>> adj(n);
for(int i = 0; i < m; ++i){
int a,b,c;
cin >> a >> b >> c;
--a; --b;
adj[a].push_back({c,b});
adj[b].push_back({c,a});
}
vector<int> dis(n, 1e9);
dis[s] = 0;
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
vector<vector<int>> par(n), chi(n);
pq.push({0, s});
while(!pq.empty()){
pair<int,int> a = pq.top(); pq.pop();
if(dis[a.ss]!=a.ff){
continue;
}
for(auto &[w, nxt] : adj[a.ss]){
if(a.ff+w<dis[nxt]){
par[nxt].clear();
par[nxt].push_back(a.ss);
pq.push({a.ff+w, nxt});
dis[nxt] = a.ff+w;
}else if(a.ff+w==dis[nxt]){
par[nxt].push_back(a.ss);
}
}
}
vector<bool> ways(n, false);
queue<int> q;
q.push({t});
while(!q.empty()){
int a = q.front(); q.pop();
ways[a] = true;
for(int i : par[a]){
chi[i].push_back(a);
q.push(i);
}
}
vector<vector<int>> free(n);
for(int i = 0; i < n; ++i){
if(!ways[i]) continue;
for(int j : par[i]){
if(ways[j]) free[i].push_back(j);
}
for(int j : chi[i]){
if(ways[j]) free[i].push_back(j);
}
}
using te = pair<int,pair<int,bool>>;
priority_queue<te, vector<te>, greater<te>> ppq;
ppq.push({0,{u,0}});
vector<int> dis_tick(n, 1e9);
dis_tick[u] = 0;
while(!ppq.empty()){
int weight = ppq.top().ff;
int node = ppq.top().ss.ff;
bool go = ppq.top().ss.ss;
ppq.pop();
if(dis_tick[node]!=weight){
continue;
}
if(ways[node]&&!go){
for(int &i : free[node]){
if(weight<dis_tick[i]){
dis_tick[i] = weight;
ppq.push({weight,{i,i==s||i==t}});
}
}
}
for(auto &[w, nxt] : adj[node]){
if(weight+w<dis_tick[nxt]){
dis_tick[nxt] = weight+w;
ppq.push({weight+w,{nxt, go}});
}
}
}
cout << dis_tick[v] << '\n';
return 0;
}
# | 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... |