码迷,mamicode.com
首页 > 其他好文 > 详细

(最大生成树) hdu 4313

时间:2015-04-14 00:17:23      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

Matrix

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


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.
 

 

Author
TJU
 
将边权从大到小排序,然后如果某条边两遍最多只有一个机器人,直接减掉。
#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

标签:

原文地址:http://www.cnblogs.com/a972290869/p/4423573.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!