• hdu 1660 Accepted Necklace (dfs or dp)


    题面

    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

    解析

    emmm,背包问题,这就不多解释了,我们直接枚举每一个背包选还是不选就好了。这里讲一下dfs的思路,dfs的话,我们直观的感受也是按照每个物品去进行判断,然后参数里面放当前物品价值,当前物品容量,当前拿的数量就好了,在数量达到上限的时候结束递归,当然不要忘记剪枝。

    代码实现

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<queue>
    #include<cmath>
    using namespace std;
    int n,m,t,mostval,ans;
    struct node {
        int val,w;
    };
    node a[25];
    void dfs (int cur_n,int cur_v,int cur_w,int cur_num) {
        if (n-cur_n<m-cur_num||cur_w>mostval) return ;
        if (cur_num==m) {
            ans=max(ans,cur_v);
        }
        dfs (cur_n+1,cur_v+a[cur_n].val,cur_w-a[cur_n].w,cur_num+1);
        dfs (cur_n+1,cur_v,cur_w,cur_num);
        return ;
    }
    int main () {
        cin>>t;
        while (t--) {
           cin>>n>>m;
           for (int i=1;i<=n;i++)  cin>>a[i].val>>a[i].w;
           cin>>mostval;
           ans=-999;
           dfs (0,0,0,0);
           cout<<ans<<endl;
        }
        return 0;
    }
    

    dp方程

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <list>
    #include <map>
    #include <queue>
    #include <stack>
    #include <bitset>
    #include <algorithm>
    #include <numeric>
    #include<vector>
    #include <functional>
    #define x first
    #define y second 
    using namespace std;
    typedef long long ll;
    const int N=1000000+10;
    int main() {
       int t,n,m;
       cin>>t;
       int dp[25][1010];
       int mostval;
       int val[25]={0},w[25]={0};
       while (t--) {
          cin>>n>>m;
          for (int i=1;i<=n;i++) cin>>val[i]>>w[i];
          cin>>mostval;
          memset (dp,0,sizeof (dp));
          for (int i=1;i<=n;i++) 
           for (int j=m;j>=1;j--) 
            for (int k=mostval;k>=val[i];k--) {
               dp[j][k]=max (dp[j][k],dp[j-1][k-w[i]]+val[i]);
            }
          cout<<dp[m][mostval]<<endl;
       }
       return 0; 
    }
    
    
    
  • 相关阅读:
    php 多进程
    关于TP的RBAC的使用
    谈谈自己对于Auth2.0的见解
    php 写队列
    关于thinkphp中Hook钩子的解析
    JS的闭包
    单链表的查找和取值-1
    shell输入输出重定向
    转-Visual Studio控制台程序输出窗口一闪而过的解决方法
    linux下如何调用不同目录下的.h 库文件
  • 原文地址:https://www.cnblogs.com/hhlya/p/13233910.html
Copyright © 2020-2023  润新知