ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

HDU3938 Portal(最小生成树 + 计数)

2019-08-20 15:42:59  阅读:251  来源: 互联网

标签:10 cnt HDU3938 int sum 路径 最小 计数 Portal


Problem Description

ZLGG found a magic theory that the bigger banana the bigger banana peel .This important theory can help him make a portal in our universal. Unfortunately, making a pair of portals will cost min{T} energies. T in a path between point V and point U is the length of the longest edge in the path. There may be lots of paths between two points. Now ZLGG owned L energies and he want to know how many kind of path he could make.

Input

There are multiple test cases. The first line of input contains three integer N, M and Q (1 < N ≤ 10,000, 0 < M ≤ 50,000, 0 < Q ≤ 10,000). N is the number of points, M is the number of edges and Q is the number of queries. Each of the next M lines contains three integers a, b, and c (1 ≤ a, b ≤ N, 0 ≤ c ≤ 10^8) describing an edge connecting the point a and b with cost c. Each of the following Q lines contain a single integer L (0 ≤ L ≤ 10^8).

Output

Output the answer to each query on a separate line.

Sample Input

10 10 10
7 2 1
6 8 3
4 5 8
5 8 2
2 8 9
6 4 5
2 1 5
8 10 5
7 3 7
7 8 8
10
6
1
5
9
1
8
2
7
6

Sample Output

36
13
1
13
36
1
36
2
16
13

中文题意

给你一个图,有q个询问,每次询问给一个值L,每次询问问的是有多少个点对,这些点对满足起点到终点所有路径中每条路径最长边中取最小值T(有点拗口哈,就是起点到终点所有路径的最长边取min)小于L。

分析

这道题和NOIP2013货车运输很相似。其实只要发现,所有路径的最长边的最小值一定在最小生成树上就行了!为什么呢?我们考虑最长边的最小值对应的那条边,它连接的是两个点集对吧,把它割掉,我们来看看这条边有什么特点。不难发现,这条边是连接两个集合的最小边,(如果不是最小,明显有其他路径来取代原路径),而这正是最小生成树上的边的特点啊!因此这条边一定在最小生成树上,我们只需枚举最小生成树上的边即可。
因此我们按询问和边都从小到大排个序,枚举边,如果超过当前询问,就把当前计数sum作为当前询问的答案。每次合并两个集合的时候,sum+=cnt[a]cnt[b]sum += cnt[a] * cnt[b]sum+=cnt[a]∗cnt[b]就可以了。
在补充一下:为什么不用考虑不在最小生成树上的边呢?因为不在最小生成树上的边加上去就会形成一个环,而不加这条边的时候这个集合已经连通了,加了这条边,它明显大于环上其他的边,而环上任意两点满足题意的路径不可能覆盖了这条边的,因此不可能成为我们要的T。

代码如下:

#include <bits/stdc++.h>
#define LL long long
#define N 10050
using namespace std;
struct node{
	int a, b, c, i;
	bool operator < (const node & A) const{
		return c < A.c;
	}
}d[N * 5], q[N];
int f[N], p[N], sum, ans[N], cnt[N];
int find(int x){
	return x == f[x]? x: f[x] = find(f[x]);
}
int main(){
	int i, j, n, m, t, a, b, c;
	while(scanf("%d%d%d", &n, &m, &t) != EOF){
		for(i = 1; i <= n; i++) f[i] = i;
		for(i = 1; i <= m; i++) scanf("%d%d%d", &d[i].a, &d[i].b, &d[i].c);
		sort(d + 1, d + i);
		for(i = 1; i <= t; i++) scanf("%d", &q[i].c), q[i].i = i;
		sort(q + 1, q + i);
		for(i = 1; i <= t; i++) p[q[i].i] = i;
		for(i = 1; i <= n; i++) cnt[i] = 1;
		j = 1; sum = 0;
		for(i = 1; i <= m; i++){
			c = d[i].c;
			while(c > q[j].c && j <= t){
				ans[j++] = sum;
			}
			a = find(d[i].a); b = find(d[i].b);
			if(a != b){
				f[b] = a;
				sum += cnt[a] * cnt[b];
				cnt[a] += cnt[b];
			}
		}
		while(j <= t) ans[j++] = sum;
		for(i = 1; i <= t; i++) printf("%d\n", ans[p[i]]);
	}
	return 0;
}

标签:10,cnt,HDU3938,int,sum,路径,最小,计数,Portal
来源: https://blog.csdn.net/iamhpp/article/details/99848083

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有