HDU 4313 Matrix (Kruskal应用)
2015-02-09 16:07
281 查看
Matrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2498 Accepted Submission(s): 940
Problem DescriptionMachines have once again attacked the kingdom of Xions. The kingdom of Xions has N cities and N-1 bidirectional roads. The road network is such that there is a
unique path between any pair of cities.
Morpheus has the news that K Machines are planning to destroy the whole kingdom. These Machines are initially living in K different cities of the kingdom and
anytime from now they can plan and launch an attack. So he has asked Neo to destroy some of the roads to disrupt the connection among Machines. i.e after destroying those roads there should not be any path between any two Machines.
Since the attack can be at any time from now, Neo has to do this task as fast as possible. Each road in the kingdom takes certain time to get destroyed and they
can be destroyed only one at a time.
You need to write a program that tells Neo the minimum amount of time he will require to disrupt the connection among machines.
Input
The first line is an integer T represents there are T test cases. (0<T <=10)
For each test case the first line input contains two, space-separated integers, N and K. Cities are numbered 0 to N-1. Then follow N-1 lines, each containing three, space-separated integers, x y z, which means there is a bidirectional road connecting city x
and city y, and to destroy this road it takes z units of time.Then follow K lines each containing an integer. The ith integer is the id of city in which ith Machine is currently located.
2 <= N <= 100,000
2 <= K <= N
1 <= time to destroy a road <= 1000,000
Output
For each test case print the minimum time required to disrupt the connection among Machines.
Sample Input
1 5 3 2 1 8 1 0 5 2 4 5 1 3 4 2 4 0
Sample Output
10 Hint Neo can destroy the road connecting city 2 and city 4 of weight 5 , and the road connecting city 0 and city 1 of weight 5. As only one road can be destroyed at a time, the total minimum time taken is 10 units of time. After destroying these roads none of the Machines can reach other Machine via any path.
Author
TJU
Source
2012 Multi-University Training Contest 2
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4313
题目大意:n个城市,n-1条路xi yi,破坏每条路需要时间zi,有k个机器,每个机器开始在某个城市中,现在要破坏道路使得任意两个机器不连通,求最小的破坏时间
题目分析:Kruskal的变形,求最大生成树,建完图,按权值从大到小排序,还需要一个标记数组vis用来纪录哪些城市有机器或者说与机器相通,合并的时候只有当两个祖先不都与机器相通时合并,此时如果两个祖先有一个与机器相通,则另一个肯定也与机器相通,因为我们要合并它们,这样构造出来一棵"最大生成树",拿总的权值减去这棵树的权值就是最小破坏时间,注意数字超int要用long long
#include <cstdio> #include <cstring> #include <algorithm> #define ll long long using namespace std; int const MAX = 1e5 + 5; int n, k; ll cnt; int fa[MAX], rank[MAX]; bool vis[MAX]; struct Edge { int u, v, val; }e[MAX]; bool cmp(Edge a, Edge b) { return a.val > b.val; } void UFset() { for(int i = 0; i <= n; i++) fa[i] = i; memset(rank, 0, sizeof(rank)); } int Find(int x) { return x == fa[x] ? x : fa[x] = Find(fa[x]); } void Union(int a, int b) { int r1 = Find(a); int r2 = Find(b); if(r1 == r2) return; if(rank[r1] > rank[r2]) fa[r2] = r1; else { fa[r1] = r2; if(rank[r1] == rank[r2]) rank[r1]++; } } void Kruskal() { int u, v; UFset(); for(int i = 0; i < n - 1; i++) { u = Find(e[i].u); v = Find(e[i].v); if(u != v && !(vis[u] && vis[v])) { if(vis[u] || vis[v]) { vis[u] = true; vis[v] = true; } Union(u, v); cnt += e[i].val; } } return ; } int main() { int T; scanf("%d", &T); while(T--) { int tmp; ll sum = 0; cnt = 0; memset(vis, false, sizeof(vis)); scanf("%d %d", &n, &k); for(int i = 0; i < n - 1; i++) { scanf("%d %d %d", &e[i].u, &e[i].v, &e[i].val); sum += e[i].val; } for(int i = 0; i < k; i++) { scanf("%d", &tmp); vis[tmp] = true; } sort(e, e + n - 1, cmp); Kruskal(); printf("%I64d\n", sum - cnt); } }
相关文章推荐
- hdu 4313 Matrix 树形dp
- hdu 4313 Matrix 树形dp
- HDU-4313-Matrix
- HDU 4313 Matrix 树形dp
- HDU 4313 Matrix (贪心+并查集)
- HDU-4313 Matrix 最小生成树,集合划分
- Kruskal HDOJ 4313 Matrix
- HDU 4313 Matrix
- hdu 4313 Matrix(树形DP)
- HDU 4313 Matrix 树状DP
- hdu 4313 Matrix 并查集 多校联合赛(二) 第四题
- HDU 4313 Matrix
- HDU 4313 Matrix 贪心 || 树形dp
- hdu 4313(类似于kruskal)
- hdu 4313 Matrix(最大生成树)
- HDU 4313 Matrix (贪心+并查集)
- HDU 4313 Matrix 类似 克鲁斯卡尔算法
- hdu 4313(kruskal思想)
- hdu 4313 Matrix
- HDU 4313 Matrix(贪心+并查集)