HDU 1054 Strategic Game (树形DP)
来源:互联网 发布:java管理系统 编辑:程序博客网 时间:2024/04/30 14:41
Strategic Game
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7092 Accepted Submission(s): 3359
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
代码:
#include<stdio.h>#include<string.h>#include<vector>#include<algorithm>using namespace std;const int maxn=16000;vector<int>G[maxn];int dp[maxn][2];void DFS(int u,int fa){ dp[u][1]=1; dp[u][0]=0; for(int i=0;i<G[u].size();i++) { int v=G[u][i]; if(v==fa)continue; DFS(v,u); dp[u][1]=dp[u][1]+min(dp[v][1],dp[v][0]); dp[u][0]=dp[u][0]+dp[v][1]; }}int main(){ int n; while(~scanf("%d",&n)) { for(int i=0;i<=maxn;i++)G[i].clear(); for(int i=0;i<=1500;i++) { dp[i][0]=dp[i][1]=0; } int root=-1; for(int i=0;i<n;i++) { int t,k; char a,b,c; scanf("%d",&t); scanf("%c%c%d%c",&a,&b,&k,&c); for(int j=0;j<k;j++) { int x; scanf("%d",&x); G[t].push_back(x); } if(k!=0&&root==-1) // 根一定是第一次出现且k不为0的 t;建的是单向图 root=t; } DFS(root,-1); printf("%d\n",min(dp[root][1],dp[root][0])); }}
#include<stdio.h>#include<string.h>#include<vector>#include<algorithm>using namespace std;const int maxn=16000;vector<int>G[maxn];int dp[maxn][2];void DFS(int u,int fa){ dp[u][1]=1; dp[u][0]=0; for(int i=0;i<G[u].size();i++) { int v=G[u][i]; if(v==fa)continue; DFS(v,u); dp[u][1]=dp[u][1]+min(dp[v][1],dp[v][0]); dp[u][0]=dp[u][0]+dp[v][1]; }}int main(){ int n; while(~scanf("%d",&n)) { for(int i=0;i<=maxn;i++)G[i].clear(); for(int i=0;i<=1500;i++) { dp[i][0]=dp[i][1]=0; } int root=-1; for(int i=0;i<n;i++) { int t,k; char a,b,c; scanf("%d",&t); scanf("%c%c%d%c",&a,&b,&k,&c); for(int j=0;j<k;j++) { int x; scanf("%d",&x); G[t].push_back(x); G[x].push_back(t); } if(k!=0) root=t; } DFS(root,-1); printf("%d\n",min(dp[root][1],dp[root][0])); //printf("%d %d",dp[3][0],dp[3][1]); }}
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基础&&模板
- JBPM的第一个hellow的实例
- HDOJ-2586 How far away ?(在线LCA)
- 那些不能遗忘的知识点回顾——C/C++系列(笔试面试高频题)
- cf 680 c
- Android RxJava 使用RxJava开发
- HDU 1054 Strategic Game (树形DP)
- eclipse误删文件
- 第14周阅读程序(1)
- GitHub
- ORA-01034: ORACLE not available
- hadoop环境搭建
- Kubernetes学习笔记---安装
- IDM必须要放在桌面上么?
- Zephyr 文档教程:目录