(最大產生樹) hdu 4313

來源:互聯網
上載者:User

標籤:

Matrix

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2611    Accepted Submission(s): 978


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. 

 

InputThe 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 

 

OutputFor each test case print the minimum time required to disrupt the connection among Machines. 

 

Sample Input15 32 1 81 0 52 4 51 3 4240 

 

Sample Output10HintNeo 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. 

 

AuthorTJU 將邊權從大到小排序,然後如果某條邊兩遍最多隻有一個機器人,直接減掉。
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<cstdlib>#include<algorithm>using namespace std;int n,k,fa[100010],mark[100010];struct node{    int x,y,z;}e[100010];bool cmp(node a,node b){    return a.z>b.z;}int find(int x){    if(x!=fa[x])        fa[x]=find(fa[x]);    return fa[x];}int main(){    int tt,x;    scanf("%d",&tt);    while(tt--)    {        long long ans=0;        scanf("%d%d",&n,&k);        memset(mark,0,sizeof(mark));        for(int i=0;i<n;i++)            fa[i]=i;        for(int i=0;i<n-1;i++)            scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].z),ans+=e[i].z;        sort(e,e+n-1,cmp);        while(k--)        {            scanf("%d",&x);            mark[x]=1;        }        for(int i=0;i<n-1;i++)        {            int fx,fy;            fx=find(e[i].x);            fy=find(e[i].y);            if(mark[fx]+mark[fy]<=1)            {                if(fx!=fy)                {                    fa[fx]=fy;                    mark[fy]+=mark[fx];                }                ans-=e[i].z;            }        }        printf("%I64d\n",ans);    }    return 0;}

  

(最大產生樹) hdu 4313

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.