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. BZOJ4399 魔法少女LJJ(线段树合并)

    注意到只有增加点/合并的操作.这些操作都可以用线段树完成,于是线段树合并一发就好了.注意乘积大小直接比较肯定会炸,取个对数即可.数据中存在重边. #include<iostream> #i ...

  2. hdu 1286 找新朋友 (欧拉函数)

    找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  3. [洛谷P2763]试题库问题

    题目大意:有 $k$ 种类型和 $n$ 个题目,每个题目会适应部分类型,第$i$个类型需要$s_i$的题,一道题只能满足一种类型,现要求出满足所有类型的题目的方案 题解:看到匹配,想到网络流,源点向试 ...

  4. Java 中request.getInputStream()和BufferedReader 和 InputStreamReader 用法

    关于request.getInputStream(): http://www.cnblogs.com/steve-cnblogs/articles/5420198.html 浏览器 采用了一种编码方式 ...

  5. sls文件

    http://www.ituring.com.cn/article/42238 只是数据而已 深入学习之前,明白SLS文件只是结构化的数据而已是很有用的.看懂和编写SLS文件不需要理解这一点,但会让你 ...

  6. C#与数据库的连接的三种方式

    学习了.net的知识从C#一直到MVC,我一直觉得基础很重要,最近有复习一下数据库连接的三种方式 1 返回结果集的一张表 public static DataTable ExecuteDataTabl ...

  7. Chrome 本地通信

    http://blog.csdn.net/ztmaster/article/details/52684772

  8. 解决tomcat不支持中文路径的问题

    问题描述: 开发文件下载功能时,因为需求比较简单,要求下载一个说明文件.于是,直接给出了文件所在服务器的地址,通过链接直接下载此文件(因需求简单,未考虑安全方面的问题-_-||). 在这个过程中,文件 ...

  9. loj6043 「雅礼集训 2017 Day7」蛐蛐国的修墙方案

    传送门:https://loj.ac/problem/6043 [题解] 我们考虑这是个置换,所以一定形成了很多不相交的环. 对于每个环,我们只能选一段.不选.选一段.不选这样交替下去. 显然只有偶环 ...

  10. codevs1163访问艺术馆 树形dp

    算裸的树形dp吧 回来复习一波 #include<cstdio> #include<cstring> #include<algorithm> #include< ...