hdoj 1054 Strategic Game

动态规划+深搜

/*
HDOJ: 1054 Strategic Game
*/
#include <iostream>
#include <cstdio>
#include <cstring>

#define MaxN 1505

using namespace std;

struct Node {
    int no;
    int sons;
    int son_no[MaxN];
    bool isRoot;
}node[MaxN];
int dp[MaxN][2];
int n;

int solve(int i, int flag)
{
    if(dp[i][flag] != -1)
        return dp[i][flag];
    
    int sum = flag;
    for(int j = 0; j < node[i].sons; j++) {
        if(flag == 0) {
            sum += solve(node[i].son_no[j], 1);
        }
        else {
            sum += min(solve(node[i].son_no[j], 0), solve(node[i].son_no[j], 1));
        }
    }   
    dp[i][flag] = sum;
    
    return sum;
}

int main()
{
    //freopen("data.in", "rb", stdin);
    while(scanf("%d", &n) != EOF) {
        for(int i = 0; i < n; i++) {
            node[i].no = i;
            node[i].isRoot = true;
            node[i].sons = 0;
            dp[i][0] = dp[i][1] = -1;
        }
        for(int i = 0; i < n; i++) {
            int a, b;
            scanf("%d:(%d)", &a, &b);
            node[a].sons = b;
            for(int j = 0; j < b; j++) {
                int tmp;
                scanf("%d", &tmp);
                node[a].son_no[j] = tmp;
                node[tmp].isRoot = false;
            }
        }
        
        int i = 0;
        while(!node[i].isRoot) 
            i++;
        
        printf("%d\n", min(solve(i, 0), solve(i, 1)));
    }
    
    return 0;
}

你可能感兴趣的:(hdoj 1054 Strategic Game)