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. PyQt5创建第一个窗体(正规套路)

    一.Pyqt5 创建第一个窗体 很多人写窗体程序都是直接敲代码,不使用设计器,我个人不是很赞成这种做法.使用设计器的好处是直观.维护方便,尤其开发复杂窗体的效率高. 但是每次修改ui文件后,需要重新生 ...

  2. django中的事务管理

    在讲解之前首先来了解一下数据库中的事务. 什么是数据库中的事务? 热心网友回答: ():事务(Transaction)是并发控制的单位,是用户定义的一个操作序列.这些操作要么都做,要么都不做,是一个不 ...

  3. 【Tools】maven安装

    安装Maven插件老是报以下的错误,好像少了一个叫guava库的东西,但是在其他机器安装不报这个错误. Cannot complete the install because one or more  ...

  4. The type java.lang.String cannot be resolved. It is indirectly referenced from required .class files

    最近在做J2ME开发项目,配置环境一切OK,但是打开项目时某些文件提示: The type java.lang.String cannot be resolved. It is indirectly ...

  5. 如何利用服务器下发的Cookie实现基于此Cookie的会话保持

    Cookie是一种在客户端保持HTTP状态信息的常用技术,基于Cookie的会话保持常常出现在很多AX的部署案例中,尤其是涉及电子交易的系统部署中.此类系统往往要求负载均衡设备按照服务器下发的Cook ...

  6. Spark:Master High Availability(HA)高可用配置的2种实现

    Spark Standalone集群是Master-Slaves架构的集群模式,和大部分的Master-Slaves结构集群一样,存在着Master单点故障的问题.如何解决这个单点故障的问题,Spar ...

  7. Weblogic8.1 的性能优化

    注:在下面做的介绍都是以Weblogic8.1为例的,其它版本的Weblogic可能会有些许不同. 1) 设置JAVA参数: a) 编辑Weblogic Server启动脚本文件: BEA_HOMEu ...

  8. SQL in查询报告类型转换失败的3种解决办法

    -- in查询 nvarchar转int 错误 (NodeId 为 int 类型) ) = '3,5,6,' )' SELECT ID , NodeName FROM WF_WorkFlowNode ...

  9. 数据库连接&数据库进程&数据库操作

    root@webwall:/home/xiachengjiao# vi/webwall/mysql/my.cnf(看配置文件中的参数) root@webwall:/webwall/mysql/bin# ...

  10. Selenium 处理模态对话框

    模态对话框的原理 模态窗口 点击下一步的动作为,聚焦到“下一步”,然后直接回车 driver.FindElement(By.CssSelector("div.rg_btn a")) ...