This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//UNSTOPPABLE
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define int long long
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define pii pair<int,int>
#define sigma signed
#define tpii pair <pair <int,int> , int>
#define bruh cout << "NO\n"
using namespace std;
using namespace __gnu_pbds;
const int N = 5e5 + 5;
int mod = 1e9 + 7;
const int INF = 1e18;
int n,m,k,q,a[N],dist[N],used[N],p[N],s[N],t[N],ans[N],l[N],r[N];
vector <pii> g[N];
int get(int x){
if(p[x] == x) return x;
return p[x] = get(p[x]);
}
void merge(int v , int u){
v = get(v);
u = get(u);
if(v == u) return;
p[v] = u;
}
void Goldik(){
cin >> n >> m;
for(int i = 1 ; i <= m ; i++){
int u,v,w;
cin >> u >> v >> w;
g[u].pb({v , w});
g[v].pb({u , w});
}
cin >> k;
set <pii> st;
for(int i = 1 ; i <= n ; i++){
dist[i] = INF;
}
for(int i = 1 ; i <= k ; i++){
cin >> a[i];
dist[a[i]] = 0;
used[a[i]] = 1;
st.insert({0 , a[i]});
}
while(st.size()){
int v = (*st.begin()).S;
// cout << v << '\n';
st.erase(*st.begin());
used[v] = 1;
for(auto to : g[v]){
if(used[to.F]) continue;
if(dist[to.F] > dist[v] + to.S){
dist[to.F] = dist[v] + to.S;
st.insert({dist[to.F] , to.F});
// cout << v << ' ' << to.F << "SIGMA\n";
}
}
}
vector <pii> qu;
cin >> q;
for(int i = 1 ; i <= q ; i++){
cin >> s[i] >> t[i];
qu.pb({(1e8 / 2) , i});
l[i] = 0 , r[i] = 1e8;
}
vector <pii> sig;
for(int i = 1 ; i <= n ; i++){
sig.pb({dist[i] , i});
// cout << i << ' ' << dist[i] << '\n';
}
sort(all(sig));
reverse(all(sig));
while(true){
for(int i = 1 ; i <= n ; i++) p[i] = i;
vector <pii> k;
for(auto it : qu){
int md = it.F , i = it.S;
if(l[i] > r[i]) continue;
k.pb({l[i] + r[i] >> 1 , i});
}
if(!k.size()) break;
qu = k;
sort(all(qu));
reverse(all(qu));
int cur = 0;
for(auto it : qu){
int md = it.F , i = it.S;
while(cur < n){
if(sig[cur].F < md) break;
int v = sig[cur].S;
for(auto to : g[v]){
if(dist[to.F] < md) continue;
merge(v , to.F);
}
cur++;
}
if(get(s[i]) == get(t[i])){
l[i] = md + 1;
}
else{
r[i] = md - 1;
}
}
}
for(int i = 1 ; i <= q ; i++){
cout << r[i] << '\n';
}
}
//rewai mnogo zadach vozmozhno odna iz nih gde to popadetsya
//returning winter prime?
//chem prowe tem luchshe
sigma main(/*AZ AZDAN UZDIKSIZ*/){
//freopen("txt.in","r",stdin);
//freopen("txt.out","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
srand(time(0));
int TT = 1;
// cin >> TT;
for(int i = 1 ; i <= TT ; i++){
//cout << "Case " << i << ": ";
Goldik();
}
}
Compilation message (stderr)
plan.cpp: In function 'void Goldik()':
plan.cpp:87:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
87 | k.pb({l[i] + r[i] >> 1 , i});
| ~~~~~^~~~~~
plan.cpp:85:8: warning: unused variable 'md' [-Wunused-variable]
85 | int md = it.F , i = it.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... |