hdu 2660 Accepted Necklace (二维背包)

Accepted Necklace

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

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

题意:就是你有n个宝石,想用k个做成一个项链送给你妈妈,每个宝石的价值和重量都不一样,你可以选择几个做成项链,但重量不能超过w,并且要得到的价值最大。

解题思路:一看就是一个贪心背包问题,用dp的思想来解决。在重量和个数直接取价值最大。

贴出代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int n, k, w,T;
    int value[25], weight[25], num[1005][25];
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d", &n, &k);
        memset(num, 0, sizeof(num));
        for(int i = 0; i<n; i++)
        {
            scanf("%d%d", &value[i], &weight[i]);
        }
        scanf("%d", &w);

        for(int i = 0; i<n; i++)
        {
            for(int j = k; j>0; j--)
            {
                for(int x = w; x>=weight[i]; x--)
                {
                    if(num[x][j] < num[x-weight[i]][j-1]+value[i])
                        num[x][j] = num[x-weight[i]][j-1]+value[i];
                }
            }
        }
        printf("%d\n", num[w][k]);
    }
    return 0;
}

hdu 2660 Accepted Necklace (二维背包)

时间: 2024-10-14 12:01:56

hdu 2660 Accepted Necklace (二维背包)的相关文章

hdu - 2660 Accepted Necklace (二维费用的背包问题)

http://acm.hdu.edu.cn/showproblem.php?pid=2660 f[v][u]=max(f[v][u],f[v-1][u-w[i]]+v[i]; 注意中间一层必须逆序循环. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 int dp[21][1001]; 6 int v[1001],w[1001]; 7 int

hdu 2660 Accepted Necklace(01-背包变形 || DFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2660 ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943

HDU 2660 Accepted Necklace (DFS)

Accepted Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2474    Accepted Submission(s): 973 Problem Description I have N precious stones, and plan to use K of them to make a necklace f

【DP】 HDU 2660 Accepted Necklace 限制背包

给出n个物品 最多能拿k个 选取的物品的总重量不能超过w 因为每个物品只有一个 转移顺序为 for(int i=0; i<n; i++) for(int j=w; j>=b[i]; j--) for(int l=1; l<=k; l++) 保证了一个物品只放进一次 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <string> #include <

HDU 2660 Accepted Necklace

此题的大意就是要制作一条项链,这个项链必须用k块石头来制作,且重量不能超过W,问所能制作项链的最大价值. 此题数据很水,DFS完全暴力就能过. 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #define MAX(A,B) (A>B?A:B) 5 int n, k, W;//n块石头 制作一条项链所需要的宝石数量k 最大承受重量 W 6 int v[35], w[35], ans;//每块

HDU 2159 FATE(二维完全背包)

中文题目就不用解释了   就是裸的二维完全背包 d[i][j]表示消耗i忍耐杀j个怪最多可获得的经验  然后就用完全背包来做了  二维背包背包不过是多了一重循环 <span style="font-family:Arial Black;">#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N = 105; int ma

HDU 2159FATE(二维背包)

题目地址:HDU 2159 二维的背包,刚开始用的一维,老感觉哪里不对,有两个制约因素.于是就改成了二维的,就过了.. 代码如下: #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <queue> #include <cmath> #include <sta

HDU 2159 FATE(二维全然背包)

中文题目就不用解释了   就是裸的二维全然背包 d[i][j]表示消耗i忍耐杀j个怪最多可获得的经验  然后就用全然背包来做了  二维背包背包只是是多了一重循环 <span style="font-family:Arial Black;">#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N = 105; int ma

二维背包初步

问题 二维费用的背包问题是指:对于每件物品,具有两种不同的费用:选择这件物品必须同时付出这两种代价:对于每种代价都有一个可付出的最大值(背包容量). 问怎样选择物品可以得到最大的价值. 设这两种代价分别为代价1和代价2,第i件物品所需的两种代价分别为a[i]和b[i].两种代价可付出的最大值(两种背包容量)分别为V和U.物品的价值为c[i]. 算法 费用加了一维,只需状态也加一维即可. 设f[i][v][u]表示前i件物品付出两种代价分别为v和u时可获得的最大价值. 状态转移方程就是:f [i]