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
 
题目意思:求出K个宝石最大价值总和,但重量不能超过W;
#include<stdio.h>
struct ston
{
int sa,sw;
};
struct ston s[25],tem;
int su,N,K,W;
void DFS(int i,int suma, int w,int k)
{
int j;
if(su<suma)//比较总价值
su=suma;
if(k==K)//宝石个数不能超过K个
return ;
for(j=i+1;j<=N;j++)
if(s[j].sw+w<=W&&k+1<=K)
DFS(j,s[j].sa+suma,s[j].sw+w,k+1);
}
int main()
{
int t,i,j,e,sum;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&N,&K);
for(i=1;i<=N;i++)
scanf("%d%d",&s[i].sa,&s[i].sw);
scanf("%d",&W); for(i=1;i<=N;i++)//先按价值从大到小排序
{
e=i;
for(j=i+1;j<=N;j++)
if(s[e].sa<s[j].sa)
e=j;
tem=s[i];s[i]=s[e];s[e]=tem;
}
sum=0;
for(i=1;i<=N;i++)//看以那个开头总价值最大
if(s[i].sw<=W&&K>0)
{
su=s[i].sa;
DFS(i,s[i].sa,s[i].sw,1);
if(su>sum)
sum=su;
}
printf("%d\n",sum);
}
}

hdu2660 Accepted Necklace (DFS)的更多相关文章

  1. HDOJ(HDU).2660 Accepted Necklace (DFS)

    HDOJ(HDU).2660 Accepted Necklace (DFS) 点我挑战题目 题意分析 给出一些石头,这些石头都有自身的价值和重量.现在要求从这些石头中选K个石头,求出重量不超过W的这些 ...

  2. HDU 2660 Accepted Necklace【数值型DFS】

    Accepted Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  3. hdu 2660 Accepted Necklace

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2660 Accepted Necklace Description I have N precious ...

  4. Accepted Necklace

    Accepted Necklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...

  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 5727 Necklace dfs+二分图匹配

    Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...

  7. 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]; 注意中间一层必须逆序循环 ...

  8. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  9. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

随机推荐

  1. 精通 Oracle+Python,第 2 部分:处理时间和日期

    从 Python 2.4 版开始,cx_Oracle 自身可以处理 DATE 和 TIMESTAMP 数据类型,将这些列的值映射到 Python 的 datetime 模块的 datetime 对象中 ...

  2. IntelIoT技术笔记Java/Eclipse

    1. 获取最新版本 使用"Team sync perspective",如果想要看到全部工程的差异,选择全部工程,右键-Team-sync with Repository:将会自动 ...

  3. NET平台和C#

    .NET平台和C#编程 一.深入.NET框架 1..NET框架具有两个组件:CLR(公共语言运行时)和FCL(框架类库),CLR是.NET框架的基础 2.框架核心类库: System.Collecti ...

  4. Codeforces Round #205 (Div. 2) : B

    如果某个数出现的次数大于或等于2次,那么平均分配到两个容器里面: 这里利用一个k来使得当出现次数为奇数时候分配得更加均匀: 剩下的就平均分配到两个容器里: 代码: #include<iostre ...

  5. 如何用 React Native 创建一个iOS APP?(二)

    我们书接上文<如何用 React Native 创建一个iOS APP?>,继续来讲如何用 React Native 创建一个iOS APP.接下来,我们会涉及到很多控件. 1 AppRe ...

  6. Qt4.8 移植(超详细Configure的参数)

    Qt4.8.6 configure 参数 不只是适用于Qt4.8.6,原则上适用于Qt4所有版本 Usage: configure [-h] [-prefix <dir>] [-prefi ...

  7. Dynamic系列--Dynamic 与反序列化

    通常在调用其他站点的api时,如果返回的结果为 json数据,而我们又不想再重新定义实体类时,可以使用dynamic类型. 但是有以下需要注意的地方. 当内容为空时,反序列化结果为null 当内容格式 ...

  8. 怒刷BZOJ记录(二)1038~10xx

    我实在是太弱了...不滚粗只能刷BZOJ了...这里来记录每天刷了什么题吧. 2015-8-13: 正式开始! 1030[JSOI2007]文本生成器                       | ...

  9. Monkey ‘mk_request_header_process’函数输入验证漏洞

    漏洞名称: Monkey ‘mk_request_header_process’函数输入验证漏洞 CNNVD编号: CNNVD-201308-003 发布时间: 2013-08-22 更新时间: 20 ...

  10. Light OJ 1037 - Agent 47(预处理状态压缩DP)

    题目大意: 有个特工要执行任务,他会遭遇到最多15个目标,特工必须把他们全部杀死.当他杀死一个目标后他可以使用目标的武器来杀死其他人.因此他必须有一个杀人的顺序,使得他开枪的次数最小. 现在给你一个表 ...