hdu 2660 Accepted Necklace
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=2660
Accepted Necklace
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
dfs。。
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<set>
using std::max;
using std::sort;
using std::pair;
using std::swap;
using std::queue;
using std::multiset;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 30;
const int INF = 0x3f3f3f3f;
typedef unsigned long long ull;
struct Node {
int v, w;
}A[N];
bool vis[N];
int W, K, n, ans;
void dfs(int cur, int w, int v, int tot) {
if (tot == K) {
ans = max(ans, v);
return;
}
for (int i = cur; i < n; i++) {
if (!vis[i] && tot + 1 <= K && w + A[i].w <= W) {
vis[i] = true;
dfs(i + 1, w + A[i].w, v + A[i].v, tot + 1);
vis[i] = false;
}
}
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) {
ans = -INF;
scanf("%d %d", &n, &K);
rep(i, n) {
vis[i] = false;
scanf("%d %d", &A[i].v, &A[i].w);
}
scanf("%d", &W);
dfs(0, 0, 0, 0);
printf("%d\n", ans);
}
return 0;
}
hdu 2660 Accepted Necklace的更多相关文章
- 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 ...
- 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]; 注意中间一层必须逆序循环 ...
- Accepted Necklace
Accepted Necklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 5730 Shell Necklace [分治fft | 多项式求逆]
hdu 5730 Shell Necklace 题意:求递推式\(f_n = \sum_{i=1}^n a_i f_{n-i}\),模313 多么优秀的模板题 可以用分治fft,也可以多项式求逆 分治 ...
- 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 5730 Shell Necklace(CDQ分治+FFT)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5730 [题目大意] 给出一个数组w,表示不同长度的字段的权值,比如w[3]=5表示如果字段长度为3 ...
- hdu 5730 Shell Necklace——多项式求逆+拆系数FFT
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5730 可以用分治FFT.但自己只写了多项式求逆. 和COGS2259几乎很像.设A(x),指数是长度,系数 ...
随机推荐
- 【LeetCode】6. ZigZag Conversion 锯齿形转换
题目: 思路: 以图为例:s={'A','B','C','D','E','F','G','H'.....} 1.先不考虑中间元素F.G.H.N...,每一行前后元素在数组中对应下标相差size=2*n ...
- ORACLE 导空表结构
exp username/psd@sid file='E:\xx.dmp' tables=(xxx_%) ROWS=N 以下代码没什么用,我就乱写 set oracle_sid=ora11gsqlpl ...
- 关于MPEG2中的图像序列和图像组头GOP
图像序列 图像序列是由图像组构成的,是随机存取段落. sequence_header_code – The sequence_header_code is the bit string ‘000001 ...
- 洛谷P1113 杂物
P1113 杂务 251通过 441提交 题目提供者该用户不存在 标签图论递推 难度普及/提高- 提交该题 讨论 题解 记录 最新讨论 为什么会只有10分? 题目描述 John的农场在给奶牛挤奶前有很 ...
- Devexpress 中如何写ASPxGridView新增修改时的数据验证
//验证 protected void grid_Deptlist_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidatio ...
- JavaWeb工作原理
一.HTTP协议的理解 什么是HTTP协议 HTTP是一种超本文传输协议,是一套计算机在网络中通信的规则.他是一种无状态的传输方式. HTTP协议的格式 HTTP的请求: 请求行(HTTP方法.请求的 ...
- tool debug Android phonegap app
phonegap debug 最近发现了一个可以调试phonegap的工具 在Google浏览器上调试Android真机的APP 这是好啊!!!跟Mac上的Safari 浏览器一样调试iOS 的A ...
- 生成ssl证书
http://blog.csdn.net/liuchunming033/article/details/48470575 https://segmentfault.com/a/119000000256 ...
- Windows phone 8 学习笔记(1) 触控输入(转)
Windows phone 8 的应用 与一般的Pc应用在输入方式上最大的不同就是:Windows phone 8主要依靠触控操作.因此在输入方式上引入一套全新的触控操作方式,我们需要重新定义相关的事 ...
- SQL 2008配置管理工具服务显示 远程过程调用失败0x800706be
摘自: http://www.cnblogs.com/cool-fire/archive/2012/09/15/2686131.html 基本上我的解决方案也是根据该文提示 操作的. 因为 我后来 装 ...