poj 1463 Strategic game 树状dp
来源:互联网 发布:如何学习linux命令 编辑:程序博客网 时间:2024/05/21 08:47
Strategic game
Time Limit: 2000MS Memory Limit: 10000KTotal Submissions: 6607 Accepted: 3047
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
还是对树地守卫问题,不过这题是守卫所有的边。节点可以安排士兵,其能守卫相邻的边。求用最少的士兵,守卫所有的边。与皇宫守卫不同。这里只需要看节点是否安排守卫。如果不安排,则其所有子节点均要安排,如果安排守卫,则其子节点状态随意。
//dp[t][0/1] : 根节点为t的子树(0:根节点不安排守卫1:安排)的所有边被守卫的情况下的最小安排士兵数量。dp[t][0] = sum(dp[ti][1]);dp[t][1] = sum(min(dp[ti][0], dp[ti][1]));
#include <cstdio>#include <vector>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int INF = 999999999;int n;std::vector<int > v[2005];bool vis[2005];int dp[2005][2];void Tdp(int t) {if (v[t].size() == 0) {dp[t][0] = 0;dp[t][1] = 1;return ;}for (int i=0; i<v[t].size(); i++) {Tdp(v[t][i]);dp[t][0] += dp[v[t][i]][1];dp[t][1] += min(dp[v[t][i]][0], dp[v[t][i]][1]);}dp[t][1]++;}void init() {memset(dp, 0, sizeof(dp));memset(vis, false, sizeof(vis));for (int i=0; i<1505; i++) {v[i].clear();}}int main () {while (scanf ("%d",&n)!=EOF) {init();for (int i=0; i<n; i++) {int a, b, c;scanf ("%d:(%d)", &a, &b);for (int j=0; j<b; j++) {scanf ("%d", &c);v[a].push_back(c);vis[c] = true;}}int root;for (int i=0; i<n; i++) {if (!vis[i]) {root = i;break;}}Tdp(root);cout << min(dp[root][0], dp[root][1]) << endl;}return 0;}<!--话说这输入真他妈恶心-->
0 0
- poj 1463 Strategic game 树状dp
- poj 1463 Strategic game(树状DP…
- poj1463 Strategic game (树状dp)
- poj 1463 Strategic game(树dp)
- 树形DP专辑--POJ 1463 Strategic game
- poj 1463 Strategic game(树形DP)
- POJ 1463 Strategic game( 树形DP )
- POJ 1463-Strategic game(树形DP)
- POJ - 1463Strategic game入门树形DP
- poj 1463 Strategic game 树形dp
- poj 1463Strategic game【树形dp】
- poj 1463 Strategic game(树形dp)
- poj 1463 Strategic game 树形dp
- poj 1463 Strategic game (树形DP)
- poj 1463 Strategic game 树形dp
- POJ-1463-Strategic game
- POJ 1463 Strategic game
- POJ 1463 Strategic game
- 怎样将word转换成pdf文件的步骤
- 我的第一篇CSDN文章
- 网页自适应+viewport介绍
- 第五题:Longest Palindromic Substring
- oracle权限问题
- poj 1463 Strategic game 树状dp
- 打开文件夹对话框用法
- codeforces 507C
- socket send 异常
- java之File对象对文件的操作常用的几个方法
- 在Tomcat上安装部署SAIKU
- iOS 图片显示原理
- 剖析 Linux hypervisor
- Codeforces 500C New Year Book Reading