(贪心)kruskal思想

时间:2023-03-08 23:25:50
(贪心)kruskal思想

hdu4313

Matrix

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2204    Accepted Submission(s): 824
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

题意:有一颗树形图王国,在有些城市会放置一种随时会爆炸的炸弹,为了使放置炸弹的这些城市两两互不连通,需要破坏掉一些道路,破坏每条道路都需要花费一定的时间,问至少花费多少时间可以完成任务;

分析:贪心算法:类似kruskal最小生成树的过程,不过此处将边按权值从大到小排列,每次将边加进来时要判断是否会使两个危险的点连通,是的话这条边就是需要被删除的,否则将它加到树上。

程序:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include"stdio.h"
#include"string.h"
#include"iostream"
#include"map"
#include"string"
#include"queue"
#include"stdlib.h"
#include"math.h"
#include"algorithm"
#include"vector"
#define M 100009
#define eps 1e-10
#define inf 1000000000
#define mod 1000000000
#define INF 1000000000
using namespace std;
struct node
{
int u,v,w;
}e[M];
int cmp(node a,node b)
{
return a.w>b.w;
}
int f[M],use[M];
int finde(int x)
{
if(x!=f[x])
f[x]=finde(f[x]);
return f[x];
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,k,a,i;
scanf("%d%d",&n,&k);
for(i=0;i<n;i++)
f[i]=i;
for(i=0;i<n-1;i++)
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
memset(use,0,sizeof(use));
for(i=1;i<=k;i++)
{
scanf("%d",&a);
use[a]=1;
}
sort(e,e+n-1,cmp);
__int64 ans=0;
for(i=0;i<n-1;i++)//加入并查集的时候最好把每个集合的的根节点指向有标记的点,方便比较
{
if(use[e[i].u]&&use[e[i].v])
ans+=e[i].w;
else if(use[e[i].u]&&!use[e[i].v])
{
int x=finde(e[i].v);
if(use[x])
ans+=e[i].w;
else
f[x]=finde(e[i].u); }
else if(!use[e[i].u]&&use[e[i].v])
{
int x=finde(e[i].u);
if(use[x])
ans+=e[i].w;
else
f[x]=finde(e[i].v);
}
else
{
int x=finde(e[i].u);
int y=finde(e[i].v);
if(use[x]&&use[y])
ans+=e[i].w;
else if(use[x]&&!use[y])
f[y]=x;
else
f[x]=y;
}
}
printf("%I64d\n",ans);
}
return 0;
}