【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 <iostream>
#include <algorithm>
#include <sstream>
#include <math.h>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#define cler(arr, val)    memset(arr, val, sizeof(arr))
#define IN     freopen ("in.txt" , "r" , stdin);
#define OUT  freopen ("out.txt" , "w" , stdout);
typedef long long  LL;
const int MAXN = 1000006;//点数的最大值
const int MAXM = 20006;//边数的最大值
const int INF = 1101521204;
const int mod=1000000007;
int dp[23][1211],a[1111],b[1111];
int main()
{
    int n,t,m,k,w;
   // IN;
    scanf("%d",&t);
    while(t--)
    {
        cler(dp,0);
        scanf("%d%d",&n,&k);
        for(int i=0; i<n; i++)
            scanf("%d%d",&a[i],&b[i]);
        scanf("%d",&w);
        for(int i=0; i<n; i++)
            for(int j=w; j>=b[i]; j--)
                for(int l=1; l<=k; l++)
                    dp[l][j]=max(dp[l-1][j-b[i]]+a[i],dp[l][j]);
        printf("%d\n",dp[k][w]);
    }
}
时间: 2024-08-08 01:28:08

【DP】 HDU 2660 Accepted Necklace 限制背包的相关文章

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 f

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

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

此题的大意就是要制作一条项链,这个项链必须用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 5616 Jam&#39;s balance 背包DP

Jam's balance Problem Description Jim has a balance and N weights. (1≤N≤20)The balance can only tell whether things on different side are the same weight.Weights can be put on left side or right side arbitrarily.Please tell whether the balance can me

hdu 1011 Starship Troopers (依赖背包 树形dp)

题目: 链接:点击打开链接 题意: n个房间组成一棵树,你有m个战队,从1号房间开始依次clear每个房间,在每个房间需要花费的战队个数是bugs/20,得到的价值是the possibility of capturing a brain,求最大的价值. 算法: 树形dp,有依赖的背包问题.(依次clear每个房间) 思路: 状态转移dp[i][j]表示根结点为i时(房间i)花费j个战队能够得到的最大价值(捕捉到一个brain最大的可能值).递归求出每个根结点处的最大值,最后dp[1][m]就是

简单的dp hdu 数塔(水题)

数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 21314    Accepted Submission(s): 12808 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少

HDU 4563 御剑术I(背包)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4563 题意:一个点开始在原点.有n个命令.第i个命令施加到这个点时,这个点的速度为(Vxi,Vyi),即在x方向的速度为Vxi,在y方向的速度为 Vyi.并且这个命令施加到点时之前的速度全部消失.每种命令最多使用一次.问在x方向走长度为m时在y方向的最大高度是多少?每种命令只能在整数时刻施 加. 思路:首先,每种命令使用的先后顺序显然是没有关系的.除了最后一个施加的命令,之前的命令必然都是使用了整数