NC22399. Exotic … Ancient City
描述
输入描述
The first line contains three positive integers , describing the number of rows, the maximal possible columns and the number of edges in each pair of adjacent columns, respectively. You need to calculate the answer for each subsystem of columns while the edges between each pair of columns remain.
Each of the following e lines describes a group of edges, containing three positive integers , meaning that for there is an edge with a cost of w. No two edges connect the same pair of vertices, and people can travel between any pair of vertices in the subsystem of each m.
输出描述
Output M lines. The m-th line contains a single integer, the minimum total cost for the subsystem of (m + 1) columns.
示例1
输入:
4 4 8 3 4 12 1 1 20 1 3 22 4 2 12 4 4 2 2 2 2 1 2 2 1 4 2
输出:
62 80 98 116
示例2
输入:
6 6 15 1 2 1 1 3 1 3 4 1 2 4 1 6 3 2 6 5 2 3 5 2 2 3 2 4 3 2 6 4 2 5 4 2 4 6 2 6 6 2 5 5 3 5 1 3
输出:
19 28 37 46 55 64
C++(clang++11) 解法, 执行用时: 215ms, 内存消耗: 28584K, 提交时间: 2021-03-30 13:15:09
#include<bits/stdc++.h> #define pii pair<int,int> #define pb push_back #define fi first #define se second #define ll long long using namespace std; const int M=2e5+9; int n,m,k; int f[M];ll ans[M],s[M]; vector<pii>e[33],cur,pre; int find(int x){return f[x]==x?x:f[x]=find(f[x]);} int main(){ scanf("%d%d%d",&n,&m,&k); for(int i=1,u,v,w;i<=k;++i){ scanf("%d%d%d",&u,&v,&w); for(int j=w;j<30;++j)e[j].pb(pii{u,v+n}); } for(int t=0;t<30;++t){ memset(s,0,sizeof(s)); cur.clear(); for(int i=1;i<=2*n;++i)f[i]=i; for(auto o:e[t]){ int u=find(o.fi),v=find(o.se); if(u>v)swap(u,v); if(u!=v){ f[u]=v; if(u>n&&v>n)cur.pb(pii{u-n,v-n}); s[1]++; } } for(int d=2;cur.size();++d){ pre=cur;cur.clear(); for(auto o:pre){ int u=find(o.fi),v=find(o.se); if(u>v)swap(u,v); if(u!=v){ f[u]=v; if(u>n&&v>n)cur.pb(pii{u-n,v-n}); } else s[d]--; } } for(int i=1;i<=m;++i)s[i]+=s[i-1]; for(int i=1;i<=m;++i)s[i]+=s[i-1]; for(int i=1;i<=m;++i)ans[i]+=1ll*(i+1)*n-1-s[i]; } for(int i=1;i<=m;++i)printf("%lld\n",ans[i]); return 0; }