• Strategic game (树形DP)


    Strategic game

     POJ - 1463

    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
    题意:N个数n-1条边,代表N-1条路,每条路至少需要一个警卫看管,问每条路都至少有一个警卫看管最少需要安排几个警卫。
    思路:
      和POJ - 3342 基本上一模一样。树形DP模板题,dp[x][0]表示在不选x这个节点上不放置警卫时以x为根节点的子树最少需要设置的警卫数。dp[x][1]表示选x这个节点上不放置警卫时以x为根节点的子树最少需要设置的警卫数。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<algorithm>
    using namespace std;
    const int maxn=5000;
    vector<int>v[maxn];
    int n;
    int in[maxn];
    int vis[maxn];
    int dp[maxn][2];
    void dfs(int x)
    {
    
            dp[x][1]=1;
            dp[x][0]=0;
            vis[x]=1;
        for(int i=0;i<v[x].size();i++)
        {
            if(vis[v[x][i]])
                continue;
            int to=v[x][i];
            dfs(to);
            dp[x][1]+=min(dp[to][0],dp[to][1]);
            dp[x][0]+=dp[to][1];
        }
    }
    int main()
    {
        while(~scanf("%d",&n))
        {
            memset(vis,0,sizeof(vis));
            memset(in,0,sizeof(in));
            memset(dp,0,sizeof(dp));
            for(int i=0;i<=n;i++)
                v[i].clear();
            int f,sum,child;
            for(int i=0;i<n;i++)
            {
                scanf("%d:(%d)",&f,&sum);
                while(sum--)
                {
                    scanf("%d",&child);
                    v[f].push_back(child);
                    in[child]++;
                } 
            }
            for(int i=0;i<n;i++)
            {
                if(in[i]==0)
                {
                    dfs(i);
                    printf("%d
    ",min(dp[i][1],dp[i][0]));
                    break;
                }
            }
        }
    } 
  • 相关阅读:
    spring集成quartz
    ScheduledExecutorService
    中国方言输入法Rime入门
    TinyOS实例介绍
    公式输入较好的参考
    USRP IQ信号分析
    法语学习(1)--入门资料推荐
    Python小技巧
    编码知识
    jaspersoft studio colunm header and detail ,detail中显示多列数据
  • 原文地址:https://www.cnblogs.com/1013star/p/9985817.html
Copyright © 2020-2023  润新知