poj 1463 Strategic game(树dp)
来源:互联网 发布:金十网数据官网 编辑:程序博客网 时间:2024/06/05 15:25
Strategic game
Time Limit: 2000MS Memory Limit: 10000KTotal Submissions: 5605 Accepted: 2551
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).
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 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.
- 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
题意:给出一棵树,在某个节点上放置一个卫兵可以管辖周围所有与该节点相连的边,问最少需要放置多少个卫兵,能管辖所有的边。
思路:设dp[i][0],表示在结点 i 没放置士兵的情况下,看住以结点 i 为根的子树的所有边所需的最少士兵;设dp[i][1],表示在结点 i 放置士兵的情况下,看住以结点 i 为根的子树的所有边所需的最少士兵。
状态转移方程: dp[i][0]=dp[i][0]+∑dp[j][1],j 是 i 的儿子结点;(根结点不放士兵时,与其相连的边必须由儿子结点来看守)
dp[i][1]=dp[i][1]+∑( MIN ( dp[j][0] , dp[j][1] ) ),j 是 i 的儿子结点。 (根结点放士兵时,儿子结点可放可不放)
AC代码:
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <queue>#include <vector>#include <cmath>#include <cstdlib>#define L(rt) (rt<<1)#define R(rt) (rt<<1|1)#define ll long longusing namespace std;const int maxn=1505;struct node{ int v,next;}edge[maxn*2];int head[maxn],dp[maxn][2];bool vis[maxn];int n,num;void init(){ memset(head,-1,sizeof(head)); num=0;}void add(int u,int v){ edge[num].v=v; edge[num].next=head[u]; head[u]=num++;}void input(){ int u,v,k; for(int i=0;i<n;i++) { scanf("%d:(%d)",&u,&k); while(k--) { scanf("%d",&v); add(u,v); add(v,u); } }}void dfs(int u){ vis[u]=true; dp[u][0]=0; dp[u][1]=1; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(!vis[v]) { dfs(v); dp[u][0]+=dp[v][1]; dp[u][1]+=min(dp[v][1],dp[v][0]); } }}void solve(){ memset(vis,false,sizeof(vis)); dfs(0); printf("%d\n",min(dp[0][0],dp[0][1]));}int main(){ while(~scanf("%d",&n)) { init(); input(); solve(); } return 0;}
- poj 1463 Strategic game(树dp)
- poj 1463 Strategic game (树形DP)
- pku 1463 Strategic game(树dp)
- poj 1463 Strategic game(最小点覆盖树形DP)
- POJ 1463 && HDU 1054 Strategic Game(树形DP)
- 【POJ 1463】【hdu 1054】Strategic game(树形dp)
- poj 1463 Strategic game (树形dp 守卫)
- 树形DP专辑--POJ 1463 Strategic game
- poj 1463 Strategic game(树形DP)
- POJ 1463 Strategic game( 树形DP )
- POJ 1463-Strategic game(树形DP)
- poj 1463 Strategic game 树状dp
- POJ - 1463Strategic game入门树形DP
- poj 1463 Strategic game 树形dp
- poj 1463Strategic game【树形dp】
- poj 1463 Strategic game(树形dp)
- poj 1463 Strategic game 树形dp
- poj 1463 Strategic game 树形dp
- 使用RMAN迁移数据库到异机
- 【Oracle】-【TWO_TASK】-TWO_TASK环境变量的作用
- spring ioc原理(看完后大家可以自己写一个spring)
- hdu 1284 钱币兑换问题
- 关于windows程序设计 列表框类 ——environ
- poj 1463 Strategic game(树dp)
- Find m combinations from a N set
- Spring+Hibernate 异常No Hibernate Session bound to thread, and configuration does not allow creation
- 【android】scaleType属性与ImagView中图片的显示的关系 颜色
- timus 1319. Hotel URAL 解题报告
- MFC中绘图
- php之pdo扩展库
- 1046. Shortest Distance (20)—PAT
- 【每天一个Linux命令】08. Linux中time命令的用法