HDU 2660 Accepted Necklace【数值型DFS】
Accepted Necklace
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2474 Accepted Submission(s): 973
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.
2 1
1 1
1 1
3
【题意】:宝石的数目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】的更多相关文章
- HDOJ(HDU).2660 Accepted Necklace (DFS)
HDOJ(HDU).2660 Accepted Necklace (DFS) 点我挑战题目 题意分析 给出一些石头,这些石头都有自身的价值和重量.现在要求从这些石头中选K个石头,求出重量不超过W的这些 ...
- hdu 2660 Accepted Necklace
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2660 Accepted Necklace Description I have N precious ...
- HDU 1015 Safecracker【数值型DFS】
Safecracker Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 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 ...
- 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]; 注意中间一层必须逆序循环 ...
- HDU 1427 速算24点【数值型DFS】
速算24点 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- 1789: [Ahoi2008]Necklace Y型项链
1789: [Ahoi2008]Necklace Y型项链 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 421 Solved: 258[Submit] ...
- [BZOJ1789][BZOJ1830][Ahoi2008]Necklace Y型项链
[BZOJ1789][BZOJ1830][Ahoi2008]Necklace Y型项链 试题描述 欢乐岛上众多新奇的游乐项目让小可可他们玩的非常开心.现在他们正在玩比赛串项链的游戏,谁串的最快就能得到 ...
- Accepted Necklace
Accepted Necklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
随机推荐
- LeetCode--Factorial Trailing Zeroes(注意)
Given an integer n, return the number of trailing zeroes in n!. 问题描述:给出一个正整数n,计算n!结构后面有几个0.要求:在多项式时间 ...
- BZOJ 2226 [Spoj 5971] LCMSum | 数论拆式子
题目: http://www.lydsy.com/JudgeOnline/problem.php?id=2226 题解: 题目要求的是Σn*i/gcd(i,n) i∈[1,n] 把n提出来变成Σi/g ...
- 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 解题报告
P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has ta ...
- 停课day1
一早上只做了一个calculator 还是参照题解,好惭愧 f[1]=0; flag[1]=true; for (int i=2,N=num[n];i<p;i++) { fo ...
- Visaul Studio 常用快捷键动画演示
从本篇文章开始,我将会陆续介绍提高 VS 开发效率的文章,欢迎大家补充~ 在进行代码开发的时候,我们往往会频繁的使用键盘.鼠标进行协作,但是切换使用两种工具会影响到我们的开发速度,如果所有的操作都可以 ...
- js用for of 遍历数组
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 创建Maven项目出现:An internal error occurred during: "Retrieving archetypes:". Java heap space 错误解决办法
首先说明一下网上的方法: 在Eclipse中创建Maven的Web项目时出现错误:An internal error occurred during: "Retrieving archety ...
- powercmd注册码
推荐一个很方便的软件:powercmd 用户名:nzone 注册码:PCMDA-86128-PCMDA-70594 . 下载地址网上很多: http://soft.hao123.com/soft/a ...
- Linux的yum命令——(八)
Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够从指定的服务器自动下载 ...
- windows启动redis服务
参考:https://www.cnblogs.com/M-LittleBird/p/5902850.html 使用python的pip install redis以后还需要下载安装redis安装文件才 ...