Strategic GameTime
limit:10000MS
Memory Limit:32768KB
64bit IO Format:%i64d &%i64u SubmitStatus
Description
Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and T Hen 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 in the nodes so, they can observe all the edges. Can you help him?
Your program should find the minimum number of soldiers that Bob have 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 is 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 a soldier (at the Node 1).
The output should is printed on the standard output. For each given input data set, print one, integer number in a, gives the result (the minimum number of sold Iers). An example are 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
Given a map, ask to find the minimum number of people can be the side of the detention center.
Dp[i][0] with I as the root of the subtree, I do not put the person, the minimum number of people needed.
DP[I][1] with I as the root of the subtree, I put the person, the minimum number of people needed.
State transition equation: J is a child of I
Dp[i][0] + = dp[j][1];
DP[I][1] + = max (dp[j][0],dp[j][1]);
#include <cstdio> #include <cstring> #include <algorithm>using namespace std; struct node{int u, v; int next;} Edge[2000];int head[2000], cnt; int dp[2000][2]; void Add (int u,int v) {edge[cnt].u = u; edge[cnt].v = v; Edge[cnt].next = Head[u]; Head[u] = cnt++; return;} void Dfs (int u) {int I, V; Dp[u][0] = 0; DP[U][1] = 1; for (i = head[u]; I! =-1; i = edge[i].next) {v = edge[i].v; DFS (v); Dp[u][0] + = dp[v][1]; Dp[u][1] + = min (dp[v][0],dp[v][1]); } return; int main () {int n, m, I, J, u, V, RT; while (scanf ("%d", &n)! = EOF) {cnt = 0; RT = (n-1) * (n)/2; memset (head,-1,sizeof (head)); Memset (Dp,0,sizeof (DP)); for (i = 0; i < n; i++) {scanf ("%d: (%d)", &u, &m); while (m--) {scanf ("%d", &v); Add (U,V); RT-= V; } } DFS (RT); printf ("%d\n", Min (dp[rt][0],dp[rt][1])); } return 0;}
Poj1463--hdu1054--strategic Game (tree DP Exercise 4)