(最大生成树) hdu 4313

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;
}

  

时间: 2024-08-24 20:50:54

(最大生成树) hdu 4313的相关文章

HDU 4313 Matrix(贪心+并查集)

HDU 4313 题意: 有n个节点,n-1条边,其中k个节点为危险节点,有大规模杀伤性武器,切断哪些路能使得这些大规模杀伤性武器的危险节点之间彼此不连通,且切断的边权值之和最小. 思路: 初始化每个节点为一个集合,并记录每个集合中危险节点的数目(0或1). 要实现权值之和尽可能的小,则要权值尽可能小,故先将n-1条边按权值先升序排序. 排序后枚举这些边: 若边的两端节点所在集合均有大规模杀伤性武器,则删除它并累计其权值. 若只有一边有,则合并这两个集合(用并查集),合并时尽可能将危险节点置于父

!HDU 4313 破坏导弹攻城计划-连通图-(最小生成树变形)

题意:有n个城市,城市之间有n-1条路连接每个城市,也就是说有一个树,敌人在一些城市放了导弹,共有m个导弹,敌人想把导弹运到一起然后开战,现在给你个任务就是摧毁一些路,使任何两个导弹都不能运到一起,摧毁每条路都有相应的代价,求最小的代价. 分析: 导弹不能运到一起就是说任何两个导弹不能在一个连通图中,所以我们的任务就是用最少的代价摧毁一些路使m个导弹分别在m个连通图中. 这题是就反面,最少的代价,那么就是剩下的路的代价越大越好,这就是最大生成树,所以解题方法是:用Kruskal算法求最大生成树,

【树形DP】HDU 4313 Matrix

通道 题意:边权树,有m个点是危险的,现在想将树分成m块使得每块中恰好只有一个危险的点,问最小的花费是多少 思路: dp[i][0|1以i节点为根节点的子树中,i所在的连通块中没有(有)危险节点的最小花费: 如果i是叶子节点:如果i为危险点dp[i][0] = inf,dp[i][1]= 0:否则dp[i][0] = dp[i][1] = 0: 如果i不是叶子节点:如果i是危险点dp[i][0] = inf , dp[i][1] = sigma min(dp[son][0],dp[son][1]

HDU 4313 Matrix(并查集|最小生成树变种)

 题意:给你一个有n(2<=n<=100000)个节点的树,树中每条边都有一个权值.然后再给你k(2<=k<=n)个点,表示这些点上有一个机器人.最后让你删去一些边使任意两个机器人都不能互达,且所删边的权值之和要最小. 思路:不难发现,最后一定要正好去掉k-1条边才能使他们互不连通,对于这k-1条边我们希望他们的权值之和最小, 更进一步,我们希望选取的每条边都尽量小. 正常来想,应该是边权递增排序,每次去掉一条边,看是否不连通的数目加一,但这样做太麻烦. 可以反向来想,把边递减

HDU 4313 Matrix 树形dp

题意: 给定n个点的树,m个黑点 以下n-1行给出边和删除这条边的费用 以下m个黑点的点标[0,n-1] 删除一些边使得随意2个黑点都不连通. 问删除的最小花费. 思路: 树形dp 每一个点有2个状态,成为黑点或白点. 若本身这个点就是黑点那么仅仅有黑点一种状态. 否则能够觉得是子树中某个黑点转移上来. 所以dp[i][0]是i点为黑点的状态. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <st

HDU 4313树形DP

Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3232    Accepted Submission(s): 1283 Problem Description Machines have once again attacked the kingdom of Xions. The kingdom of Xions has N

HDU 4313 并查集

点击打开链接 题意:给一个城镇的图,有m个城镇上是有敌兵的,为了将所有敌兵的联系隔断,需要删除的所有边的最小的权值 思路:看了就知道是并查集Kruskal思想的题,我们将边的权值从大到小向里面加,如果我要加的这条变加进去之后,敌兵可以相连,那么这条边肯定要删下去,而我们从大到小加的边,所以肯定是最小的,因为可以的边我们都用上了嘛 #include <queue> #include <vector> #include <math.h> #include <stdio

hdu4313 贪心并查集 || 树形dp

http://acm.hdu.edu.cn/showproblem.php?pid=4313 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

HDU 4786 Fibonacci Tree(生成树,YY乱搞)

http://acm.hdu.edu.cn/showproblem.php?pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1733    Accepted Submission(s): 543 Problem Description Coach Pang is interested in