zoukankan      html  css  js  c++  java
  • 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 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 }
  • 相关阅读:
    element表单中一个elformitem下多个formitem项校验(循环校验)
    vscode配置
    git push时提示错误 sign_and_send_pubkey: no mutual signature supported
    syncthing文件同步网盘配置
    MQTT服务搭建和简单使用
    python脚本避免被多次执行
    Hadoop集群对datanode宕机后的处理机制源码阅读
    工作冲突和低谷
    职场的帮助
    测试总体思想
  • 原文地址:https://www.cnblogs.com/hxsyl/p/2672448.html
Copyright © 2011-2022 走看看