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. 
 

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
原创粉丝点击