HDU 1054 Strategic Game (树形DP)
来源:互联网 发布:淘宝有人工客服电话嘛 编辑:程序博客网 时间:2024/04/30 08:18
Strategic Game
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8641 Accepted Submission(s): 4161
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:
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
40:(1) 11:(2) 2 32:(0)3:(0)53:(3) 1 4 21:(1) 02:(0)0:(0)4:(0)
Sample Output
12
Source
Southeastern Europe 2000
Recommend
JGShining | We have carefully selected several similar problems for you: 1068 1053 1150 1151 1281
题意是有一棵二叉树,每个节点放置一个兵可以监管到跟它连接的所有节点,问最少需要多少士兵。
最小点覆盖问题,树形DP可以完美的解决这类题,这题可以说是树形DP的模板题,邻接表建图,用dp[i][0]表示在i节点不放兵时的状态,此时它的子节点都要放兵,dp[i][1]表示放兵的状态,此时取值是两个孩子节点中小的那一个。dfs遍历树,从叶子节点到根得到每一次的dp值,最后得到结果。
代码实现:
#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#include<queue>#include<cstdio>#define ll long long#define mset(a,x) memset(a,x,sizeof(a))using namespace std;const double PI=acos(-1);const int inf=0x3f3f3f3f;const double esp=1e-6;const int maxn=1505;const int mod=1e9+7;int dir[4][2]={0,1,1,0,0,-1,-1,0};ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}ll lcm(ll a,ll b){return a/gcd(a,b)*b;}ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}struct edge{int u;int v;int next;}edge[maxn*2];int dp[maxn][2],head[maxn],visit[maxn],cnt;void init(){cnt=0;mset(dp,0);mset(visit,0);mset(head,-1);}void add(int x,int y){edge[cnt].v=y;edge[cnt].next=head[x];head[x]=cnt++;edge[cnt].v=x;edge[cnt].next=head[y];head[y]=cnt++;}void dfs(int s){dp[s][1]=1;visit[s]=1;for(int i=head[s];~i;i=edge[i].next){int x=edge[i].v;if(visit[x])continue;dfs(x);dp[s][1]+=min(dp[x][1],dp[x][0]);dp[s][0]+=dp[x][1];}return ;}int main(){int n,i,j,x,y,z;while(cin>>n){init();int start=0;for(i=0;i<n;i++){scanf("%d:(%d)",&x,&z);for(j=0;j<z;j++){scanf("%d",&y);add(x,y);}if(start==0&&z==0)start=x;}dfs(start);int ans=min(dp[start][0],dp[start][1]);printf("%d\n",ans);}return 0;}
阅读全文
0 0
- hdu 1054 Strategic Game 树形dp
- Strategic Game - HDU 1054 树形dp
- hdu 1054 Strategic Game 树形dp
- hdu 1054 Strategic Game(树形DP)
- HDU 1054 Strategic Game(树形DP)
- hdu - 1054 - Strategic Game(树形dp)
- HDU 1054 Strategic Game (树形DP)
- HDU 1054 Strategic Game (树形DP)
- HDU-1054 Strategic Game(树形DP)
- hdu 1054 Strategic Game 树形DP
- HDU 1054 Strategic Game (树形DP)
- HDU 1054 Strategic Game (树形DP)
- HDU 1054Strategic Game 树形DP
- HDU 1054 Strategic Game (树形DP)
- Strategic Game+hdu+树形dp
- 1054 Strategic Game 树形DP
- HDU 1054 Strategic Game 树形DP/二分图匹配
- hdu 1054 Strategic Game 树形dp基础&&模板
- 类和类之间传递参数
- 企业如何选择仓库管理软件和进销存软件
- 尼克的任务
- android调用webservice复杂数据类型处理
- @rownum
- HDU 1054 Strategic Game (树形DP)
- MyBatis 框架 1
- 【微信开发学习笔记(二)】:上传图片后显示不了
- cors跨域ie9失败
- java基础编程(1):无缓存交换
- QQ第三方友盟分享
- 如何转换 CGCS2000坐标系
- RabbitMQ (三) 发布/订阅
- Java序列化和反序列化