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

HDU 4313树形DP

时间:2017-04-17 09:36:40      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:strong   for   type   rom   ext   rup   panel   printf   from   

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

 

Source
 

 题意:

有n个点,n-1条边的树,边有权值,有k个坏点,要求去掉一些边让所有的坏点不相互联通,问去掉的最小边权值。

代码:

//两个相邻的点都是坏点时必须去掉他们之间的边,儿子是坏点父亲不是坏点时
//可以至少保留1个儿子坏点去掉其他的兄弟坏点的边,我们必然会保留边权值大的
//那个儿子,然后向他的祖先传递坏点信息并更新dp(隔离那个坏点的最小边权)。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int inf=0x7fffffff;
int t,n,k,dp[100009],f[100009],ma[100009],head[100009],tol;
ll ans;
struct node{
    int v,w,next;
}nodes[200009];
void Add(int x,int y,int z){
    nodes[tol].v=y;
    nodes[tol].w=z;
    nodes[tol].next=head[x];
    head[x]=tol++;
}
void dfs(int u,int fa){
    dp[u]=inf;
    ma[u]=f[u];
    ll tmp=0;
    for(int i=head[u];i!=-1;i=nodes[i].next){
        int v=nodes[i].v;
        if(v==fa) continue;
        dfs(v,u);
        if(!ma[v]) continue;
        if(f[u]) ans+=min(nodes[i].w,dp[v]);
        else{
            ans+=min(tmp,1LL*min(dp[v],nodes[i].w));
            tmp=max(tmp,1LL*min(dp[v],nodes[i].w));
        }
    }
    if(tmp!=0){
        dp[u]=tmp;
        ma[u]=1;
    }
}
int main()
{
    scanf("%d",&t);
    while(t--){
        tol=0;
        memset(head,-1,sizeof(head));
        scanf("%d%d",&n,&k);
        int x,y,z;
        for(int i=1;i<n;i++){
            scanf("%d%d%d",&x,&y,&z);
            Add(x,y,z);
            Add(y,x,z);
        }
        memset(f,0,sizeof(f));
        for(int i=0;i<k;i++){
            scanf("%d",&x);
            f[x]=1;
        }
        memset(ma,0,sizeof(ma));
        ans=0;
        dfs(0,-1);
        printf("%I64d\n",ans);
    }
    return 0;
}

 

HDU 4313树形DP

标签:strong   for   type   rom   ext   rup   panel   printf   from   

原文地址:http://www.cnblogs.com/--ZHIYUAN/p/6721194.html

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