zoukankan      html  css  js  c++  java
  • Strategic game(POJ 1463 树形DP)

    Strategic game
    Time Limit: 2000MS   Memory Limit: 10000K
    Total Submissions: 7490   Accepted: 3483

    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

    Source

    一道简单的树形DP,状态转移方程:和上一次做的题目基本一样
      dp[node][0]+=dp[tree[node][i]][1];

      dp[node][1]+=min(dp[tree[node][i]][0],dp[tree[node][i]][1]);  然后+1

    一开始写成 dp[node][1]+=dp[tree[node][i]][0];

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 using namespace std;
     6 #define Max 1506
     7 int dp[Max][2],fa[Max],tree[Max][12];
     8 int n;
     9 int num[Max];
    10 void tree_dp(int node)
    11 {
    12     int i;
    13     for(i=0;i<num[node];i++)
    14     {
    15         tree_dp(tree[node][i]);
    16         dp[node][0]+=dp[tree[node][i]][1];
    17         dp[node][1]+=min(dp[tree[node][i]][0],dp[tree[node][i]][1]);
    18     }
    19     dp[node][1]++;
    20     return;
    21 }
    22 int main()
    23 {
    24     int i,j;
    25     int a,b;
    26     int k,root,f;
    27     freopen("in.txt","r",stdin);
    28     while(scanf("%d",&n)!=EOF)
    29     {
    30         memset(dp,0,sizeof(dp));
    31         memset(tree,0,sizeof(tree));
    32         for(i=0;i<Max;i++)
    33             fa[i]=-1;
    34         for(i=0;i<n;i++)
    35         {
    36             scanf("%d%*c%*c%d%*c",&f,&k);
    37             num[f]=k;
    38             for(j=0;j<k;j++)
    39             {
    40                 scanf("%d",&tree[f][j]);
    41                 fa[tree[f][j]]=f;
    42             }
    43         }
    44         root=0;
    45         while(fa[root]!=-1)
    46             root=fa[root];
    47         tree_dp(root);
    48         printf("%d
    ",min(dp[root][0],dp[root][1]));
    49     }
    50     return 0;
    51 }
  • 相关阅读:
    centos 7 部署confluence
    在Ubuntu操作系统里安装Docker
    Python模块-pip
    微信小程序 等宽不等高瀑布流
    微信小程序 保存图片和复制文字 功能
    C# 模拟鼠标移动和点击
    使用Fiddler抓包工具更改请求的url请求参数
    C#中Request.ServerVariables详细说明及代理
    (C# webservice 获取客户端IP疑难杂症)---试过n种方法都失败,获取代理服务器,访问者客户端真实IP
    C# 命名管道中客户端访问服务器时,出现“对路径的访问被拒绝”
  • 原文地址:https://www.cnblogs.com/a1225234/p/5228449.html
Copyright © 2011-2022 走看看