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:
 

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;}


原创粉丝点击