hdu 4313 Matrix

Matrix

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


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.
 

 

Source
 

 

Recommend
zhuyuanchen520
题意:给n个顶点,和n-1条边(一颗生成树)。然后给定k个点,表示这些点上有一个机器人。最后让你删去一些边使任意两个机器人都不能互达,且所删边的权值之和要最小。

 

思路:
1、很明显删除k-1条边,可以按类似kruskal最小生成树的做法,不过此处将边按权值从大到小排列,依次把较大的边加进去,如果加入某一条边能够使两个机器人联通,那这条边就是要删除的,不需要再加入, 依次这样循环就可以了。(贪心思想)

AC代码:(sum定义为_int64不能使用long long )

#include <stdio.h>

#include <algorithm>
#include <string.h>
#define maxn 200000
#define ll __int64
using namespace std;
struct node
{
  int x;
  int y;
  int t;
}a[maxn];
int n,m;
int b[maxn];
int set[maxn];
ll sum;
bool cmp( node A,node B)
{
   return A.t>B.t;
}
void make_set( )
{
  for( int i=0;i<=200009;i++)
   set[i]=i;
}
int find( int x)
{
 return x==set[x]?x:set[x]=find(set[x]);
}

ll kruscal( )
{
 sort(a+1,a+n,cmp);
 int i;
 for( i=1;i<=n-1;i++)
 {
   int fx=find(a[i].x),fy=find(a[i].y);
   if(b[fx]&&b[fy])
   {
 
 sum+=a[i].t;
 continue;
   }
   if(b[fx]||b[fy]) b[fx]=b[fy]=1;
   if(fx!=fy) set[fx]=fy;           
  
 }
 return sum;
}
int main( )
{
 int test,i;
 scanf("%d",&test);
 while(test--)
 {
  
   make_set( );
   sum=0;
   scanf("%d%d",&n,&m);
   for( i=0;i<=n;i++)
     b[i]=0;
   for( i=1;i<=n-1;i++)
   {
  scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].t);
   }
   int x;
   for( i=1;i<=m;i++)
   {
 scanf("%d",&x);
 b[x]=1;
   }
   printf("%I64d\n",kruscal( ));
  
  
 }
 return 0;
}

        链接:http://acm.hdu.edu.cn/showproblem.php?pid=4313
posted @ 2012-07-29 11:00  jiai  Views(433)  Comments(0Edit  收藏  举报