zoukankan      html  css  js  c++  java
  • HDU 1054

    Strategic Game

    Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 7477    Accepted Submission(s): 3558


    Problem 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.
      The input file 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_identifier
    or
    node_identifier:(0)

    The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500). Every edge appears only once in the input data.

    For example for the tree:



    the solution is one soldier ( at the node 1).

    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 table:
     
    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[root][1] 表示选择root点时最少用多少, dp[root][0]表示不选择root点时最少用多  少
      状态转移方程为:
      dp[root][0] += dp[son][1];
      dp[root][1] += min(dp[son][0], dp[son][1]);
     
     AC代码:
     1 # include <bits/stdc++.h>
     2 using namespace std;
     3 const int MAX = 1550;
     4 struct Node
     5 {
     6     int to;
     7     int next;
     8 }tree[MAX * 2];
     9 int head[MAX];
    10 int tol = 0;
    11 int dp[MAX][2];
    12 void add(int a, int b)
    13 {
    14     tree[tol].to = b;
    15     tree[tol].next = head[a];
    16     head[a] = tol++;
    17     
    18     tree[tol].to = a;
    19     tree[tol].next = head[b];
    20     head[b] = tol++;
    21 } 
    22 void dfs(int root, int f)
    23 {
    24     for(int i = head[root]; i != -1; i = tree[i].next)
    25     {
    26         int son = tree[i].to;
    27         if(son == f)
    28             continue;
    29         dfs(son, root);
    30         dp[root][0] += dp[son][1];
    31         dp[root][1] += min(dp[son][0], dp[son][1]);
    32     }
    33 }
    34 int main()
    35 {
    36     int n;
    37     while(scanf("%d", &n) != EOF)
    38     {
    39         memset(head, -1, sizeof(head));
    40         memset(dp, 0, sizeof(dp));
    41         tol = 0;
    42         for(int i = 1; i <= n; i++)
    43         {
    44             int a, len;
    45             scanf("%d:(%d)", &a, &len);
    46             for(int j = 0; j < len; j++)
    47             {
    48                 int b;
    49                 scanf("%d", &b);
    50                 add(a + 1, b + 1);
    51             }
    52             dp[i][1] = 1;
    53         }
    54         int root = 1;
    55         dfs(root, -1);
    56         printf("%d
    ", min(dp[root][1], dp[root][0]));
    57     }
    58     return 0;
    59 } 
    View Code
     
     
    生命不息,奋斗不止,这才叫青春,青春就是拥有热情相信未来。
  • 相关阅读:
    eclipse 下载 WindowBuilder
    CLOB、BLOB , CLOB与BLOB的区别
    rpm 安装并配置MySQL(包含指定数据存储路径)
    此Flash Player 与您的地区不相容,请重新安装Adobe Flash Player问题解决
    Eclipse 如何添加 更换字体(转载)
    Eclipse安装WebJavaEE插件、Eclipse编写HTML代码(综合问题统一记录)
    关于hp proliant sl210t服务器raid 1阵列配置(HP P420/Smart Array P420阵列卡配置)
    LINUX下EFIBOOTMGR的使用,删除UEFI主板多余启动项和添加启动项
    安装Linux系统时LSI RAID卡的驱动挂载
    IBM x3250m5安装redhat 6.5 加载raid卡驱动
  • 原文地址:https://www.cnblogs.com/lyf-acm/p/5794838.html
Copyright © 2011-2022 走看看