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基础之创建窗口——创建应用程序窗口(TryWindow)

    控制台程序. 准备好应用程序窗口及其包含的组件并显示,这称为实现窗口.调用应用程序窗口对象的setVisible()方法就会实现窗口.实现了应用程序的GUI之后,在主线程中修改或查询GUI可能会导致死 ...

  2. ES6入门之Iterator和for...of

    Iterator遍历器 遍历器(Iterator)就是这样一种机制.它是一种接口,为各种不同的数据结构提供统一的访问机制.任何数据结构只要部署Iterator接口,就可以完成遍历操作(即依次处理该数据 ...

  3. postgres-toolkit (A Victorinox for PostgreSQL DBA )

    postgres-toolkit A collection of scripts and utilities to manage PostgreSQL servers. Allows DBA to p ...

  4. JAVA-面向对象-继承

    继承   (关键字extends   ) (关键字 final 表示终态,在父类前加 final 则父类无法被继承,加在方法前则方法不能被重写或者覆盖,加在变量前则变量只能被赋值一次) 1.权限修饰符 ...

  5. 创建Java类并实例化的基本过程

    package com.sanguosha.java; /* * 面向对象实现的过程 * 1.创建类并设计类的成员(成员变量即属性and成员方法即方法) * 2.通过类来创建类的对象,也称类的实例化 ...

  6. Ruby与Python开发的环境IDE配置(附软件的百度云链接)

    Ruby开发环境配置 1.Aptana_RadRails(提示功能不好,开发Ruby不推荐) 链接:http://pan.baidu.com/s/1i5q96K1 密码:yt04 2.Aptana S ...

  7. ofbiz进击 。 ofbiz 退货流程(包含获取可退货项流程分析 以及 取消退货项的过程分析)

    根据订单获取可退货项流程分析 退货的时候,调用 services_return.xml 中的获取可进行退货的退货项  getReturnableItems  ,该服务调用了Java类 org.ofbi ...

  8. 查看真机的系统版本sdk

    1.adb devices 确保连接上了真机 2.adb shell 进入android系统 3.进入system目录下 4.查看build.prop文件 cat build.prop

  9. linux第11天 共享内存和信号量

    今天主要学习了共享内存和信号量 在此之前,有个管道问题 ls | grep a 整句话的意思是将ls输出到管道的写端,而流通到另一端的读端,grep a则是从管道的读端读取相关数据,再做筛选 共享内存 ...

  10. yii中sphinx索引配置解析

    #MySQL数据源配置,详情请查看:http://www.coreseek.cn/products-install/mysql/#请先将var/test/documents.sql导入数据库,并配置好 ...