poj 1463

来源:互联网 发布:data science 知乎 编辑:程序博客网 时间:2024/06/04 00:21

       这是一道动态规划的题目,具体点是一道树形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 LOCALusing 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

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

Sample Output

12

       

0 0
原创粉丝点击