POJ 1463 Strategic game

题目:
Strategic game
Time Limit: 2000MS Memory Limit: 10000K
Total Submissions: 8051 Accepted: 3746
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

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


一个动规题,以前做的时候用的是多叉树转二叉树,写起来是挺烦的。
现在直接用多叉树写,还是很简单的树形dp。
dp[x][z]
z=0,x点的父亲节点没有守卫时,x子树需要最少的守卫数
z=1,x点的父亲节点有守卫时,x子树需要的最少守卫数
状态转移很简单,直接看代码吧。

这里写图片描述

#include 
#include 
#include 
#include 

#define N 1555
#define M 2222

using namespace std;

int head[N], to[M], nxt[M];
int cnt, n;
int du[N], dp[N][2];

void add(int u, int v) {
    to[cnt] = v; nxt[cnt] = head[u]; head[u] = cnt++;
}

void read() {
    memset(du, 0, sizeof du);
    memset(head, -1, sizeof head);
    memset(nxt, -1, sizeof nxt);
    cnt = 0;
    for (int i = 0, a, b, c; i < n; i++) {
        scanf("%d:(%d)", &a, &b);
        for (int j = 0; j < b; j++) {
            scanf("%d", &c);
            add(a, c);
            du[c]++;
        }
    }
}

int dfs(int x, int z) {
    if (dp[x][z] < 0x3f3f3f3f) return dp[x][z];
    for (int i = head[x]; ~i; i = nxt[i]) {
        dfs(to[i], 0);
        dfs(to[i], 1);
    }
    if (z == 0) {
        dp[x][z] = 1;
        for (int i = head[x]; ~i; i = nxt[i]) {
            dp[x][z] += dp[to[i]][1];
        }
    }
    else {
        dp[x][z] = 0;
        for (int i = head[x]; ~i; i = nxt[i]) {
            dp[x][z] += dp[to[i]][0];
        }
        int rs = 1;
        for (int i = head[x]; ~i; i = nxt[i]) {
            rs += dp[to[i]][1];
        }
        dp[x][z] = min(dp[x][z], rs);
    }
    return dp[x][z];
}

void solve() {
    int root;
    for (int i = 0; i < n; i++) {
        if (du[i] == 0) root = i;
    }
    memset(dp, 0x3f, sizeof dp);
    cout << dfs(root, 1) << endl;
}

int main() {
    while (cin >> n) {
        read();
        solve();
    }
    return 0;
} 

你可能感兴趣的:(poj,dp,算法,poj,dp)