zoukankan      html  css  js  c++  java
  • hdu 1054 Strategic Game 经典树形DP

    Strategic Game

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

    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
     
    Source
     
    这一道题和 hdu2412是一个类型的。而且还容易了一些。
     
     
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<stdlib.h>
     4 
     5 struct node
     6 {
     7     int next[1502];
     8     int num;
     9 }f[1502];
    10 int dp[1502][2];
    11 
    12 int Min(int x,int y)
    13 {
    14     return x>y? y:x;
    15 }
    16 
    17 void dfs(int k)
    18 {
    19     int i,j,cur;
    20     if( f[k].num==0 )
    21     {
    22         dp[k][0]=0;
    23         dp[k][1]=1;
    24         return;
    25     }
    26     for(i=1;i<=f[k].num;i++)
    27     {
    28         cur=f[k].next[i];
    29         dfs(cur);
    30         j=Min(dp[cur][0],dp[cur][1]);
    31         dp[k][1]+=j;
    32 
    33         dp[k][0]+=dp[cur][1];
    34     }
    35     dp[k][1]++;
    36 }
    37 
    38 int main()
    39 {
    40     int n,root,r,num,x,len;
    41     int i,j;
    42     while(scanf("%d",&n)>0)
    43     {
    44         getchar();
    45         for(i=1;i<=1500;i++)  f[i].num=0;
    46         for(i=1;i<=n;i++)
    47         {
    48             scanf("%d:(%d)",&r,&num);
    49             if(i==1) root=r;
    50             f[r].num=num;
    51             len=0;
    52             for(j=1;j<=num;j++)
    53             {
    54                 scanf("%d",&x);
    55                 f[r].next[++len]=x;
    56             }
    57         }
    58         memset(dp,0,sizeof(dp));
    59         dfs(root);
    60         printf("%d
    ",Min(dp[root][0],dp[root][1]));
    61     }
    62     return 0;
    63 }
  • 相关阅读:
    转(Java中的递归思想)
    stuff thing object 区别
    论文阅读笔记ECCV 2018: Factorizable net: an efficient subgraphbased framework for scene graph generation
    论文阅读笔记Adversarial Learning with Mask Reconstruction for TextGuidedImage Inpainting
    论文阅读笔记Image Generation from Scene Graphs
    评价gan好坏的指标:IS和FID
    深度学习——正则化(L1\L2)(还没搞明白
    Adam
    L1 L2 SmoothL1损失函数
    ground truth
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3393413.html
Copyright © 2011-2022 走看看