博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4313
阅读量:6037 次
发布时间:2019-06-20

本文共 3989 字,大约阅读时间需要 13 分钟。

Matrix

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1390 Accepted Submission(s): 507

Problem Description
Machines 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.
1 //题意:有多个城市,多条路,路都是双向的。有一些城市有机器人大军,我们想通过城市之间的道路破坏机器人大军之间的联系,并且要用最少的时间。 2 //用kruskal算法利用所有的边,生成多棵最大生成树,每棵树上只有一个城市有机器人大军,剩下的边即是需要被破坏的边。 3 #include 
4 #include
5 #include
6 using namespace std; 7 8 typedef struct Node 9 { 10 int x, y, v; 11 bool operator <(const Node &a) const12 { 13 return v > a.v; 14 } 15 }; 16 17 const int N = 100002; 18 Node edge[N];19 bool b[N]; 20 int f[N]; 21 22 int find(int x)23 { 24 if(f[x] == -1) 25 return x; 26 return f[x] = find(f[x]); 27 } 28 29 void fold(int x, int y)30 { 31 f[find(x)] = find(y); 32 }33 34 long long kruskal(int n)35 { 36 int i,j,k; 37 sort(edge, edge +n); //按权由大到小排序 38 long long sum = 0; 39 memset(f, -1, sizeof(f)); 40 for(i = 0; i < n; i ++)41 { 42 int x = find(edge[i].x); 43 int y = find(edge[i].y); 44 if(b[x] && b[y]) 45 { 46 sum += edge[i].v; 47 continue; 48 } 49 if(b[x] || b[y]) 50 b[x] = b[y] = 1; 51 fold(x,y); 52 } 53 return sum ; 54 } 55 56 int main()57 { 58 int T, i,j,k;59 int n, m, x; 60 cin>>T; 61 while(T--)62 { 63 cin>>n>>m; 64 for(i = 0; i < n-1; i ++) 65 cin>>edge[i].x>>edge[i].y>>edge[i].v; 66 memset(b, 0, sizeof(b)); 67 for(i = 0; i < m; i ++)68 { 69 cin>>x; 70 b[x] = 1; 71 } 72 cout<
<

 

转载地址:http://ourhx.baihongyu.com/

你可能感兴趣的文章
《服务器SSH Public Key认证指南》-补充
查看>>
我的友情链接
查看>>
Java break continue return 的区别
查看>>
算法(Algorithms)第4版 练习 1.3.4
查看>>
jquery easyUI checkbox复选项获取并传后台
查看>>
浅析NopCommerce的多语言方案
查看>>
设计模式之简单工厂模式
查看>>
C++中变量的持续性、链接性和作用域详解
查看>>
2017 4月5日上午
查看>>
Google Chrome开发者工具
查看>>
第一阶段冲刺报告(一)
查看>>
使用crontab调度任务
查看>>
【转载】SQL经验小记
查看>>
zookeeper集群搭建 docker+zk集群搭建
查看>>
Vue2.5笔记:Vue的实例与生命周期
查看>>
论JVM爆炸的几种姿势及自救方法
查看>>
联合体、结构体简析
查看>>
使用throw让服务器端与客户端进行数据交互[Java]
查看>>
java反射与代理
查看>>
深度分析Java的ClassLoader机制(源码级别)
查看>>