树形DP_______Strategic game( POJ 1463 )

来源:互联网 发布:淘宝女装卫衣 编辑:程序博客网 时间:2024/06/02 03:44

Description

Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a tree. He has to put the minimum number of soldiers on the nodes so that they can observe all the edges. Can you help him? 

Your program should find the minimum number of soldiers that Bob has to put for a given tree. 

For example for the tree: 


the solution is one soldier ( at the node 1).

Input

The input contains several data sets in text format. Each data set represents a tree with the following description: 

  • the number of nodes 
  • the description of each node in the following format 
    node_identifier:(number_of_roads) node_identifier1 node_identifier2 ... node_identifiernumber_of_roads 
    or 
    node_identifier:(0) 

The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500);the number_of_roads in each line of input will no more than 10. Every edge appears only once in the input data.

Output

The output should be printed on the standard output. For each given input data set, print one integer number in a single line that gives the result (the minimum number of soldiers). An example is given in the following:

Sample Input

40:(1) 11:(2) 2 32:(0)3:(0)53:(3) 1 4 21:(1) 02:(0)0:(0)4:(0)

Sample Output

12



题意:

有N个点构成一个树,现在要向N个点中某些点放置守卫,如果某几个点放置守卫那么这个点的所有相连边就被看守住。问要将所有边看住至少需要放置几个守卫?


分析:

树形DP,

dp[ i ][ 1 ] 表示 i 点放置守卫,然后将以 i 为根的子树所有边看住至少需要的守卫数。

dp[ i ][ 0 ] 表示 i 点不放置守卫,然后将以 i 为根的子树所有边看住至少需要的守卫数。


状态转移方程为:

dp[fa][0]+=dp[son][1];//如果该父亲不放,那么儿子必须放
dp[fa][1]+=min(dp[son][0],dp[son][1]);//如果该父亲放,儿子在放和不放之间选择最小的( 这点很重要!! )


代码:

#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<string>#include<map>#include<vector>#include<queue>using namespace std;const int inf = 999999999;int n;vector<int>edge[1510];int fa[1510];int dp[1510][2];void dfs(int now){    dp[now][1] = 1;    dp[now][0] = 0;  //初始化很重要    for(int i = 0 ; i < edge[now].size() ; i ++)        dfs(edge[now][i]);    for(int i = 0 ; i < edge[now].size() ;i ++)    {        dp[now][1] += min(dp[edge[now][i]][0],dp[edge[now][i]][1]);        dp[now][0] += dp[edge[now][i]][1];    }}int main(){    while(scanf("%d",&n)!=EOF)    {        for(int i = 0 ;  i < n ; i ++)            edge[i].clear();        memset(fa,0,sizeof(fa));        for(int i = 0 ;  i < n ; i ++)        {            int now,next,num;            scanf("%d:(%d)",&now,&num);            for(int j = 0 ; j < num ; j ++)            {                scanf("%d",&next);                edge[now].push_back(next);                fa[next] = 1;            }        }        memset(dp,1,sizeof(dp));        int fr = -1;        for(int i = 0 ; i < n ; i ++)            if(!fa[i]) fr = i;        dfs(fr);        int ans = dp[fr][0];        if(dp[fr][1] < ans) ans = dp[fr][1];        printf("%d\n",ans);    }    return 0;}


0 0
原创粉丝点击