HDU 2660 Accepted Necklace
来源:互联网 发布:广州优化网站 编辑:程序博客网 时间:2024/05/17 22:22
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.
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
数据很小啊,直接枚举全部的选择情况就好了,用位运算搞一下即可。
#include<queue>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define rep(i,j,k) for (int i=j;i<=k;i++)const int INF=0x7FFFFFFF;const int sz=100;int T,a[sz],b[sz],n,m,w;int main(){ scanf("%d",&T); while (T--) { scanf("%d%d",&n,&m); rep(i,0,n-1) scanf("%d%d",&a[i],&b[i]); scanf("%d",&w); int ans=0; rep(i,0,(1<<n)-1) { int k=0,x=0,y=0; rep(j,0,n-1) { if ((1<<j)&i) { k++; x+=a[j]; y+=b[j]; } } if (k!=m||y>w) continue; ans=max(ans,x); } printf("%d\n",ans); } return 0;}
0 0
- hdu 2660 Accepted Necklace
- hdu-2660 Accepted Necklace
- hdu 2660 Accepted Necklace
- HDU 2660 Accepted Necklace
- HDU 2660 Accepted Necklace
- hdu 2660 Accepted Necklace
- HDU 2660 Accepted Necklace
- HDU 2660 Accepted Necklace
- hdu 2660 Accepted Necklace(dp)
- hdu 2660 Accepted Necklace DFS
- 杭电 HDU 2660 Accepted Necklace
- (step4.3.6)hdu 2660(Accepted Necklace)
- HDU 2660 Accepted Necklace (DFS)
- 【DP】 HDU 2660 Accepted Necklace 限制背包
- hdu 2660 Accepted Necklace(dfs、dp)
- HDOJ(HDU).2660 Accepted Necklace (DFS)
- hdu 2660 Accepted Necklace dp / dfs
- Accepted Necklace 2660
- OS X包管理工具——Homebrew
- BEEGO session写入,读取
- jquery实现点击div空白处隐藏该div
- Oracle RAC的五大优势及其劣势
- C++中模板的特化与偏特化
- HDU 2660 Accepted Necklace
- 【题】【线段树(lazy)】NKOJ 1868 矩形周长【USACO5.5.1】Picture
- PAT 甲级 1060 Are They Equal
- windows/linux + java + jna + dll/so 调用C/C++
- JQuery 树形表格插件使用
- Swift利用闭包(closure)来实现传值-->前后两个控制器的反向传值
- hdu 1503
- 核方法(kernel method)的主要思想
- hive教程