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): 2925    Accepted Submission(s): 1222


    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
     
    Recommend
    JGShining
     
     
    这题用树形DP,速度很快 154MS.
    相比于二分匹配的做法快很多。
    二分匹配做法见:
     
    /*
    HDU 1054
    
    G++ 312ms 560K
    */
    
    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int MAXN=1510;
    struct Node
    {
        int father,brother,child;
        int yes;//该结点放置
        int no;//该结点不放置
    }t[MAXN];
    void DFS(int x)
    {
        int child=t[x].child;
        while(child)
        {
            DFS(child);
            t[x].yes+=min(t[child].yes,t[child].no);
            //父亲结点放置了,儿子结点可以放置也可以不放置
            t[x].no+=t[child].yes;
            //父亲结点没有放置,儿子结点必须放置
            child=t[child].brother;
        }
    }
    bool used[MAXN];
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        int n;
        int root,k,v;
        while(scanf("%d",&n)!=EOF)
        {
            memset(used,false,sizeof(used));
            int Root;//根结点
            for(int i=0;i<n;i++)
            {
                scanf("%d:(%d)",&root,&k);
                root++;//编号从1开始
                if(i==0)Root=root;
                if(!used[root])
                {
                    used[root]=true;
                    t[root].brother=t[root].father=t[root].child=0;
                    t[root].yes=1;
                    t[root].no=0;
                }
                while(k--)
                {
                    scanf("%d",&v);
                    v++;
                    if(!used[v])
                    {
                        used[v]=true;
                        t[v].brother=t[v].father=t[v].child=0;
                        t[v].yes=1;
                        t[v].no=0;
                    }
                    t[v].brother=t[root].child;
                    t[v].father=root;
                    t[root].child=v;
                }
    
            }
            DFS(Root);
            printf("%d\n",min(t[Root].yes,t[Root].no));
    
        }
        return 0;
    }
  • 相关阅读:
    企业库相关资源下载Enterprise Library Download[2007.5.29更新]
    企业库EntLib初识(3)配置管理应用程序块ConfigurationApplicationBlock
    我喜欢博客园
    今天是个值得纪念的日子
    Three collaboration teamwork Tools
    How to do ‘undo’ in TortoiseSVN
    继承windows service的安装类出现的多余服务被安装的问题
    List Sort 使用的注意几点
    下面两段代码,你认为那一段写的更好?
    asp.net 2.0教程 c#中的接口、抽象、委托
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2646836.html
Copyright © 2011-2022 走看看