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 ff first
#define ss second
#define all(a) a.begin(), a.end()
const int N = 1e5;
const int M = 1e9;
int n, m, k;
vector< pair<int, int> > g[N];
int AEC[N], distanc[N];
vector<int> A, B;
priority_queue< pair<int, int> > q;
void dijkstra(int start, vector<int>&dis){
for(int i = 0;i < n; i++) dis[i] = (i == start ? 0 : M);
q.push({dis[start], start});
while(!q.empty()){
int v = q.top().ss, D = q.top().ff;
q.pop();
if(D > dis[v]) continue;
for(auto [to, len] : g[v]){
if(dis[to] > dis[v] + len){
dis[to] = dis[v]+len;
q.push({dis[to], to});
if(AEC[start]==0 and AEC[to] == 1){
distanc[start] = min(distanc[start], dis[to]);
}
if(AEC[start]==1 and AEC[to] == 0){
distanc[to] = min(distanc[to], dis[to]);
}
}
}
}
}
main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i = 0;i < m; i++){
int a, b, w; cin >> a >> b >> w;
a--, b--;
g[a].push_back({b, w});
g[b].push_back({a, w});
}
vector<int> dis(n);
cin >> k;
for(int i = 0;i < k; i++){
int a; cin >> a; a--;
AEC[a] = 1;
}
for(int i = 0;i < n; i++){
distanc[i] = M;
if(AEC[i] == 0) B.push_back(i);
else A.push_back(i);
}
if(k > (int)sqrt(n)){
for(int start : B){
dijkstra(start, dis);
}
}else{
for(int start : A){
dijkstra(start, dis);
}
}
int Q; cin >> Q;
if(n <= 15 and m <= 200 and Q <= 200){
vector<vector<int>> dis1(n, vector<int>(n, -M));
vector<int> used(n, 0), path;
int s;
path.push_back(0);
function<void(int)> dfs=[&](int v){
used[v] = 1;
int mn = M;
for(int i = path.size()-1; i >= 0; i--){
if(AEC[path[i]] == 1) mn = 0;
else
mn = min(mn, distanc[path[i]]);
dis1[path[i]][path.back()] = max(dis1[path[i]][path.back()], mn);
}
for(auto [to, len] : g[v]){
if(used[to] == 0){
path.push_back(to);
dfs(to);
path.pop_back();
}
}
used[v] = 0;
};
dfs(0);
while(Q--){
int a, b; cin >> a >> b; a--, b--;
if(dis1[a][b] == -M and dis1[b][a] == -M) assert(false);
if(dis1[a][b] == -M) cout << dis1[b][a];
else cout << dis1[a][b];
cout << '\n';
}
}else{
while(Q--){
int a, b; cin >> a >> b; a--, b--;
if(AEC[a] == 1 or AEC[b] == 1){
cout << 0 << '\n';
}else{
cout << min(distanc[a], distanc[b]) << '\n';
}
}
}
return 0;
}
Compilation message (stderr)
plan.cpp: In function 'void dijkstra(int, std::vector<int>&)':
plan.cpp:24:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
24 | for(auto [to, len] : g[v]){
| ^
plan.cpp: At global scope:
plan.cpp:41:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
41 | main(){
| ^~~~
plan.cpp: In lambda function:
plan.cpp:86:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
86 | for(auto [to, len] : g[v]){
| ^
plan.cpp: In function 'int main()':
plan.cpp:75:7: warning: unused variable 's' [-Wunused-variable]
75 | int s;
| ^
# | 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... |