poj 1463

       这是一道动态规划的题目,具体点是一道树形DP,当然也可以用贪心写,不过我只写了动态规划的,没试过贪心。 顺便提一下,这道题其实是求一棵树的最小点覆盖。

       首先,谈谈什么是树形DP,就我个人的理解来说,状态转移方程从子节点向父节点进行状态转移,那就是树形DP。

       然后,定义一下最小点覆盖,这是一个图论的概念。对于图G的任意一条边(u,v),要么u属于顶点覆盖,要么v属于顶点覆盖。也就是说,这个集合中的点要覆盖图中所有的边。在图的所有顶点覆盖中,顶点数量最少的那个称为最小点覆盖。要注意,顶点覆盖强调点覆盖边,不要和支配集混淆。还有一点,求图的最小点覆盖不存在多项式时间算法,但如果图是树,那么便很好解决。

       最后,就是说一下这道题的状态转移方程。主要是两个状态,dp[u][0]代表u节点属于最小点覆盖,dp[u][1]不属于最小点覆盖。dp[u][0]=∑( min( dp[v][1] + dp[v][0] ) ),dp[u][1]=∑( dp[v][0] )(其中,v为u的子节点)。


代码(C++):

#include <cstdlib>
#include <iostream>

#define MAX 1509

//#define LOCAL

using namespace std;

struct edge{
    int to;
    int next;   
};

edge array[MAX];
int head[MAX],dp[MAX][2];

void func(int u)
{
     int i,v;
     dp[u][0]=1;         //u属于最小点覆盖 
     dp[u][1]=0;         //u不属于最小点覆盖 
     for(i=head[u];i!=-1;i=array[i].next)
     {
          v=array[i].to;
          func(v);
          dp[u][0]+=dp[v][0]<dp[v][1]?dp[v][0]:dp[v][1];
          dp[u][1]+=dp[v][0];                               
     }
}

int main(int argc, char *argv[])
{
#ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif

    int n,m,u,v,i,c,r,ans;
    while(scanf("%d",&n)!=EOF)
    {
        memset(head,-1,sizeof(head));  
        c=0;
        r=-1;                    
        for(i=0;i<n;i++)
        {
           scanf("%d:(%d)",&u,&m);
           if(r==-1) r=u;
           while(m--)
           {
              scanf("%d",&v);
              array[c].to=v;
              array[c].next=head[u];
              head[u]=c;
              c++;
           }
        }
        func(r);
        ans=dp[r][0]<dp[r][1]?dp[r][0]:dp[r][1];
        printf("%d\n",ans);
    } 
    system("PAUSE");
    return EXIT_SUCCESS;
}

题目( http://poj.org/problem?id=1463):

Strategic game
Time Limit: 2000MS Memory Limit: 10000K
   

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

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

       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值