hdu4313 貪心並查集 || 樹形dp

life4711發表於2015-01-25

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 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.
 
/**
hdu 4313   並查集+貪心
題目大意:
         給定n(n<=100000)個節點的樹,每條邊有一定的權值,有m個點是危險的,現在想將樹分成m塊使得每塊中恰好只有一個危險的點,問最小的花費是多少。
解題思路:
         類似Kruskal的貪心過程,將節點按照從大到小排序,以每個危險的節點為並查集的根節點

*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;

const int maxn=100005;

struct note
{
    int u,v;
    LL w;
    bool operator < (const note &other) const
    {
        return w>other.w;
    }
} edge[maxn];

int father[maxn];
bool flag[maxn];
int n,m;

void init()
{
    for(int i=0; i<=n; i++)
    {
        father[i]=i;
    }
}

int find(int u)
{
    if(u==father[u])
        return u;
    return father[u]=find(father[u]);
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0; i<n-1; i++)
        {
            int u,v;
            LL w;
            scanf("%d%d%I64d",&edge[i].u,&edge[i].v,&edge[i].w);
        }
        memset(flag,false,sizeof(flag));
        for(int i=0; i<m; i++)
        {
            int u;
            scanf("%d",&u);
            flag[u]=true;
        }
        init();
        LL sum=0;
        sort(edge,edge+n-1);
        for(int i=0; i<n-1; i++)
        {
            int x=find(edge[i].u);
            int y=find(edge[i].v);
            if(flag[x]&&flag[y])
            {
                sum+=edge[i].w;
            }
            else if(flag[x])
            {
                father[y]=x;
            }
            else
            {
                father[x]=y;
            }
        }
        printf("%I64d\n",sum);
    }
    return 0;
}


/**
hdu 4313  樹形dp
題目大意:
         給定n(n<=100000)個節點的樹,每條邊有一定的權值,有m個點是危險的,現在想將樹分成m塊使得每塊中恰好只有一個危險的點,問最小的花費是多少。
解題思路:
         dp[i][0]代表的是在當前以i節點為根節點的子樹中,i所在的連通塊中沒有危險節點的最小花費;
         dp[i][1]代表的是在當前以i節點為根節點的子樹中,i所在的連通塊中有危險節點的最小花費;
         如果i是葉子節點:如果i為危險點dp[i][0] = inf,dp[i][1]= 0;否則dp[i][0] = 0,dp[i][1] = inf;
         如果i不是葉子節點:如果i是危險點dp[i][0] = inf , dp[i][1] = sigma min(dp[son][0],dp[son][1]+w);
         否則dp[i][0] = sigma min(dp[son][0],dp[son][1]+w),dp[i][1] = min(dp[i][0] – min(dp[son][0],dp[son][1]+w)+dp[son][1])。

*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;

const int maxn=100005;
const LL inf=0x3fffffffffffffffLL;

int head[maxn],ip;
LL dp[maxn][2];
int n,m;
bool flag[maxn];

struct note
{
    int v,next;
    LL w;
} edge[maxn*2];

void init()
{
    memset(head,-1,sizeof(head));
    ip=0;
}

void addedge(int u,int v,LL w)
{
    edge[ip].v=v,edge[ip].w=w,edge[ip].next=head[u],head[u]=ip++;
}

void dfs(int u,int pre)
{
  ///  printf("%d->%d\n",pre,u);
    if(flag[u])
    {
        dp[u][0]=inf;
        dp[u][1]=0;
        for(int i=head[u]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].v;
            LL w=edge[i].w;
            if(v==pre)continue;
            dfs(v,u);
            dp[u][1]+=min(dp[v][0],dp[v][1]+w);
        }
        return;
    }
    else
    {
        dp[u][0]=0;
        dp[u][1]=inf;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            LL w=edge[i].w;
            if(v==pre) continue;
            dfs(v,u);
            dp[u][0]+=min(dp[v][0],dp[v][1]+w);
        }
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            LL w=edge[i].w;
            if(v==pre)continue;
            dp[u][1]=min(dp[u][1],dp[u][0]-min(dp[v][0],dp[v][1]+w)+dp[v][1]);
        }
        return;
    }
}
/**void dfs1(int u,int pre)
{
    printf("%d->%d\n",pre,u);
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==pre)continue;
        dfs1(v,u);
    }
}*/
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        init();
        for(int i=1; i<n; i++)
        {
            int u,v;
            LL w;
            scanf("%d%d%I64d",&u,&v,&w);
            u++;
            v++;
            addedge(u,v,w);
            addedge(v,u,w);
          //  printf("%d-->%d\n",u,v);
           // printf("%d-->%d\n",v,u);

        }
        memset(flag,false,sizeof(flag));
        for(int i=0; i<m; i++)
        {
            int x;
            scanf("%d",&x);
            x++;
            flag[x]=true;
        }
        dfs(1,-1);
        //dfs1(1,-1);
        /**for(int i=1;i<=n;i++)
        {
            printf("%d %I64d %I64d\n",i,dp[i][0],dp[i][1]);
        }*/
        printf("%I64d\n",min(dp[1][0],dp[1][1]));
    }
    return 0;
}



相關文章