poj 1463 Strategic game(树形DP)

来源:互联网 发布:晨曦预算软件视频教程 编辑:程序博客网 时间:2024/04/30 03:01

1、http://poj.org/problem?id=1463

2、题目大意:

给定一棵树,有n个结点,现在要在每个结点上布兵,使得这些士兵可以监视到所有的路线,问最少放多少士兵?

若果父节点放士兵,那么孩子结点可以放,可以不放,如果父节点不放士兵,那么孩子结点必须放士兵。

int dp[i][0]表示在i结点不放置士兵,初始值为0

int dp[i][1]表示在i结点放置士兵,那么初始值就是1

状态转移方程

for(int i=0; i<vec[root].size(); i++)
    {
        int v=vec[root][i];
        dfs(v);
        dp[root][0]+=dp[v][1];
        dp[root][1]+=min(dp[v][0],dp[v][1]);
        //printf("%d %d %d\n",root,dp[root][0],dp[root][1]);
    }

3、题目:

Strategic game
Time Limit: 2000MS Memory Limit: 10000KTotal Submissions: 5830 Accepted: 2657

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

 

4、AC代码:

#include<stdio.h>#include<vector>using namespace std;#define N 1505vector<int> vec[N];int f[N];int dp[N][2];void dfs(int root){    for(int i=0; i<vec[root].size(); i++)    {        int v=vec[root][i];        dfs(v);        dp[root][0]+=dp[v][1];        dp[root][1]+=min(dp[v][0],dp[v][1]);        //printf("%d %d %d\n",root,dp[root][0],dp[root][1]);    }}int main(){    int n,a,m,b;    while(scanf("%d",&n)!=EOF)    {        for(int i=0;i<n;i++)        {            vec[i].clear();            f[i]=-1;            dp[i][0]=0;            dp[i][1]=1;        }        for(int i=0; i<n; i++)        {            scanf("%d:(%d)",&a,&m);            for(int i=1; i<=m; i++)            {                scanf("%d",&b);                vec[a].push_back(b);                f[b]=a;            }        }        int a=0;        while(f[a]!=-1)        {            a=f[a];        }        dfs(a);        printf("%d\n",min(dp[a][0],dp[a][1]));    }    return 0;}


 

原创粉丝点击