• HDU2260 Accepted Necklace DFS


    Accepted Necklace

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1027    Accepted Submission(s): 407


    Problem Description

    I have N precious stones, and plan to use K of them to make a necklace for my mother, but she won't accept a necklace which is too heavy. Given the value and the weight of each precious stone, please help me find out the most valuable necklace my mother will accept.
     

    Input

    The first line of input is the number of cases.
    For each case, the first line contains two integers N (N <= 20), the total number of stones, and K (K <= N), the exact number of stones to make a necklace.
    Then N lines follow, each containing two integers: a (a<=1000), representing the value of each precious stone, and b (b<=1000), its weight.
    The last line of each case contains an integer W, the maximum weight my mother will accept, W <= 1000.
     

    Output

    For each case, output the highest possible value of the necklace.
     

    Sample Input
    1
    2 1
    1 1
    1 1
    3
     

    Sample Output
    1
     
      该题就是利用DFS产生随机的组合然后求解最大值。代码写的实在不咋地,DFS的变化很多,非常灵活,是递归的一种最完美的实现。DFS函数每次传进去四个参数,放第几个物体,所剩空间,所剩次数,以及临时和,对于每一个状态保留递归后的最大值,每次从放物品的后一个物品选起。
      代码如下:
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    
    int hash[25], rec[1005][25], N, M, W;
    
    struct Node
    {
        int p, w;
    }e[25];
    
    void DFS( int pos, int lw, int lm, int sum )
    {
        int flag= 0;
        for( int i= pos+ 1; i<= N; ++i )
        {
            if( !hash[i]&& lm>= 1&& lw>= e[i].w )
            {
                flag= 1;
                hash[i]= 1;
                DFS( i, lw- e[i].w, lm- 1, sum+ e[i].p );
                hash[i]= 0;
                rec[lw][lm]= max( rec[lw][lm], rec[lw- e[i].w][lm- 1] );
            }
        }
        if( !flag )
        {
            rec[lw][lm]= sum;
        }
    }
    
    int main()
    {
        int T;
        scanf( "%d", &T );
        while( T-- )
        {
            memset( hash, 0, sizeof( hash ) );
            memset( rec, 0, sizeof( rec ) );
            scanf( "%d %d", &N, &M );
            for( int i= 1; i<= N; ++i )
            {
                scanf( "%d %d", &e[i].p, &e[i].w );
            }
            scanf( "%d", &W );
            for( int i= 1; i<= N; ++i )
            {
                if( !hash[i]&& M>= 1&& W>= e[i].w )
                {
                    hash[i]= 1;
                    DFS( i, W- e[i].w, M- 1, e[i].p );
                    hash[i]= 0;
                    rec[W][M]= max( rec[W][M], rec[W- e[i].w][M- 1] );
                }
            }
            printf( "%d\n", rec[W][M] );
        }
    }
    

  • 相关阅读:
    js将单个反斜杠转化为斜杠的问题
    HTML提供的6种空格
    JavaScript 内存管理
    JavaScript:4个常见的内存泄露
    正则多种匹配描述
    css3图片展示方式
    动态规划篇一:初见动态规划
    小球下落(Dropping Balls, Uva 679)
    破损的键盘(悲剧文本)(Broken Keyboard(a.k.a. Beiju Text),Uva 11988)
    铁轨(rails, ACM/ICPC CERC 1997,Uva 514)
  • 原文地址:https://www.cnblogs.com/Lyush/p/2135468.html
Copyright © 2020-2023  润新知