HDU-1054 Strategic Game(ツリーDP)

6082 ワード

Strategic Game
Time Limit : 20000/10000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 17 Accepted Submission(s) : 11 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem 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.
The input file 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_identifier or node_identifier:(0)
The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500). Every edge appears only once in the input data.
For example for the tree:
the solution is one soldier ( at the node 1).
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 table: 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
経典の简単な直白の木の形のDPテーマ、これは私が书いた最初の木の形のdpテーマで、実は木の形のDP问题、このテーマからその中の规则を探し当てることができて、木の形のDPは木の上でダイナミックな计画を行うことにほかならない.この問題はDFS遍歴の過程で動的計画を行い,状態遷移方程式はdp[root][0]+=dp[k][1];dp[root][1]+=min(dp[root][0],dp[root][1]); 状態遷移方程式は理解に難くなく,問題の意味と結びつけて得ることができる.ここでdfsは状態遷移方程式に合わせて,前述したように,実際にdfsはすべての状況を遍歴し,一般的な動的計画はすべての状況を遍歴してこそ最適解が得られる.dfsは一般的なDPの何層かのforサイクルのようだ.注意して、状态は方程式の位置を移して、もう1つのテーマとこのテーマはあまり悪くなくて、いっしょにすることを提案しますhttp://acm.hdu.edu.cn/showproblem.php?pid=1520 完成してから合計すると、木のDPについて初歩的な理解が必要です.
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <stdlib.h>

using namespace std;
#define MAX 1500
int n;
int root;
int tot;
struct Node
{
    int value;
    int next;
}edge[MAX*2+5];
int head[MAX+5];
int dp[MAX+5][2];
int vis[MAX+5];
void add(int x,int y)
{
    edge[tot].value=y;
    edge[tot].next=head[x];
    head[x]=tot++;
}
void dfs(int root)
{
    dp[root][0]=0;
    dp[root][1]=1;
    vis[root]=1;
    for(int i=head[root];i!=-1;i=edge[i].next)
    {
        int k=edge[i].value;
        if(!vis[k])
        {
            dfs(k);
            dp[root][0]+=dp[k][1];
            dp[root][1]+=min(dp[k][0],dp[k][1]);

        }
    }
}
int main()
{

    int a,b;
    int m;
    while(scanf("%d",&n)!=EOF)
    {
        tot=0;
        memset(vis,0,sizeof(vis));
        memset(head,-1,sizeof(head));
        memset(dp,0,sizeof(dp));
        for(int i=0;i<n;i++)
        {
            scanf("%d:(%d)",&a,&m);
            if(i==0) root=a;
            for(int j=0;j<m;j++)
            {
                scanf("%d",&b);
                add(a,b);
                add(b,a);
            }
        }
        dfs(root);
        printf("%d
"
,min(dp[root][0],dp[root][1])); } return 0; }