杭电1085(多重背包求解)

题目:

We all know that Bin-Laden is a notorious terrorist,
and he has disappeared for a long time. But recently, it is reported that he
hides in Hang Zhou of China!
“Oh, God! How terrible! ”

Don’t be so
afraid, guys. Although he hides in a cave of Hang Zhou, he dares not to go out.
Laden is so bored recent years that he fling himself into some math problems,
and he said that if anyone can solve his problem, he will give himself up!

Ha-ha! Obviously, Laden is too proud of his intelligence! But, what is his
problem?
“Given some Chinese Coins (硬币) (three kinds-- 1, 2, 5), and their
number is num_1, num_2 and num_5 respectively, please output the minimum value
that you cannot pay with given coins.”
You, super ACMer, should solve the
problem easily, and don’t forget to take $25000000 from Bush!

Input

Input contains multiple test cases. Each test case
contains 3 positive integers num_1, num_2 and num_5 (0<=num_i<=1000). A
test case containing 0 0 0 terminates the input and this test case is not to be
processed.

Output

Output the minimum positive value that one cannot pay
with given coins, one line for one case.

Sample Input

1 1 3

0 0 0

Sample Output

4

思想:

  给你1 2 5价值的钱,数量有限,可以用多重背包问题求解。

算出总的钱数,当做背包容量。相当于给你1你可以买1价值的东西,

给你4你却只能买3价值的东西。所以显然到最后,如果dp[i] < i;说明

所给的钱数不可以购买。

代码:

 1 #include<stdio.h>
2 #include<string.h>
3
4
5 int getmax(int x, int y){
6 return x > y ? x : y;
7 }
8
9 int main(){
10 int sumprice, price[3], num[3001], i, j, cout, dp[8001], k;
11 while(scanf("%d %d %d", &price[0], &price[1], &price[2]) && (price[0] || price[1] || price[2])){
12 cout = 0;
13 k = 0;
14 sumprice = price[0] + 2 * price[1] + 5 * price[2];
15 while(price[0] --){
16 num[cout ++] = 1;
17 }
18 while(price[1] --){
19 num[cout ++] = 2;
20 }
21 while(price[2] --){
22 num[cout ++] = 5;
23 }
24 memset(dp, 0, sizeof(dp));
25 for(i = 0; i < cout; i ++){
26 for(j = sumprice; j >= num[i]; j --){
27 dp[j] = getmax(dp[j], dp[j - num[i]] + num[i]);
28 }
29 }
30 for(i = 1; i <= sumprice; i ++){
31 if(dp[i] < i){
32 printf("%d\n", i);
33 k = 1;
34 break;
35 }
36 }
37 if(k == 0){
38 printf("%d\n", sumprice + 1);
39 }
40 }
41 return 0;
42 }

时间: 2024-11-05 02:19:51

杭电1085(多重背包求解)的相关文章

杭电 1085

Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14670    Accepted Submission(s): 6566 Problem Description We all know that Bin-Laden is a notorious terrorist, and he

HDU 1085 多重背包转化为0-1背包问题

题目大意: 给定一堆1,2,5价值的硬币,给定三个数表示3种价值硬币的数量,任意取,找到一个最小的数无法取到 总价值为M = v[i]*w[i](0<=i<3) 那么在最坏情况下M个数都能取到 , M+1必然取不到 所以给M+1个背包,往里面塞东西,最后由前往后检测,找到第一个无法取满的背包的体积 这道题目里,每一种物品的v*w必然小于M+1,所以不出现完全背包的情况,全部采用多重背包转化为0-1背包解决问题即可 1 #include <cstdio> 2 #include <

Holding Bin-Laden Captive!(杭电1085)(母函数)

Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15501    Accepted Submission(s): 6951 Problem Description We all know that Bin-Laden is a notorious terrorist, and he

POJ3260——The Fewest Coins(多重背包+完全背包)

The Fewest Coins DescriptionFarmer John has gone to town to buy some farm supplies. Being a very efficient man, he always pays for his goods in such a way that the smallest number of coins changes hands, i.e., the number of coins he uses to pay plus

杭电 2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(多重背包)

http://acm.hdu.edu.cn/showproblem.php?pid=2191 悼念512汶川大地震遇难同胞--珍惜现在,感恩生活 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14531    Accepted Submission(s): 6121 Problem Description 急!灾区的食物依然短缺! 为

杭电1171 Big Event in HDU(母函数+多重背包解法)

Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 23728    Accepted Submission(s): 8363 Problem Description Nowadays, we all know that Computer College is the biggest department

杭电 HDU ACM 2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(多重背包)

悼念512汶川大地震遇难同胞--珍惜现在,感恩生活 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 19574    Accepted Submission(s): 8285 Problem Description 急!灾区的食物依然短缺! 为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,

杭电 1248 寒冰王座(完全背包)

http://acm.hdu.edu.cn/showproblem.php?pid=1248 寒冰王座 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10792    Accepted Submission(s): 5476 Problem Description 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票(记住,只有一张钞票

杭电 1203 I NEED A OFFER!(01背包)

http://acm.hdu.edu.cn/showproblem.php?pid=1203 I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15759    Accepted Submission(s): 6259 Problem Description Speakless很早就想出国,现在他已经考完了