Accepted Necklace

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2474    Accepted Submission(s): 973

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
 
 

【题意】:宝石的数目n,制成项链所需的宝石个数k,然后再给出每个宝石的价值v与重量w,还有母亲会接受的最大重量,求出在小于等于最大重量范围内,项链的价值尽可能大。

【分析】:因为数据不大可用dfs,也可以用01背包。

【代码】:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long
using namespace std;
int maxn,n,k,wei;
int vis[];
struct node
{
int v,w;
}a[];
/*
给出宝石的数目n,制成项链所需的宝石个数k,然后再给出每个宝石的价值与重量,
还有母亲会接受的最大重量,求出在小于等于最大重量范围内,项链的价值尽可能大。*/
void dfs(int v, int w, int x, int s)//x:枚举下标,s:遍历深度(个数)
{
if(w==wei||s==k)
{
if(maxn<v)
maxn=v;
return ;
}
for(int i=x;i<=n;i++)
//这个地方下标直接从x开始,因为宝石并没有要求序列问题只是求价值和,所以不需要考虑重复情况,是个很大的剪枝
//举例: 3 4 5 6 == 4 3 5 6 == 5 3 6 4虽然序列不同,但是价值和相同,这样的只需要搜一次即可也就是前面搜过的重量不再搜
{
if(!vis[i] && w+a[i].w <= wei)//未访问+未越界(小于等于最大重量范围
{
vis[i]=;
dfs(v+a[i].v, w+a[i].w, i+, s+);
vis[i]=;
}
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>k;
for(int i=;i<n;i++)
cin>>a[i].v>>a[i].w;
cin>>wei;
memset(vis,,sizeof(vis));
maxn=-;
dfs(,,,);
cout<<maxn<<endl;
}
return ;
}

DFS

#include <cstdio>
#include <cstring>
int max(int x, int y)
{
return x>y?x:y;
}
int dp[][];
int main()
{
int W,val[],wei[];
int n, k, t, i, j, l; while(~scanf("%d",&t))
{
while(t--)
{
memset(dp,,sizeof(dp));
scanf("%d %d",&n,&k);
for(i = ;i < n; i++)
{
scanf("%d %d",&val[i],&wei[i]);
}
scanf("%d",&W);
for(i = ; i < n; i++)
{
for(l = W; l >= wei[i]; l--)
{
for(j = ; j <= k; j++)
{
dp[l][j] = max(dp[l][j],dp[l-wei[i]][j-]+val[i]);
}
}
}
printf("%d\n",dp[W][k]);
}
}
return ;
}

01背包

HDU 2660 Accepted Necklace【数值型DFS】的更多相关文章

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

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

  2. hdu 2660 Accepted Necklace

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

  3. HDU 1015 Safecracker【数值型DFS】

    Safecracker Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  4. 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 ...

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

  6. HDU 1427 速算24点【数值型DFS】

    速算24点 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  7. 1789: [Ahoi2008]Necklace Y型项链

    1789: [Ahoi2008]Necklace Y型项链 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 421  Solved: 258[Submit] ...

  8. [BZOJ1789][BZOJ1830][Ahoi2008]Necklace Y型项链

    [BZOJ1789][BZOJ1830][Ahoi2008]Necklace Y型项链 试题描述 欢乐岛上众多新奇的游乐项目让小可可他们玩的非常开心.现在他们正在玩比赛串项链的游戏,谁串的最快就能得到 ...

  9. Accepted Necklace

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

随机推荐

  1. JDBC连接数据库的过程

    以连接MySQL为例: (1)加载MySQL数据库连接的驱动程序.到MySQL官网下载该驱动程序jar包,然后把包复制到WEB-INF/lib目录下,则JDBC会调用Class.forName()方法 ...

  2. [LOJ#6437][BZOJ5373]「PKUSC2018」PKUSC

    [LOJ#6437][BZOJ5373]「PKUSC2018」PKUSC 试题描述 九条可怜是一个爱玩游戏的女孩子. 最近她在玩一个无双割草类的游戏,平面上有 \(n\) 个敌人,每一个敌人的坐标为 ...

  3. hdu4035 Maze 【期望dp + 数学】

    题目链接 BZOJ4035 题解 神题啊...orz 不过网上题解好难看,数学推导不写\(Latex\)怎么看..[Latex中毒晚期] 我们由题当然能很快写出\(dp\)方程 设\(f[i]\)表示 ...

  4. 洛谷 P2611 [ZJOI2012]小蓝的好友 解题报告

    P2611 [ZJOI2012]小蓝的好友 题目描述 终于到达了这次选拔赛的最后一题,想必你已经厌倦了小蓝和小白的故事,为了回馈各位比赛选手,此题的主角是贯穿这次比赛的关键人物--小蓝的好友. 在帮小 ...

  5. 创建dll

    在制作dll的时候,如果全局变量不导出,而函数调用中,包含了全局变量,那么会出现全局变量没有值的问题. add.c #pragma once //强制无签名utf-8 #include "a ...

  6. git使用笔记(六)github

    By francis_hao    Nov 20,2016 github介绍 github是一个网站https://github.com/,可以实现基于git(当然,svn也是可以的)的代码托管工作. ...

  7. [模拟赛] GotoAndPlay

    GotoAndPlay 10月3日,在杭州市西湖景区,一只小松鼠不停地接受一道道食物,花生. 玉米.饼干,可谓来者不拒,憨态可掬的模样吸引了众多围观者... Description 小松鼠终于吃撑了, ...

  8. c++编辑器下载地址

    https://msdn.itellyou.cn/ 输入上述地址选中下图所示的按钮:

  9. oracle的sql语句训练

    --查询工资最高的人的名字select ename ,sal from emp where sal=(select max(sal) from emp );--求出员工的工资在所有人的平均工资之上的人 ...

  10. 基于多线程的TCP socket通信经典案例

    服务器端 package com.thinkvenus.study.socket; import java.io.BufferedReader; import java.io.IOException; ...