Accepted Necklace

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3136 Accepted Submission(s): 1213

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

背包问题,DP[i][m][k]表示选第i件物品时的体积为m,以选定的物品为k件,所以Dp[i][m][k]=max(Dp[i-1][m][k],Dp[i-1][m-v[i]][k-1]+w[i]);再将其转化为01背包

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int,int>p;
const int MAX = 1100;
int Dp[MAX][35];
p Th[35];
int main()
{
int n,k,m;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%d %d",&Th[i].first,&Th[i].second);
}
scanf("%d",&m);
memset(Dp,0,sizeof(Dp));
for(int i=1;i<=n;i++)
{
for(int j=m;j>=Th[i].second;j--)
{
for(int s=1;s<=k;s++)
{
Dp[j][s]=max(Dp[j-Th[i].second][s-1]+Th[i].first,Dp[j][s]);
}
}
}
printf("%d\n",Dp[m][k]);
}
return 0;
}

Accepted Necklace的更多相关文章

  1. hdu 2660 Accepted Necklace

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2660 Accepted Necklace Description I have N precious ...

  2. HDOJ(HDU).2660 Accepted Necklace (DFS)

    HDOJ(HDU).2660 Accepted Necklace (DFS) 点我挑战题目 题意分析 给出一些石头,这些石头都有自身的价值和重量.现在要求从这些石头中选K个石头,求出重量不超过W的这些 ...

  3. HDU 2660 Accepted Necklace【数值型DFS】

    Accepted Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. hdu2660 Accepted Necklace (DFS)

    Problem Description I have N precious stones, and plan to use K of them to make a necklace for my mo ...

  5. hdu 2660 Accepted Necklace(dfs)

    Problem Description I have N precious stones, and plan to use K of them to make a necklace for my mo ...

  6. 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]; 注意中间一层必须逆序循环 ...

  7. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  8. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

  9. 2016 Multi-University Training Contest 1 H.Shell Necklace

    Shell Necklace Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

随机推荐

  1. Java基础之序列化对象——反序列化对象(DeserializeObjects)

    控制台程序,使用如下代码能读入包含Junk对象的文件: import java.io.*; import java.nio.file.*; class DeserializeObjects { pub ...

  2. SqlServer StringToTable性能测试

    问题起因: 最近做的项目DB数据量比较大(基本上一个月的数据就是10亿),而工程中Proc参数中包含有id拼接字符串,id拼接字符串格式:1,2,4,5,100,301.当数据量很小的情况下,这样做没 ...

  3. zjuoj The 12th Zhejiang Provincial Collegiate Programming Contest Ace of Aces

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5493 The 12th Zhejiang Provincial ...

  4. [原创]java WEB学习笔记67:Struts2 学习之路-- 类型转换概述, 类型转换错误修改,如何自定义类型转换器

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  5. [原创]java WEB学习笔记60:Struts2学习之路--Actioin-声明式异常处理

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  6. .NET: WPF DependencyProperty

    DependencyProperty and DependencyObject is the core of WPF data binding. We can use this two class t ...

  7. thinkphp介绍

    1.thinkphp是一个免费的开源的轻量级的高效的国产的php框架 2.现在主流的框架有:   zend framwork 框架,功能十分齐全,是php官网开发的一个框架   yii框架 十分轻巧的 ...

  8. c++ 中this底层

    成员变量设置在一个结构体中, 操作成员变量的成员函数,其实质上就是拥有一个隐藏的 成员变量结构体的地址指针,俗称this指针.

  9. CSS_03_01_CSS选择器中单选择器:关联选择器档

    选择器中单选择器:关联选择器档 第01步:编写select.css @charset "utf-8"; /* span标签中的b标签的exam类 */ span b.exam{ b ...

  10. struts不同session范围添加、访问属性

    第01步:编写bean类 package com.self.bean; import java.util.Date; public class User { private Date birthday ...