HDU 4313

简介: Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1390 Accepted Submission(s): 507 Problem...

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 <iostream> 
 4 #include <cstring> 
 5 #include <algorithm>   
 6 using namespace std;  
 7  
 8 typedef struct Node
 9 {     
10      int x, y, v;     
11      bool operator <(const Node &a) const
12      {         
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<<kruskal(n-1)<<endl;     
73      }
74      return 0; 
75 }

 

目录
相关文章
|
机器学习/深度学习 Java 算法
|
测试技术 Java
|
人工智能 Java
HDU 1257
最少拦截系统 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4182    Accepted Submission(s): 1528 ...
761 0
|
Java 测试技术
HDU 1847
Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3204    Accepted Submission(s): 2008 Problem Description 大学英语四级考试就要来临了,你是不是在紧张的复习?也许紧张得连短学期的ACM都没工夫练习了,反正我知道的Kiki和Cici都是如此。
833 0
|
机器学习/深度学习 人工智能
HDU 2674
  题意:求N!mod2009,N=41时,N!因式分解一定含7*7*41,即N!%2009=0.所以只要计算0
692 0
|
Java
HDU 1846(巴什博弈)
Brave Game Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4050    Accepted Submission(s): 2644 Problem Description 十年前读大学的时候,中国每年都要从国外引进一些电影大片,其中有一部电影就叫《勇敢者的游戏》(英文名称:Zathura),一直到现在,我依然对于电影中的部分电脑特技印象深刻。
770 0