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] );
        }
    }
    

  • 相关阅读:
    handsontable合并项mergeCells应用及扩展
    handsontable的基础应用
    overflow的使用
    阿里云服务器磁盘空间扩容步骤
    使用Gitblit 在Windows2008 r2上部署Git Server(完整版)
    搭建一个基于微信公众号的信息采集功能
    js数组内置方法
    C#数组、js数组、json
    将EF项目从dbfirst转化为codefirst
    通过HttpWebRequest调用webService
  • 原文地址:https://www.cnblogs.com/Lyush/p/2135468.html
Copyright © 2011-2022 走看看