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>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
// #pragma GCC optimize("-O3")
// #pragma GCC optimize("no-stack-protector")
// #pragma GCC optimize("fast-math")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
//using namespace __gnu_pbds;
#define fast_io ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define vec vector
#define getin freopen("input.txt","r",stdin);
#define getout ofstream cout("output.txt");
#define getfiles getin;getout
#define m_p make_pair
#define f first
#define sz(x) (int)x.size()
#define pw(x) (1LL<<x)
#define pb push_back
#define endl '\n'
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define s second
//#define int long long
typedef pair<int,int> pii;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef pair<ll,int> pli;
typedef pair<int,ll> pil;
typedef long double ld;
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>o_set;
template<typename T>bool umax(T &a,const T &b) {return (a<b?a=b,1:0);}
template<typename T>bool umin(T &a,const T &b) {return (a>b?a=b,1:0);}
auto rnd=bind(uniform_int_distribution<ll>(1,1e18),mt19937(time(0)));
const int N=1e5+1;
const ll inf=1e18;
ll dst[N];
int p[N],sz[N],n,m;
vec<pii>g[N];
void make(int v){
p[v]=v;
sz[v]=1;
}
int get(int v){
return p[v]=(p[v]==v?v:get(p[v]));
}
void mg(int a,int b){
int c=get(a),d=get(b);
if(c==d)
return;
if(sz[c]<sz[d])
swap(c,d);
p[d]=c;
sz[c]+=sz[d];
sz[d]=0;
}
vec<pli>srt;
pii qry[N];
int l[N],r[N],md[N];
vec<int>ndtoans[N];
signed main(){
fast_io;
cin>>n>>m;
for(int i=0;i<m;i++){
int a,b,c;
cin>>a>>b>>c;--a;--b;
g[a].pb({b,c});
g[b].pb({a,c});
}
int k;
cin>>k;
priority_queue<pli,vec<pli>,greater<pli>>pq;
fill(dst,dst+N,inf);
for(int i=0;i<k;i++){
int x;
cin>>x;--x;
dst[x]=0;
pq.push({0,x});
}
while(!pq.empty()){
auto c=pq.top();
ll dt=c.f;
int v=c.s;
pq.pop();
if(dst[v]<dt)
continue;
for(auto &z : g[v]){
if(dst[z.f]>dst[v]+z.s){
dst[z.f]=dst[v]+z.s;
pq.push({dst[z.f],z.f});
}
}
}
for(int i=0;i<n;i++){
srt.pb({dst[i],i});
// debug()<<imie(i)imie(dst[i]);
}
sort(rall(srt));
int q;
cin>>q;
for(int i=0;i<q;i++){
int a,b;
cin>>a>>b;--a;--b;
qry[i]={a,b};
l[i]=0,r[i]=n-1;
}
bool ok=0;
while(!ok){
ok=1;
for(int i=0;i<n;i++){
make(i);
ndtoans[i].clear();
}
for(int i=0;i<q;i++){
if(l[i]==r[i])
continue;
int mid=(l[i]+r[i])>>1;
ok=0;
md[i]=mid;
ndtoans[md[i]].pb(i);
}
for(int fck=0;fck<n;fck++){
int i=srt[fck].s;
// debug()<<imie(i);
for(auto &z : g[i]){
if(dst[z.f]>=dst[i])
mg(z.f,i);
}
for(auto &j : ndtoans[fck]){
if(j==3){
j=3;
// debug()<<imie(l[j])imie(r[j])imie(md[j]);
}
int a=qry[j].f,b=qry[j].s;
if(get(a)!=get(b)){
l[j]=md[j]+1;
}
else{
r[j]=md[j];
}
}
}
// cout<<endl;
}
for(int i=0;i<q;i++){
cout<<srt[l[i]].f<<endl;
}
return 0;
}
/*
*/
# | 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... |