Hdu 1054 Strategic Game
来源:互联网 发布:学编程需要学什么 编辑:程序博客网 时间:2024/06/05 05:03
Strategic Game
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3714 Accepted Submission(s): 1630
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
一城堡的所有的道路形成一个n个节点的树,如果在一个节点上放上一个士兵,那么和这个节点相连的边就会被看守住,问把所有边看守住最少需要放多少士兵。
树形DP
d[i][0]代表i结点不放士兵的最小取值
d[i][1]代表i结点放士兵的最小取值
#include<cstdio>#include<cstring>#include<vector>using namespace std;#define Min(a,b) (a<b?a:b)int N;bool vis[1600];vector<int> son[1600];int d[1600][2];void DFS(int x){int v;d[x][0]=0;d[x][1]=1;for(int i=0;i<son[x].size();i++){v=son[x][i];DFS(v);d[x][0]+=d[v][1];d[x][1]+=Min(d[v][0],d[v][1]);}}int main(){int u,v,cnt,i;while(scanf("%d",&N)==1){memset(vis,0,sizeof(vis));for(i=1;i<=N;i++){scanf("%d",&u);scanf("%*c%*c%d%*c",&cnt);son[u].clear();while(cnt--){scanf("%d",&v);son[u].push_back(v);vis[v]=1;}}for(i=0;i<N;i++)if(!vis[i]){DFS(i);break;}printf("%d\n",Min(d[i][0],d[i][1]));/*for(i=0;i<N;i++)printf("%d %d %d\n",i,d[i][0],d[i][1]);*/}return 0;}
- hdu 1054 Strategic Game
- HDU 1054 Strategic Game
- hdu 1054 Strategic Game
- hdu 1054 Strategic Game
- Hdu 1054 Strategic Game
- hdu 1054 Strategic Game
- HDU - 1054 Strategic Game
- hdu 1054 Strategic Game
- hdu-1054-Strategic Game
- HDU 1054 Strategic Game
- HDU 1054 Strategic Game
- HDU 1054 Strategic Game
- HDU 1054:Strategic Game
- HDU 1054 Strategic Game
- HDU 1054 Strategic Game
- HDU 1054Strategic Game 贪心
- hdu/hdoj 1054 Strategic Game
- hdu 1054 Strategic Game (二分匹配)
- OnInitialUpdate函数 UpdateAllViews函数 说明
- 界面闪烁以及图形重绘不彻底的另类原因
- STL vector 容器介绍
- JBoss配置二
- ubuntu下安装openMPI
- Hdu 1054 Strategic Game
- CString.Format的详细用法(转)
- 压缩virtualbox的虚拟硬盘vdi文件
- 拓扑排序算法
- 怎么获得数据库表结构
- 【7gyy】支招:自检性能搞定网速慢电脑卡的问题
- hdu1300 Pearls
- 前端获取时间javascript
- R并行开发准备