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. Python 类型

    文章出处:http://www.cnblogs.com/winstic/,请保留此连接   python是动态类型语言,不需要预先声明变量的类型,变量类型和值在赋值的那一刻被初始化   python使 ...

  2. "git add -A" is equivalent to "git add .; git add -u".

    git add -A stages All git add . stages new and modified, without deleted git add -u stages modified ...

  3. Maya pywin32

    Maya 2011 – 2013 64-bit: maya-64-bit-pywin32.zipMaya 2011 – 2013 32-bit: maya-32-bit-pywin32.zipMaya ...

  4. FutureTask源码解读

    import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.ut ...

  5. 使用ListItem给DropDownList填充数据

    global::日积月累啥的啊.DBhelper db = new 日积月累啥的啊.DBhelper(); ListItem[] item=]; DataTable dt=db.GetDataTabl ...

  6. TCP连接建立和关闭中的疑难点

    TCP连接建立和关闭中的疑难点 作者:夏语岚    撰写日期:2011-10-29 近日在阅读<Unix网络编程>,以前在<计算机网络>课程中学到TCP,当时只是简单了解了TC ...

  7. Contest 20140914 Mushroom写情书 字符串雙hash 後綴數組

    0111:Mushroom写情书 查看 提交 统计 提问 总时间限制:  10000ms 内存限制:  256000kB 描述 有一天,Mushroom准备向他的GF表白,为了增加表白成功率,Mush ...

  8. Android中moveTo、lineTo、quadTo、cubicTo、arcTo详解(实例)

    1.Why 最近在写android画图经常用到这几个什么什么To,一开始还真不知道cubicTo这个方法,更不用说能不能分清楚它们了,所以特此来做个小笔记,记录下moveTo.lineTo.quadT ...

  9. MFS学习总结

    MFS学习总结 MFS概述.特性和新版改进 MFS 工作原理和设计架构 MFS的安装.部署.配置 MFS的高级特性 MFS的性能测试 MFS集群的维护 MFS的常见问题和建议对策 一.MFS概述.特性 ...

  10. Nginx windows下搭建过程

    内容列表: 简要介绍 下载安装 配置测试 一.简要介绍 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP ...