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

POJ 1463 Strategic game 最小点覆盖集(树形dp)

时间:2014-07-22 23:01:55      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   

Strategic game
Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 6105   Accepted: 2808

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: 
mamicode.com,码迷

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

4
0:(1) 1
1:(2) 2 3
2:(0)
3:(0)
5
3:(3) 1 4 2
1:(1) 0
2:(0)
0:(0)
4:(0)

Sample Output

1
2

Source


给你一颗树,让你求最小点覆盖。
最小点覆盖:从V中取尽量少的点组成一个集合,使得E中所有边都与取出来的点相连。
dp[i][0]表示点i属于点覆盖,并且以点i为根的子树中所连接的边都被覆盖的情况下点覆盖集中所包含最少点的个数。
dp[i][1]表示点i不属于点覆盖,且以i为根的子树中所连接的边都被覆盖的情况下点覆盖集中所包含最少点的个数。
对于第一种状态dp[i][0],等于每个儿子节点的两种状态的最小值之和+1,方程为:dp[i][0]=1+∑(p[u]=i)min(dp[u][0],dp[u][1]).
对于第二种状态dp[i][1],要求所有与i连接的边都被覆盖,但是i点不属于点覆盖,那么i点所有孩子子节点都必须属于覆盖点,即对于点i的第二种状态与所有子节点的第一种状态有关,在树枝上等于所有子节点的第一种状态的和,方程为:dp[i][1]=∑(p[u]=i)dp[u][0].
//6508K	375MS
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define M 1507
using namespace std;
int head[M],nodes;
int dp[M][M];
struct E
{
    int v,next;
}edge[M*M];
void addedge(int u,int v)
{
    edge[nodes].v=v;edge[nodes].next=head[u];
    head[u]=nodes++;
}
void DP(int u,int p)
{
    dp[u][0]=1;
    dp[u][1]=0;
    int k,v;
    for(k=head[u];k!=-1;k=edge[k].next)
    {
        v=edge[k].v;
        if(v==p)continue;
        DP(v,u);
        dp[u][0]+=min(dp[v][0],dp[v][1]);
        dp[u][1]+=dp[v][0];
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        memset(head,-1,sizeof(head));
        nodes=0;
        int u,k,v;
        for(int i=0;i<n;i++)
        {
            scanf("%d:(%d)",&u,&k);
            while(k--)
            {
                scanf("%d",&v);
                addedge(u,v);
                addedge(v,u);
            }
        }
        DP(0,0);
        printf("%d\n",min(dp[0][0],dp[0][1]));
    }
    return 0;
}


POJ 1463 Strategic game 最小点覆盖集(树形dp),码迷,mamicode.com

POJ 1463 Strategic game 最小点覆盖集(树形dp)

标签:des   style   blog   http   color   os   

原文地址:http://blog.csdn.net/crescent__moon/article/details/24735429

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