一道典型的完全背包问题~
完全背包就是把01背包的第二个for循环从小到大循环就好
Piggy-Bank
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 72 Accepted Submission(s) : 31
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
But there is a big problem with piggy-banks. It is not possible to determine how much money is inside. So we might break the pig into pieces only to find out that there is not enough money. Clearly, we want to avoid this unpleasant situation. The only possibility is to weigh the piggy-bank and try to guess how many coins are inside. Assume that we are able to determine the weight of the pig exactly and that we know the weights of all coins of a given currency. Then there is some minimum amount of money in the piggy-bank that we can guarantee. Your task is to find out this worst case and determine the minimum amount of cash inside the piggy-bank. We need your help. No more prematurely broken pigs!
Input
Output
Sample Input
3 10 110 2 1 1 30 50 10 110 2 1 1 50 30 1 6 2 10 3 20 4
Sample Output
The minimum amount of money in the piggy-bank is 60. The minimum amount of money in the piggy-bank is 100. This is impossible.
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
using namespace std;
int money1,money2;
int cas,dp[10015];
int n;
int c; //花费
int w; //重量(价值)
int main()
{
scanf("%d",&cas);
while( cas-- ){
scanf("%d%d",&money1,&money2);
int money = money2-money1;
scanf("%d",&n);
memset( dp , 0x3f , sizeof(dp) );
dp[0] = 0;
for( int i=1 ; i<=n ; i++ ){
scanf("%d%d",&w,&c);
for( int V=c ; V<=money ; V++ )
dp[V] = min( dp[V] , dp[V-c]+w );
}
if( dp[money] < 0x3f3f3f3f )
printf("The minimum amount of money in the piggy-bank is %d.\n",dp[money]);
else
printf("This is impossible.\n");
}
return 0;
}
7824




被折叠的 条评论
为什么被折叠?



