博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU-2844 Coins(多重背包)
阅读量:6574 次
发布时间:2019-06-24

本文共 3386 字,大约阅读时间需要 11 分钟。

Problem Description
Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hibix opened purse and found there were some coins. He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn't know the exact price of the watch.
You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.
 
Input
The input contains several test cases. The first line of each test case contains two integers n(1 ≤ n ≤ 100),m(m ≤ 100000).The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn (1 ≤ Ai ≤ 100000,1 ≤ Ci ≤ 1000). The last test case is followed by two zeros.
 
Output
For each test case output the answer on a single line.
 

 

Sample Input
3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0
Sample Output
8
4
 
题目就是让你用所给的 种类一定,数目一定的硬币,看能组成的数字有那先(当然,询问范围是1 ~ m)
还是列出已知条件,硬币的种类,每类的个数,查询范围(1 ~ m)
在多重背包里,我们用到的条件有:背包容量,物品种类,物品每类的数量, 物品每类所用的体积大小
抽象这道题目,我们直观的知道,硬币的种类,硬币每类的数量,每类硬币的面值。题目里只有这 3 个条件, 做背包问题一定会涉及“物品占用体积的大小”,而这道题完全没有提 ,因为我们最后求的是所能组成面值的总数。 这里提一下,我们的面值,既可以当作物品的重量,又可以当作物品占的体积
 
1 #include 
2 #include
3 #include
4 #include
5 6 using namespace std; 7 const int max_size = 1000000 + 10; 8 const int MAX = 105; 9 int dp[max_size];10 bool vis[max_size];11 12 13 14 int main()15 {16 //1.将问题的模型抽象出来17 int cnt, vol;18 int val[MAX];19 int num[MAX];20 while(scanf("%d %d", &cnt, &vol) != EOF)21 {22 memset(dp, 0, sizeof(dp));23 memset(vis, false, sizeof(vis));24 if(cnt == 0 && vol == 0)25 break;26 for(int i = 0; i < cnt; i++)27 scanf("%d", val+i);28 for(int i = 0; i < cnt; i++)29 scanf("%d", num+i);30 31 for(int i = 0; i < cnt; i++)32 {33 if(val[i] * num[i] >= vol)34 {35 //CompletePack(val[i], val[i]); //那么多的价值,那么多的占用?36 37 for(int j = val[i]; j <= vol; j++) ///多重背包这里错了两次了,要注意,昨天找了一晚上38 {39 dp[j] = max(dp[j], dp[j - val[i]] + val[i]);40 vis[dp[j]] = true;41 }42 continue;43 }44 int k = 1;45 while(k < num[i])46 {47 //ZeroOnePack(k*val[i], k*val[i]);48 for(int j = vol; j - k * val[i] >= 0; j--)49 {50 dp[j] = max(dp[j], dp[j-k*val[i]] + k*val[i]);51 vis[dp[j]] = true;52 }53 num[i] -= k;54 k *= 2;55 }56 //ZeroOnePack(num[i]*val[i], num[i]*val[i]);57 for(int j = vol; j - num[i]*val[i] >= 0; j--)58 {59 dp[j] = max(dp[j], dp[j - num[i]*val[i]] + num[i]*val[i]);60 vis[dp[j]] = true;61 }62 }63 64 int ans = 0;65 for(int i = 1; i <= vol; i++)66 {67 ans += (vis[i] == true) ? 1 : 0;68 }69 printf("%d\n", ans);70 }71 return 0;72 }
View Code

 

转载于:https://www.cnblogs.com/ya-cpp/p/4337701.html

你可能感兴趣的文章
2015年2月3日
查看>>
LI 导航
查看>>
交流:Ghost版系统安装简单分析
查看>>
简单的jquery代码实现图片轮播
查看>>
IDEA的常用配置一键导入及优化内存
查看>>
keytool 错误 java.io.IOException: incorrect AVA format
查看>>
$.ajax()方法详解(转)
查看>>
java 冒泡排序
查看>>
【CSS】Table样式
查看>>
Qt Quick编程(1)——QML的核心部分ECMAScript
查看>>
js 替换非法字符
查看>>
(转)C# Winform应用程序占用内存较大解决方法整理
查看>>
win10下安装mysql5.6 zip形式步骤
查看>>
Shell:while语句、for语句、if语句
查看>>
HTTP缓存原理及相关知识(2)-CDN
查看>>
eclipse代码编辑区字符串自动转义设置
查看>>
思考XSS攻击和跨站伪造请求CSRF
查看>>
实验四恶意代码分析技术 201421430029
查看>>
神一样的代码:
查看>>
跟KingDZ学HTML5之八 HTML5之Web Save
查看>>