博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 1463 Strategic game(树形DP)
阅读量:4035 次
发布时间:2019-05-24

本文共 1997 字,大约阅读时间需要 6 分钟。

1、

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: 10000K
Total 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
#include
using namespace std;#define N 1505vector
vec[N];int f[N];int dp[N][2];void dfs(int root){ for(int i=0; i

 

转载地址:http://hhddi.baihongyu.com/

你可能感兴趣的文章
昨夜今晨最大八卦终于坐实——人类首次直接探测到了引力波
查看>>
如何优雅、机智地和新公司谈薪水?
查看>>
为什么读了很多书,却学不到什么东西?
查看>>
长文干货:如何轻松应对工作中最棘手的13种场景?
查看>>
如何用好碎片化时间,让思维更有效率?
查看>>
No.147 - LeetCode1108
查看>>
No.174 - LeetCode1305 - 合并两个搜索树
查看>>
No.175 - LeetCode1306
查看>>
No.176 - LeetCode1309
查看>>
No.182 - LeetCode1325 - C指针的魅力
查看>>
mysql:sql create database新建utf8mb4 数据库
查看>>
mysql:sql alter database修改数据库字符集
查看>>
mysql:sql drop table (删除表)
查看>>
mysql:sql truncate (清除表数据)
查看>>
scrapy:xpath string(.)非常注意问题
查看>>
yuv to rgb 转换失败呀。天呀。谁来帮帮我呀。
查看>>
yuv420 format
查看>>
YUV420只绘制Y通道
查看>>
yuv420 还原为RGB图像
查看>>
LED恒流驱动芯片
查看>>