zoukankan      html  css  js  c++  java
  • poj 1463 Strategic game 树状dp

    Strategic game

    Time Limit: 2000MS   Memory Limit: 10000K
    Total Submissions: 6607   Accepted: 3047


    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

    还是对树地守卫问题,不过这题是守卫所有的边。节点可以安排士兵,其能守卫相邻的边。求用最少的士兵,守卫所有的边。与皇宫守卫不同。这里只需要看节点是否安排守卫。如果不安排,则其所有子节点均要安排,如果安排守卫,则其子节点状态随意。

    //dp[t][0/1] : 根节点为t的子树(0:根节点不安排守卫1:安排)的所有边被守卫的情况下的最小安排士兵数量。  
      
    dp[t][0] = sum(dp[ti][1]);  
    dp[t][1] = sum(min(dp[ti][0], dp[ti][1])); 
    #include <cstdio>
    #include <vector>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    const int INF = 999999999;
    
    int n;
    std::vector<int > v[2005];
    bool vis[2005];
    int dp[2005][2];
    
    void Tdp(int t) {
        if (v[t].size() == 0) {
            dp[t][0] = 0;
            dp[t][1] = 1;
            return ;
        }
        for (int i=0; i<v[t].size(); i++) {
            Tdp(v[t][i]);
            dp[t][0] += dp[v[t][i]][1];
            dp[t][1] += min(dp[v[t][i]][0], dp[v[t][i]][1]);
        }
        dp[t][1]++;
    }
    
    void init() {
        memset(dp, 0, sizeof(dp));
        memset(vis, false, sizeof(vis));
        for (int i=0; i<1505; i++) {
            v[i].clear();
        }
    }
    
    int main () {
        while (scanf ("%d",&n)!=EOF) {
            init();
            for (int i=0; i<n; i++) {
                int a, b, c;
                scanf ("%d:(%d)", &a, &b);
                for (int j=0; j<b; j++) {
                    scanf ("%d", &c);
                    v[a].push_back(c);
                    vis[c] = true;
                }
            }        int root;
            for (int i=0; i<n; i++) {
                if (!vis[i]) {
                    root = i;
                    break;
                }
            }
            Tdp(root);
            cout << min(dp[root][0], dp[root][1]) << endl;
        }
        return 0;
    }

    <!--话说这输入真他妈恶心-->

     
  • 相关阅读:
    获取地址栏的key-value形式的值(包括重复的key值)形成对象--重复的变成数组形式
    vue---定时循环setInterval
    动画效果----webkit-animation-----动画背景变化transition:background-color 1s linear;
    background-clip--背景是否填充
    box-sizing盒子的大小,修改文字种类而保持字体大小不变font-size-adjust,----块级和内联display---盒子阴影box-shadow---对盒子中容纳不下的内容的显示overflow
    地图api学习第一天
    文字添加阴影
    css循环样式: nth-child(n)
    es6语法
    将中国标准时间--转换为yyyy-MM-dd 时分秒
  • 原文地址:https://www.cnblogs.com/xuelanghu/p/4284354.html
Copyright © 2011-2022 走看看