# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
114586 | mosquir | Evacuation plan (IZhO18_plan) | C++14 | 33 ms | 20472 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
Hasan
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int MX=1e5+9;
const ll inf=(1ll<<61);
int n,m,k,q,a[MX],par[MX],sz[MX];
ll ans[MX];
ll dis[MX];
bool vis[MX];
vector<pii>v[MX];
set<int>s[MX];
int get(int x){
if(x==par[x])return x;
return par[x]=get(par[x]);
}
void mrg(int x,int y,ll cost){
x=get(x);y=get(y);
if(x==y)return;
if(sz[x]>sz[y])swap(x,y);
sz[x]+=sz[y];
par[y]=x;
if(s[x].size()<s[y].size())swap(s[x],s[y]);
for(auto pp:s[y]){
if(s[x].find(pp)!=s[x].end()){
ans[pp]=min(ans[pp],cost);
s[x].erase(pp);
continue;
}
s[x].insert(pp);
}
s[y].clear();
}
priority_queue<pair<ll,int> >pq;
void dij(){
for(int i=1;i<=n;i++)dis[i]=inf,vis[i]=0;
for(int i=1;i<=k;i++){
dis[a[i]]=0;
pq.push({0,a[i]});
}
while(!pq.empty()){
int x=pq.top().second;
ll c=-pq.top().first;pq.pop();
if(vis[x])continue;
vis[x]=1;
for(auto pp:v[x]){
if(dis[pp.first]>c+pp.second){
dis[pp.first]=c+pp.second;
pq.push({-dis[pp.first],pp.first});
}
}
}
}
vector<pair<ll,int> >edge;
int x,y,z;
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d%d%d",&n,&m,&k,&q);
for(int i=1;i<=n;i++)sz[i]=1,par[i]=i,v[i].clear(),ans[i]=0,s[i].clear();
for(int i=1;i<=k;i++)scanf("%d",&a[i]);
for(int i=1;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
v[x].push_back({y,z});
v[y].push_back({x,z});
}
dij();
for(int i=1;i<=q;i++){
scanf("%d%d",&x,&y);
if(!dis[x]||!dis[y])continue;
ans[i]=min(dis[x],dis[y]);
s[x].insert(i);
s[y].insert(i);
}
for(int i=1;i<=n;i++)edge.push_back({-dis[i],i});
sort(edge.begin(),edge.end());
for(auto pp:edge){
for(auto pp1:v[pp.second]){
if(dis[pp1.first]>=-pp.first){
mrg(pp.second,pp1.first,-pp.first);
}
}
}
edge.clear();
for(int i=1;i<=q;i++)cout<<ans[i]<<endl;
}
}
Compilation message (stderr)
# | 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... |