SDAU 课程练习3 1018

来源:互联网 发布:包装设计收费 知乎 编辑:程序博客网 时间:2024/05/19 18:12

Problem R

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 10   Accepted Submission(s) : 3
Problem Description
Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member has any small money, he takes all the coins and throws them into a piggy-bank. You know that this process is irreversible, the coins cannot be removed without breaking the pig. After a sufficiently long time, there should be enough cash in the piggy-bank to pay everything that needs to be paid. <br><br>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! <br>
 

Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing two integers E and F. They indicate the weight of an empty pig and of the pig filled with coins. Both weights are given in grams. No pig will weigh more than 10 kg, that means 1 <= E <= F <= 10000. On the second line of each test case, there is an integer number N (1 <= N <= 500) that gives the number of various coins used in the given currency. Following this are exactly N lines, each specifying one coin type. These lines contain two integers each, Pand W (1 <= P <= 50000, 1 <= W <=10000). P is the value of the coin in monetary units, W is it's weight in grams. <br>
 

Output
Print exactly one line of output for each test case. The line must contain the sentence "The minimum amount of money in the piggy-bank is X." where X is the minimum amount of money that can be achieved using coins with the given total weight. If the weight cannot be reached exactly, print a line "This is impossible.". <br>
 

Sample Input
310 11021 130 5010 11021 150 301 6210 320 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.
 
题目大意:

有一个小猪存钱罐。空的存钱罐有一个质量,装满后有一个质量,首先给出存钱罐的两个质量
然后给出钱币的价值和质量。求在质量满足的情况下,可以得到的最小价值。

思路:

一开始是想转化成 0  1 背包来着,但是不难看出,超时显而易见。后来想   价值  1  体积   1  的物品是否可以转化成价值  100   体积  100  的呢?因为最多存钱罐只能放100个体积为 1 的嘛。然后再用 0  1 背包求解。但是事实告诉我  wa   这是个错误的想法。然后接着看了背包九讲,知道了这是一个完全背包问题,只需要把循环的方式改过来就行了。至于为什么?自己在纸上模拟一下。我的理解是:  只要还有体积,那就往背包里面装入物品,这个和  0  1   区别就是这里。0  1  是在  体积剩余v 的时候还可不可以放入这件物品。

感想:

五一假期来了,不过还是要接着刷题。很难过。。

AC代码:

#include <stdio.h>#include<iostream>#include<cstdio>#include<string.h>#include<algorithm>using namespace std;int v[10005];int w[10005];int dp[10005];int main(){    int n,i,j,k;    //freopen("r.txt","r",stdin);    int T,V,V1,V2;    cin>>T;    while(T--)    {        scanf("%d%d",&V1,&V2);        V=V2-V1;        scanf("%d",&n);        for(i=0;i<n;i++)        {            scanf("%d%d",&v[i],&w[i]);        }        for(i=0;i<=V;i++)            dp[i]=666666;        dp[0]=0;        for(i=0;i<n;i++)        {            for(j=w[i];j<=V;j++)            {                dp[j]=min(dp[j],dp[j-w[i]]+v[i]);            }        }        if(dp[V]==666666) cout<<"This is impossible."<<endl;        else            cout<<"The minimum amount of money in the piggy-bank is "<<dp[V]<<"."<<endl;    }}


0 0
原创粉丝点击