Accepted Necklace
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的更多相关文章
- hdu 2660 Accepted Necklace
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2660 Accepted Necklace Description I have N precious ...
- HDOJ(HDU).2660 Accepted Necklace (DFS)
HDOJ(HDU).2660 Accepted Necklace (DFS) 点我挑战题目 题意分析 给出一些石头,这些石头都有自身的价值和重量.现在要求从这些石头中选K个石头,求出重量不超过W的这些 ...
- HDU 2660 Accepted Necklace【数值型DFS】
Accepted Necklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 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 ...
- 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 ...
- 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]; 注意中间一层必须逆序循环 ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- 转载:hdu 题目分类 (侵删)
转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...
- 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 ...
随机推荐
- RMQ (Range Minimal Query) 问题 ,稀疏表 ST
RMQ ( 范围最小值查询 ) 问题是一种动态查询问题,它不需要修改元素,但要及时回答出数组 A 在区间 [l, r] 中最小的元素值. RMQ(Range Minimum/Maximum Query ...
- 如何用Java解析CSV文件
首先看一下csv文件的规则: csv(Comma Separate Values)文件即逗号分隔符文件,它是一种文本文件,可以直接以文本打开,以逗号分隔.windows默认用excel打开.它的格式包 ...
- iOS8中用UIVisualEffectView实现高斯模糊视图(毛玻璃效果)
UIBlurEffect *beffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]; UIVisualEffectView *vi ...
- CheckBox的用法
if (window.pageConfig["IsCommend"] == "True") { $("#IsCommend").v ...
- http://codeforces.com/contest/555/problem/B
比赛时虽然贪了心,不过后面没想到怎么处理和set的排序方法忘了- -,其实是和优先队列的仿函数一样的... 比赛后用set pair过了... #include <bits/stdc++.h&g ...
- poj1703 Find them, Catch them 并查集
poj(1703) Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26992 ...
- Android 封装Dialog
package com.example.myandroid01; import android.support.v7.app.ActionBarActivity; import android.os. ...
- MVC4下拉少数名族
List<SelectListItem> nationlist = new List<SelectListItem>() { new SelectListItem(){Valu ...
- CSS(Cascading Style Sheet,叠层样式表),作用是美化HTML网页。
CSS(Cascading Style Sheet,叠层样式表),作用是美化HTML网页. /*注释区域*/ 此为注释语法 一.样式表 (一)样式表的分类 1.内联样式表 和HTML联合显示,控 ...
- 【LAMP】在Debian系linux下安装LAMP
一.安装基本的编译环境 apt-get install build-essential 二.安装MySQL apt-get install mysql-server 三.安装Apache apt-ge ...