POJ 1463 Strategic game 最小点覆盖集(树形dp)
来源:互联网 发布:中银淘宝校园卡注销 编辑:程序博客网 时间:2024/04/30 05:45
点击打开链接
Strategic game
Time Limit: 2000MS Memory Limit: 10000KTotal Submissions: 6105 Accepted: 2808
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
Source
Southeastern Europe 2000
给你一颗树,让你求最小点覆盖。
最小点覆盖:从V中取尽量少的点组成一个集合,使得E中所有边都与取出来的点相连。
dp[i][0]表示点i属于点覆盖,并且以点i为根的子树中所连接的边都被覆盖的情况下点覆盖集中所包含最少点的个数。
dp[i][1]表示点i不属于点覆盖,且以i为根的子树中所连接的边都被覆盖的情况下点覆盖集中所包含最少点的个数。
对于第一种状态dp[i][0],等于每个儿子节点的两种状态的最小值之和+1,方程为:dp[i][0]=1+∑(p[u]=i)min(dp[u][0],dp[u][1]).
对于第二种状态dp[i][1],要求所有与i连接的边都被覆盖,但是i点不属于点覆盖,那么i点所有孩子子节点都必须属于覆盖点,即对于点i的第二种状态与所有子节点的第一种状态有关,在树枝上等于所有子节点的第一种状态的和,方程为:dp[i][1]=∑(p[u]=i)dp[u][0].
//6508K375MS#include<stdio.h>#include<string.h>#include<algorithm>#define M 1507using namespace std;int head[M],nodes;int dp[M][M];struct E{ int v,next;}edge[M*M];void addedge(int u,int v){ edge[nodes].v=v;edge[nodes].next=head[u]; head[u]=nodes++;}void DP(int u,int p){ dp[u][0]=1; dp[u][1]=0; int k,v; for(k=head[u];k!=-1;k=edge[k].next) { v=edge[k].v; if(v==p)continue; DP(v,u); dp[u][0]+=min(dp[v][0],dp[v][1]); dp[u][1]+=dp[v][0]; }}int main(){ int n; while(scanf("%d",&n)!=EOF) { memset(head,-1,sizeof(head)); nodes=0; int u,k,v; for(int i=0;i<n;i++) { scanf("%d:(%d)",&u,&k); while(k--) { scanf("%d",&v); addedge(u,v); addedge(v,u); } } DP(0,0); printf("%d\n",min(dp[0][0],dp[0][1])); } return 0;}
0 0
- poj 1463 Strategic game(最小点覆盖树形DP)
- POJ 1463 Strategic game 最小点覆盖,树形DP
- POJ 1463 Strategic game 最小点覆盖集(树形dp)
- POJ 1463 Strategic game (二分图最小覆盖点(匈牙利算法) 或 树形DP)
- poj 1463 Strategic game 树型dp,最小点集覆盖
- hdu1054 Strategic Game(最小覆盖点-树形dp)
- poj 1463 Strategic game(树形dp,最小边覆盖集)
- POJ 1463 Strategic game (树形dp 树的最小点覆盖 水)
- POJ 1463 Strategic game(树形DP-树上的点集覆盖模型)
- POJ 1463 Strategic game (树形DP,放置点覆盖边)
- Uva 1292 - Strategic game 树形dp 最小点覆盖
- HDU 1054 Strategic Game(二分图最小点覆盖 或者 树形DP)
- poj 1463 Strategic game (树形DP)
- poj 1463 Strategic game 树的最小点覆盖
- UVA1292-----Strategic game-----树形DP解决树上的最小点覆盖问题
- hdu 1054 Strategic Game 树形dp基础题,树的最小点覆盖
- poj 1463 树形dp 最小点覆盖
- Hdu1054 Strategic Game(最小覆盖点集)
- 网络字节序与主机字节序的转换
- 一段小代码
- Nagios之ndoutils-mysql中文乱码[UTF-8]分析分享
- 命令行创建和rest创建
- 机器学习中的有监督学习,无监督学习,半监督学习
- POJ 1463 Strategic game 最小点覆盖集(树形dp)
- 正确使用vs的预编译头
- Ubuntu+Jenkins+GitHub学习搭建持续集成环境
- 机器学习笔记——贝叶斯学习
- LeetCode Longest Palindromic Substring(最长回文子串)
- ios MKNetworkKit 的使用
- tomcat结合nginx使用小结
- 设计模式1——工厂方法
- Windows学习心得【对话框】