列表

详情


NC22399. Exotic … Ancient City

描述

``Welcome to the ancient city Xi'an!''

Coming down the gangway and coming up the stairs, Rikka feels a strong exoticism among the simplified Chinese characters and clean frameless designs.

It all started with that letter, an invitation to Xi'an for a one-week trip from the author named LCR. The mysterious name refers to a girl who is probably from Xi'an or Yuyang --- Rikka got such information after a week of hard work. As the lord of Evil Eye, she has a great interest in this, even stronger than that to the city with a millennium history which used to be the capital of 13 dynasties.

Finally, Rikka reached this less valued city, looking forward to an amazing meeting quixotically.

But LCR never appeared. Bored Rikka noticed a map of Xi'an on the wall of the hall. The roads inherit Tang Chang'an city's mesh layout, just like a chessboard. Rikka has felt its convenience since visiting Kyoto, but she also knows about the problems. Crossings block the traffic and the old roads occupy the space for new efficient designs.

This has reminded the girl about her perfect unique solution. Her traffic system includes a set of teleporter vertices V and a set of some bidirectional horizon-wormhole edges E linking them. People can teleport from one terminal to the other in no time. The vertices form an grid, that is, there are n rows and (m+1) columns, containing vertices in total. Let be the vertex at the x-th row of the y-th column. In her initial design, every edge is between two vertices in distinct adjacent columns, and for each pair of adjacent columns, the edges among them are similar. That is, if , then for holds. Also, people can travel by the edges between any pair of vertices in each two-column subsystem, which means, for , if we remove all vertices and edges except those in or between the i-th column and the (i + 1)-th column, the remaining vertices are still connected. No two edges connect the same pair of vertices.

Actually, the horizon-wormholes are expensive. Rikka knows each edge has an integer cost level, but the cost levels are fairly low because the energy of horizon-wormholes is too large and chaotic to measure or calculate accurately. The edges connecting the same pair of rows in every column have the same cost. That is, , if , where w(e) is the cost of edge e. Now Rikka wants to delete some (maybe zero) edges in order to minimize the total cost which is the sum of costs of all remained edges. The connectivity among all vertices must hold. That is, we can still travel between each pair of vertices, possibly passing any other columns. However, connecting each two-column subsystem or keeping edges between each pair of adjacent columns the same is not necessary.

Also, she may only use a consecutive part of columns of her initial design, so the answer for each fewer m is needed. Could you help her?

输入描述

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;
}

上一题