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. GIF文件转换为头文件工具

    目的: GIF文件转为头文件 举例: 用UE打开GIF文件,如下图所示:图1 test.gif文件将上面文件内容转化为头文件,放到一个数组里面,内容如下:图2 test.h文件 思路: 从上面可知,将 ...

  2. QueryRunner的使用

    在相继学习了JDBC和数据库操作之后,我们明显感到编写JDBC代码并非一件轻松的事儿.为了帮助我们更高效的学习工作,从JDBC的繁重代码中解脱出来,老佟给我们详尽介绍了一个简化JDBC操作的组件——D ...

  3. liveReload

    依赖条件: 1.安装liveReload浏览器插件: http://livereload.com/extensions/ chrome可以直接去在线商店安装liveReload. P.S.也可以贴代码 ...

  4. hadoop Yarn 编程API

    客户端编程库: 所在jar包: org.apache.hadoop.yarn.client.YarnClient 使用方法: 1 定义一个YarnClient实例: private YarnClien ...

  5. hustoj 1017 - Exact cover dancing link

    1017 - Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 5851 Solved: 3092 ...

  6. 深入了解一下PYTHON中关于SOCKETSERVER的模块-D

    在LINUX上以FORK的方式使用SIMPLEHTTPSERVER. 记得哟,THREADING能在WIN和LINUX上实现,但FORK,不支持WIN的. 除了SIMPLE之外,还有CGI,就不示例啦 ...

  7. Nginx+Varnish又开始新的征程了

    要自己多测试一下.总觉得机器不够用.

  8. 【UVA1378】A Funny Stone Game (博弈-求SG值-输出方案)

    [题目] Description The funny stone game is coming. There are n piles of stones, numbered with 0, 1, 2, ...

  9. 14.8.2 Role of the .frm File for InnoDB Tables InnoDB 表得到 .frm文件的作用

    14.8.2 Role of the .frm File for InnoDB Tables InnoDB 表得到 .frm文件的作用 Vsftp:/data01/mysql/zjzc# ls -lt ...

  10. heap表按字符串和数值型排序规则

    SQL> create user scan identified by scan default tablespace users; User created. SQL> grant db ...