• 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;
                }
            }
        }
    } 
  • 相关阅读:
    cdn jsdelivr + github releases 以wordpress sakura主题manifest为例
    c++后台开发面试常见知识点总结(四)数据库
    c++后台开发面试常见知识点总结(三)操作系统
    c++后台开发面试常见知识点总结(二)网络编程
    c++后台开发面试常见知识点总结(一)c++基础
    [LeetCode 206] Reverse Linked List 翻转单链表
    java.util.concurrent.BlockingQueue指南
    如何定义Spring Boot Filter
    Spring Web MVC Framework mvc框架
    springboot profile 日志配置
  • 原文地址:https://www.cnblogs.com/1013star/p/9985817.html
Copyright © 2020-2023  润新知