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 MAXN=100005,INF=1e9;
typedef pair<int,int> pii;
vector<pii> adj[MAXN],query[MAXN];
int dist[MAXN],ans[MAXN],par[MAXN],a[MAXN],b[MAXN],lo[MAXN],hi[MAXN];
bool udah[MAXN];
pair<int,pii> arr[5*MAXN];
vector<int> mid[5*MAXN];
int find(int x){
if(par[x]==x)return par[x];
return par[x]=find(par[x]);
}
void join(int a,int b){
int ortua=find(a),ortub=find(b);
par[ortua]=ortub;
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int n,m,u,v,w,k,q;
cin >> n >> m;
for(int i=1;i<=m;i++){
cin >> u >> v >> w;
adj[u].push_back({v,w});
adj[v].push_back({u,w});
}
cin >> k;
for(int i=1;i<=n;i++){
dist[i]=INF;
}
priority_queue<pii,vector<pii>,greater<pii> > PQ;
for(int i=1;i<=k;i++){
cin >> u;
dist[u]=0;
PQ.push({0,u});
}
while(!PQ.empty()){
pii now=PQ.top();
PQ.pop();
if(dist[now.second]<now.first)continue;
for(auto nxt : adj[now.second]){
if(dist[now.second]+nxt.second<dist[nxt.first]){
dist[nxt.first]=dist[now.second]+nxt.second;
PQ.push({dist[nxt.first],nxt.first});
}
}
}
int no=0;
for(int i=1;i<=n;i++){
for(auto nxt : adj[i]){
if(i>nxt.first)continue;
no++;
arr[no]={min(dist[i],dist[nxt.first]),{i,nxt.first}};
}
}
sort(arr+1,arr+m+1);
cin >> q;
for(int i=1;i<=q;i++){
cin >> a[i] >> b[i];
lo[i]=1;hi[i]=m;
mid[(lo[i]+hi[i])/2].push_back(i);
}
bool masih=1;
while(masih){
masih=0;
for(int i=1;i<=n;i++)par[i]=i;
for(int i=m;i>=1;i--){
join(arr[i].second.first,arr[i].second.second);
while(!mid[i].empty()){
int last=mid[i].back();
mid[i].pop_back();
if(find(a[last])==find(b[last])){
lo[last]=i+1;
ans[last]=arr[i].first;
}
else hi[last]=i-1;
if(lo[last]<=hi[last]){
mid[(lo[last]+hi[last])/2].push_back(last);
masih=1;
}
}
}
}
for(int i=1;i<=q;i++)cout << ans[i] << '\n';
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... |