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;
const int mxN = 1e5 + 5;
int n,m,k;
vector<pair<int,int>> g[mxN];
bool npp[mxN];
int dis[mxN];
void dijkstra_npp(){
for(int i=1;i<=n;i++) dis[i] = INT_MAX;
priority_queue<pair<int,int> , vector<pair<int,int>> , greater<pair<int,int>>> pq;
for(int node=1;node<=n;node++){
if(npp[node]){
dis[node] = 0;
pq.push({dis[node] , node});
}
}
while(!pq.empty()){
int u = pq.top().second;
int w = pq.top().first;
pq.pop();
if(dis[u] < w) continue;
for(auto [v , cost] : g[u] ){
if(dis[v] > w + cost){
dis[v] = w + cost;
pq.push({dis[v] , v});
}
}
}
}
int dis2[mxN];
void solve1(int start_node , int end_node){
// start_node or end_node is npp
if(npp[start_node] || npp[end_node]){
cout<< 0 << "\n";
return ;
}
// direct road
for(auto [v , cost] : g[start_node]){
if(v == end_node){
cout<< min(dis[start_node] , dis[end_node]) << "\n";
return ;
}
}
for(int i=1;i<=n;i++) dis2[i] = 0;
dis2[start_node] = dis[start_node];
priority_queue<pair<int,int>> pq;
pq.push({dis2[start_node] , start_node});
while(!pq.empty()){
int u = pq.top().second;
int w = pq.top().first;
pq.pop();
if(dis2[u] > w) continue;
if(u == end_node){
cout<< dis2[u] << "\n";
return ;
}
for(auto [v , cost] : g[u]){
if(dis2[v] < min(w , dis[v])){
dis2[v] = min(w , dis[v]);
pq.push({dis2[v] , v});
}
}
}
cout<< 0 << "\n";
}
int pa[mxN];
int fp(int x){
if(x == pa[x]) return x;
return pa[x] = fp(pa[x]);
}
vector<pair<int,int>> edge;
void solve2(int start_node , int end_node){
// start_node or end_node is npp
if(npp[start_node] || npp[end_node]){
cout<< 0 << "\n";
return ;
}
// direct road
for(auto [v , cost] : g[start_node]){
if(v == end_node){
cout<< min(dis[start_node] , dis[end_node]) << "\n";
return ;
}
}
int l=0 , r = min(dis[start_node] , dis[end_node]);
int ans = 0;
while(l <= r){
int mid = (l + r) / 2;
for(int i=1;i<=n;i++) pa[i] = i;
for(auto [u,v] : edge){
int pa_u = fp(u) , pa_v = fp(v);
if(pa_u == pa_v) continue;
if(min(dis[u] , dis[v]) < mid) continue;
pa[pa_v] = pa_u;
if(fp(start_node) == fp(end_node)){
break;
}
}
if(fp(start_node) == fp(end_node)){
ans = max(ans , mid);
l = mid + 1;
}else r = mid - 1;
}
cout<< ans << "\n";
}
int main(){
ios::sync_with_stdio(0),cin.tie(0);
cin>> n >> m;
for(int i=0;i<m;i++){
int u,v,w;
cin>> u >> v >> w;
g[u].push_back({v,w});
g[v].push_back({u,w});
edge.push_back({u,v});
}
cin>> k;
for(int i=1;i<=n;i++) npp[i] = false;
for(int i=0;i<k;i++){
int node;
cin>> node;
npp[node] = true;
}
dijkstra_npp();
// for(int i=1;i<=n;i++) cout<< dis[i] << " ";
// cout<< "\n";
int q;
cin>> q;
while(q--){
int start_node , end_node;
cin>> start_node >> end_node;
solve2(start_node , end_node);
}
return 0;
}
Compilation message (stderr)
plan.cpp: In function 'void dijkstra_npp()':
plan.cpp:32:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
32 | for(auto [v , cost] : g[u] ){
| ^
plan.cpp: In function 'void solve1(int, int)':
plan.cpp:52:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
52 | for(auto [v , cost] : g[start_node]){
| ^
plan.cpp:78:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
78 | for(auto [v , cost] : g[u]){
| ^
plan.cpp: In function 'void solve2(int, int)':
plan.cpp:106:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
106 | for(auto [v , cost] : g[start_node]){
| ^
plan.cpp:121:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
121 | for(auto [u,v] : edge){
| ^
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |