zoukankan      html  css  js  c++  java
  • 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] );
        }
    }
    

  • 相关阅读:
    HDU 4405 Aeroplane chess (概率dp)
    条件编译符号与公布
    hdu 1722 Cake 数学yy
    电脑显示U盘,可是读取不了
    多本Web前端深度修炼书籍(提供网盘下载链接)
    HDU 5410(2015多校10)-CRB and His Birthday(全然背包)
    Servlet体验之旅(二)——Session、Cookie
    &lt;pre&gt;标签
    每天学点Python之comprehensions
    编写html经常使用而又easy忘记的语句
  • 原文地址:https://www.cnblogs.com/Lyush/p/2135468.html
Copyright © 2011-2022 走看看