POJ 1463 树状dp
2015-03-03 23:43
169 查看
Strategic game
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).
Input
The input 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_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
Sample Output
Source
Southeastern Europe 2000
POJ
1463:
题目意思:
在树的节点上放士兵,使得树的每一条边都有士兵看守,求所需要的最少士兵数;
解题思路:
树状dp,状态转移为:
如果这个点放了士兵,则该点的dp等于所有儿子节点放士兵或者不放士兵的最小值之和;
如果这个点不放士兵,则该点的dp等于所有儿子节点必须放士兵的和;
Time Limit: 2000MS | Memory Limit: 10000K | |
Total Submissions: 6629 | Accepted: 3058 |
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).
Input
The input 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_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
4 0:(1) 1 1:(2) 2 3 2:(0) 3:(0) 5 3:(3) 1 4 2 1:(1) 0 2:(0) 0:(0) 4:(0)
Sample Output
1 2
Source
Southeastern Europe 2000
POJ
1463:
题目意思:
在树的节点上放士兵,使得树的每一条边都有士兵看守,求所需要的最少士兵数;
解题思路:
树状dp,状态转移为:
如果这个点放了士兵,则该点的dp等于所有儿子节点放士兵或者不放士兵的最小值之和;
如果这个点不放士兵,则该点的dp等于所有儿子节点必须放士兵的和;
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cstdio> #include<vector> using namespace std; const int maxn=1507; const int inf=0x3f3f3ff; struct node { int to,next; }; node tree[15007]; vector<int> G[maxn]; int dp[maxn][2]; int n; void init() { for(int i=0;i<maxn;i++) G[i].clear(); memset(tree,0,sizeof(tree)); for(int i=0;i<maxn;i++) { dp[i][0]=dp[i][1]=inf; } } void dfs(int point) { int res1=0,res2=0; for(int i=0;i<G[point].size();i++) { dfs(G[point][i]); res1+=dp[G[point][i]][1]; res2+=min(dp[G[point][i]][1],dp[G[point][i]][0]); } dp[point][0]=res1; dp[point][1]=min(dp[point][1],res2+1); } int main() { //freopen("in.txt","r",stdin); while(~scanf("%d",&n)){ init(); int a,b,c,root=-1; for(int i=1;i<=n;i++) { scanf("%d:(%d)",&a,&b); if(root==-1) root=a; for(int j=0;j<b;j++) { scanf("%d",&c); G[a].push_back(c); } } dfs(root); int ans=min(dp[root][1],dp[root][0]); printf("%d\n",ans); } return 0; }
相关文章推荐
- poj 1463 Strategic game 树状dp
- ACM: 树状DP 动态规划题 poj 1463 …
- poj1155(树状DP)
- poj1848 树状DP
- POJ 2057 The Lost House [树状DP]
- poj 1463 Strategic game(树形DP)
- ACM: 动态规划题 poj 2057 树状DP
- POJ 1463 Strategic game (树形dp 树的最小点覆盖 水)
- POJ1463 Strategic game(树形dp)
- POJ 1463 Strategic game (树形DP) 解题报告
- POJ 1463 Strategic game【树形DP】
- poj 1463 树形dp 最小点覆盖
- POJ 1655 Balancing Act(简单树状DP)
- Poj 1463 Strategic game 【树形DP】
- Strategic game(POJ 1463 树形DP)
- poj 1463 Strategic game(树形dp,最小边覆盖集)
- poj 1463 Strategic game DP
- POJ 1155 TELE 树状DP
- poj 2342 树状DP
- POJ 1463 Strategic game(树形DP-树上的点集覆盖模型)