Submission #171078

#TimeUsernameProblemLanguageResultExecution timeMemory
171078juggernautEvacuation plan (IZhO18_plan)C++14
100 / 100
900 ms102084 KiB
//Just try and the idea will come! #include<bits/stdc++.h> #define int long long int using namespace std; vector<vector<pair<int,int>>>g(100001); vector<pair<int,int>>p; int n,m,x,y,z,i,dis[100001],par[100001],sz[100001],up[100001][20],sp[100001][20],timer,tin[100001],tout[100001]; int fn(int v){ if(v==par[v])return v; return par[v]=fn(par[v]); } void dfs(int v,int p,int cost){ tin[v]=timer++; up[v][0]=p; sp[v][0]=cost; for(int i=1;i<20;i++){ up[v][i]=up[up[v][i-1]][i-1]; sp[v][i]=min(sp[v][i-1],sp[up[v][i-1]][i-1]); } for(pair<int,int>to:g[v]){ if(to.first!=p)dfs(to.first,v,to.second); } tout[v]=timer++; } bool upper(int a,int b){ return tin[a]<tin[b]&&tout[a]>tout[b]||a==0||b==0; } int lca(int a,int b){ if(upper(a,b))return a; if(upper(b,a))return b; for(int i=19;i>=0;i--)if(!upper(up[a][i],b))a=up[a][i]; return up[a][0]; } int fin(int a,int p){ int res=1e15; for(int i=19;i>=0;i--) if(!upper(up[a][i],p)){ res=min(res,sp[a][i]); a=up[a][i]; } return res; } main(){ scanf("%lld%lld",&n,&m); while(m--){ scanf("%lld%lld%lld",&x,&y,&z); g[x].push_back({y,z}); g[y].push_back({x,z}); p.push_back({x,y}); } for(i=1;i<=n;i++)dis[i]=1e15; scanf("%lld",&m); priority_queue<pair<int,int>>q; while(m--){ scanf("%lld",&x); dis[x]=0; q.push({0,x}); } while(!q.empty()){ x=q.top().second; y=-q.top().first; q.pop(); if(y>dis[x])continue; for(auto to:g[x]){ if(dis[to.first]>y+to.second){ dis[to.first]=y+to.second; q.push({-y-to.second,to.first}); } } } vector<pair<int,pair<int,int>>>vec; for(auto to:p)vec.push_back({min(dis[to.first],dis[to.second]),to}); sort(vec.rbegin(),vec.rend()); for(i=1;i<=n;i++)g[i].clear(),par[i]=i,sz[i]=1; for(auto to:vec){ x=fn(to.second.first); y=fn(to.second.second); if(x!=y){ if(sz[x]<sz[y])swap(x,y); par[y]=x; sz[x]+=sz[y]; g[to.second.first].push_back({to.second.second,to.first}); g[to.second.second].push_back({to.second.first,to.first}); } } scanf("%lld",&m); dfs(1,0,1e15); while(m--){ scanf("%lld%lld",&x,&y); int l=lca(x,y); printf("%lld\n",min(fin(x,l),fin(y,l))); } }

Compilation message (stderr)

plan.cpp: In function 'bool upper(long long int, long long int)':
plan.cpp:26:25: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
     return tin[a]<tin[b]&&tout[a]>tout[b]||a==0||b==0;
            ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
plan.cpp: At global scope:
plan.cpp:43:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main(){
      ^
plan.cpp: In function 'int main()':
plan.cpp:44:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%lld%lld",&n,&m);
     ~~~~~^~~~~~~~~~~~~~~~~~
plan.cpp:46:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld%lld%lld",&x,&y,&z);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:52:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%lld",&m);
     ~~~~~^~~~~~~~~~~
plan.cpp:55:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld",&x);
         ~~~~~^~~~~~~~~~~
plan.cpp:86:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%lld",&m);
     ~~~~~^~~~~~~~~~~
plan.cpp:89:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld%lld",&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...