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. 【bzoj2007】[Noi2010]海拔 最小割+对偶图+最短路

    题目描述 YT市是一个规划良好的城市,城市被东西向和南北向的主干道划分为n×n个区域.简单起见,可以将YT市看作一个正方形,每一个区域也可看作一个正方形.从而,YT城市中包括(n+1)×(n+1)个交 ...

  2. hdu 1267 下沙的沙子有几粒? (递推)

    下沙的沙子有几粒? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  3. hbase(0.94) get、scan源码分析

    简介 本文是需要用到hbase timestamp性质时研究源码所写.内容有一定侧重.且个人理解不算深入,如有错误请不吝指出. 如何看源码 hbase依赖很重,没有独立的client包.所以目前如果在 ...

  4. IPVS和Nginx两种WRR负载均衡算法详解

    动机 五一临近,四月也接近尾声,五一节乃小长假的最后一天.今天是最后一天工作日,竟然感冒了,半夜里翻来覆去无法安睡,加上窗外大飞机屋里小飞机(也就是蚊子)的骚扰,实在是必须起来做点有意义的事了!    ...

  5. 怎么给word加底纹

  6. nginx,docker反向代理

    1. [root@javanginx ~]# cat /etc/nginx/nginx.conf user root root;worker_processes 4;error_log /var/lo ...

  7. js和jquery修改背景颜色的区别

    html: <HTML> <head> <meta http-equiv="content-type" content="text/html ...

  8. uva 11427

    题目大意:每天晚上你都玩纸牌,如果第一次赢了就高高兴兴地去睡觉:如果输了就接着玩,假设每盘游戏获胜的的概率都是p,且各盘游戏相互独立.你是一个固执的完美主义者,因此会一直玩到当晚获胜局数的比例严格大于 ...

  9. Navicat Premium mysql

    #Sql语句 创建一表格 ),name ),age ),sex )); #添加语句 ,,'男'); ,,'男'); #删除 delete from user3;#user3中所有数据全删 delete ...

  10. [洛谷P3501] [POI2010]ANT-Antisymmetry

    洛谷题目链接:[POI2010]ANT-Antisymmetry 题目描述 Byteasar studies certain strings of zeroes and ones. Let be su ...