HDU4313Matrix (use the minimal spanning tree idea to divide sets)

Source: Internet
Author: User

HDU4313Matrix (use the minimal spanning tree idea to divide sets)
MatrixTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 2598 Accepted Submission (s): 973



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
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 shoshould 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 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

15 32 1 81 0 52 4 51 3 4240

Sample Output
10HintNeo 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 atime, 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. 
Resolution: We need to separate the k points given. Because there is only one connection between any two sets, we regard these k points as the root node of k sets, then, when we connect the edges of these k sets, their weights add up to the minimum sum we need. We sort the edge in order from large to small, and then check whether this edge can connect two similar nodes. If yes, this edge is the edge to be selected.
#include
 
  #includeusing namespace std;#define LL __int64const int N = 100005;struct EDG{    int u,v;    LL c;};int fath[N],typeNode[N],n;EDG edg[N];void init(){    for(int i=0;i<=n;i++)    {        fath[i]=i; typeNode[i]=0;    }}int findfath(int x){    if(x!=fath[x])    fath[x]=findfath(fath[x]);    return fath[x];}LL setfath(int i){    int a=findfath(edg[i].u);    int b=findfath(edg[i].v);    if(a==b)    return 0;    if(typeNode[a]&&typeNode[b])    return edg[i].c;    if(typeNode[a])    fath[b]=a;    else    fath[a]=b;    return 0;}int cmp(EDG a,EDG b){    return a.c>b.c;}int main(){    int t,k,a;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&k);        init();        for(int i=0;i
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.