Submission #114587

#TimeUsernameProblemLanguageResultExecution timeMemory
114587mosquirEvacuation plan (IZhO18_plan)C++14
100 / 100
1241 ms37224 KiB
/* 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(){ cin>>n>>m; 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<=m;i++){ scanf("%d%d%d",&x,&y,&z); v[x].push_back({y,z}); v[y].push_back({x,z}); } cin>>k; for(int i=1;i<=k;i++)scanf("%d",&a[i]); dij(); cin>>q; 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; } /* 9 12 1 9 4 1 2 5 2 3 7 2 4 3 4 3 6 3 6 4 8 7 10 6 7 5 5 8 1 9 5 7 5 4 12 6 8 2 2 4 7 5 1 6 5 3 4 8 5 8 1 5 */

Compilation message (stderr)

plan.cpp: In function 'int main()':
plan.cpp:63:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d%d%d",&x,&y,&z);
             ~~~~~^~~~~~~~~~~~~~~~~~~
plan.cpp:68:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         for(int i=1;i<=k;i++)scanf("%d",&a[i]);
                              ~~~~~^~~~~~~~~~~~
plan.cpp:72:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d%d",&x,&y);
             ~~~~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...