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. mysql的binlog安全删除

    理论上,应该在配置文件/etc/my.cnf中加上binlog过期时间的配置项,expire_logs_days = 10 但是如果没有加这一项,随着产生越来越多的binlog,磁盘被吃掉了不少.可以 ...

  2. spring day02-go3

    1.复制xml到container/basic包下2.ExampleBean中设置属性,包括基本类型(String和age)和集合类型(List,Set,Map),Properties类型3.添加无参 ...

  3. 2013年各大小IT公司待遇

    2013年各大小IT公司待遇(初版   摘自好网)本人西电硕士,根据今年找工作的情况以及身边同学的汇总,总结各大公司的待遇如下,吐血奉献给各位学弟学妹,公司比较全,你想去的公司不在这里面,基本上是无名 ...

  4. Java实现文件的读写,复制

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStr ...

  5. 02---Net基础加强

    将普通日期格式:“2014年7月8日”转换成汉字日期格式:“二零一四年七月八日”.(暂时不考虑10日,13日,23日) class Program { static void Main(string[ ...

  6. c# 获取路径的几种方法

    1.取得控制台应用程序的根目录方法 方法1.Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径方法2.AppDomain.CurrentDomain.Bas ...

  7. switch结构2016/03/08

    Switch  03/08 一.结构 switch(){  case *: ;break;……default: ;brek;} 练习:输入一个日期,判断这一年第几天? Console.Write(&q ...

  8. MyEclipse启动失败

    日志的一部分: !SESSION 2014-09-24 11:47:03.156 -----------------------------------------------eclipse.buil ...

  9. Javascript与C#编码解码

    (一) Javascript与C#编码解码的对应关系 http://www.jb51.net/article/44062.htm 这篇文章主要是对JS与C#编码解码进行了详细的介绍,需要的朋友可以过来 ...

  10. HashMap存值

    package com.exmple.tool; import java.util.HashMap; public class UserTool { public static HashMap< ...