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>
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define all(a) a.begin() , a.end()
#define F first
#define S second
using namespace std;
using ll = long long;
const int N = 2e5+5 , inf = 2e9 + 7;
const ll INF = 1e18 , mod = 1e9+7 , P = 6547;
vector<pair<int,int>> g[N];
int bad[N];
int d[N];
int s[N] , t[N] , l[N] , r[N] , p[N] , sz[N] , ans[N] , u[N];
int get(int v) {
if(v == p[v]) return v;
return p[v] = get(p[v]);
}
void merge(int a , int b) {
a = get(a);
b = get(b);
if(a == b) return;
if(sz[a] < sz[b]) swap(a , b);
sz[a] += sz[b];
p[b] = a;
}
void solve() {
int n , m;
cin >> n >> m;
for(int i = 1; i <= m; i ++) {
int a , b, c;
cin >> a >> b >> c;
g[a].push_back({b , c});
g[b].push_back({a , c});
}
for(int i = 1; i <= n; i ++) {
d[i] = inf;
}
set<pair<int,int>> dij;
int k;
cin >> k;
for(int i = 1; i <= k; i ++) {
int x;
cin >> x;
bad[x] = 1;
d[x] = 0;
dij.insert({d[x] , x});
}
while(dij.size()) {
pair<int,int> cur = *dij.begin();
dij.erase(cur);
for(auto to: g[cur.S]) {
if(cur.F + to.S < d[to.F]) {
d[to.F] = cur.F + to.S;
dij.insert({d[to.F] , to.F});
}
}
}
int q;
cin >> q;
for(int i = 1; i <= q; i ++) {
cin >> s[i] >> t[i];
if(bad[s[i]] || bad[t[i]]) {
ans[i] = 0;
l[i] = 1;
continue;
}
else {
l[i] = 0 , r[i] = (int)(1e9);
}
}
for(int JUMBL = 0; JUMBL < 30; JUMBL ++) {
vector<pair<int,int>> events;
for(int i = 1; i <= q; i ++) {
if(l[i] <= r[i]) {
int md = l[i]+r[i] >> 1;
events.push_back({md , -i});
}
}
if(events.empty()) continue;
for(int i = 1; i <= n; i ++) {
events.push_back({d[i] , i});
u[i] = 0;
p[i] = i;
sz[i] = 1;
}
sort(all(events));
reverse(all(events));
for(auto to: events) {
int x = to.F , i = to.S;
if(i > 0) {
//i = -i;
u[i] = 1;
for(auto to: g[i]) {
if(u[to.F] == 1) {
merge(i , to.F);
}
}
}
else {
i = -i;
if(get(s[i]) == get(t[i])) {
ans[i] = x;
l[i] = x+1;
}
else {
r[i] = x-1;
}
}
}
}
for(int i = 1; i <= q; i ++) {
cout << ans[i] << '\n';
}
}
/*
*/
main() {
ios;
int tt = 1;
//cin >> tt;
while(tt --) {
solve();
}
return 0;
}
Compilation message (stderr)
plan.cpp: In function 'void solve()':
plan.cpp:87:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
87 | int md = l[i]+r[i] >> 1;
| ~~~~^~~~~
plan.cpp: At global scope:
plan.cpp:130:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
130 | main() {
| ^
# | 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... |