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 = 1005;
int n,m,k;
vector<pair<int,int>> g[mxN];
vector<int> npp;
int dis[mxN];
void dijkstra(){
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(auto node : npp){
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 solve(int start_node , int end_node){
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 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});
}
cin>> k;
npp.resize(k);
for(int i=0;i<k;i++) cin>> npp[i];
dijkstra();
// 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;
solve(start_node , end_node);
}
return 0;
}
Compilation message (stderr)
plan.cpp: In function 'void dijkstra()':
plan.cpp:30:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
30 | for(auto [v , cost] : g[u] ){
| ^
plan.cpp: In function 'void solve(int, int)':
plan.cpp:61:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
61 | for(auto [v , cost] : g[u]){
| ^
# | 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... |