0-1背包 系列问题

hdu 1203

I NEED A OFFER!Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u

Description

Speakless很早就想出国,现在他已经考完了所有需要的考试,准备了所有要准备的材料,于是,便需要去申请学校了。要申请国外的任何大学,你都要交纳一定的申请费用,这可是很惊人的。Speakless没有多少钱,总共只攒了n万美元。他将在m个学校中选择若干的(当然要在他的经济承受范围内)。每个学校都有不同的申请费用a(万美元),并且Speakless估计了他得到这个学校offer的可能性b。不同学校之间是否得到offer不会互相影响。“I NEED A OFFER”,他大叫一声。帮帮这个可怜的人吧,帮助他计算一下,他可以收到至少一份offer的最大概率。(如果Speakless选择了多个学校,得到任意一个学校的offer都可以)。
 

Input

输入有若干组数据,每组数据的第一行有两个正整数n,m(0<=n<=10000,0<=m<=1000)
后面的m行,每行都有两个数据ai(整型),bi(实型)分别表示第i个学校的申请费用和可能拿到offer的概率。
输入的最后有两个0。
 

Output

每组数据都对应一个输出,表示Speakless可能得到至少一份offer的最大概率。用百分数表示,精确到小数点后一位。
 

Sample Input

10 3 4 0.1 4 0.2 5 0.3 0 0
 

Sample Output

44.0%

Hint

You should use printf("%%") to print a '%'.
 
 
 

 

    该问题化简后及是0-1背包。

    化简:此题从反面求如何申请使成功概率最大。即考虑使失败的概率最小。也就是将申请的总费用限定在n时,怎样申请,使失败的概率最小。

            n为背包的容量,每个学校会失败的概率为价值,要的申请费用为物品体积,怎样放使得价值最小。

 

    做完这道题才意识到背包有两种方法:

               第一种:开两维数组,a[i][j]表示第i个物品背包为j重时,背包的最大值。则a[i][j]=max{a[i-1][j-w[i]]+p[i] , a[i][j], a[i-1][j]};

               第二种:开一维数组,a[i]表示背包为i重时的最大价值。则a[i]=max{a[i-w[j]]+p[j] , a[i]}.

                          这里需要注意的是,第二种因为没有变量标记装到了第几个物品,所以对重量做更新时,需要从最大值开始做起。

               注意:  这里由于第一种要开两维数组,会造成超内存,所以这道题只能用第二种;这也正是第二种的优化的地方。

 

     代码实现:

 

           第一种

 

             

View Code
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string.h>
 4 using namespace std;
 5 double a[1005][10005];
 6 int w[1005];
 7 double p[1005];
 8 int main(){
 9     int n,m;
10     while(cin>>n>>m){
11           if(n==0&&m==0)
12                 break;
13           for(int i=1;i<=m;i++){
14                   scanf("%d%lf",&w[i],&p[i]);
15                   p[i]=1-p[i];
16                   }
17           for(int i=0;i<=m;i++){
18             for(int j=0;j<=n;j++){
19                   a[i][j]=1;
20                   }
21                   }
22           for(int k=1;k<=m;k++){
23                   for(int g=0;g<=n;g++){
24                           if(w[k]<=g){
25                                  if(((a[k-1][g-w[k]]*p[k])<=(a[k][g]))){
26                                             a[k][g]=a[k-1][g-w[k]]*p[k];
27                                             }
28                                  if(a[k][g]>=a[k-1][g])
29                                             a[k][g]=a[k-1][g];
30                                             }
31                           else
32                                  a[k][g]=a[k-1][g];
33                                             }
34                                             }
35           printf("%.1lf%%\n",(1-a[m][n])*100);
36           }
37     return 0;
38 }


          第二种

 

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
double a[10005];
int w[1005];
double p[1005];
int main(){
    int n,m;
    while(cin>>n>>m){
          if(n==0&&m==0)
                break;
          for(int i=1;i<=m;i++){
                  scanf("%d%lf",&w[i],&p[i]);
                  p[i]=1-p[i];
                  }
          for(int i=0;i<=n;i++){
                  a[i]=1;
                  }
          for(int k=1;k<=m;k++){
                  for(int g=n;g>=0;g--){
                          if(w[k]<=g){
                                 if(((a[g-w[k]]*p[k])<=(a[g]))){
                                            a[g]=a[g-w[k]]*p[k];
                                            }
                                            }
                                            }
                                            }
          printf("%.1lf%%\n",(1-a[n])*100);
          }
    return 0;
}

 

 

 

 

 

           以上是每种物品都只能放一次的0-1背包,下题又有稍微的不同,在于其中的物品没有限制个数,也就是说,每种物品可以放任意多次。

 

hdu 1114

 Piggy-BankTime Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u

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.

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

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.
 

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.".
 

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.

 

 

题意:给出存钱罐的原先的质量与装满之后的质量;给出存钱罐中可能存在的钱币的种数,接下来给出各种钱币的价值与重量,求这个装满的存钱罐中钱数的最小值。

      这道题之前说的不同之处在于:物品的个数不限制,可以随意拿,求到限制的重量时的最小价值。

    

思路:

         与上题很相似,但因为有条件不一样,不需要考虑钱币的重复。所以要考虑的子问题只有在背包的质量为 i 时,其中的最大质量为多少。还需要考虑的是存钱罐不能被刚好装

满的话,那么情况就不成立。

 

代码实现:

 

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int a[10005];
int w[505];
int p[505];
int main(){
    int n1,n2;
    int m;
    cin>>m;
    while(m--){
       scanf("%d%d",&n1,&n2);
       for(int g=0;g<=(n2-n1);g++){
               a[g]=1000000000;
               }
        int n;
        scanf("%d",&n);
        int minn=1000000000;
        for(int i=1;i<=n;i++){
               scanf("%d%d",&p[i],&w[i]);
               if(minn>w[i])
                   minn=w[i];
               }
        a[0]=0;
        for(int j=minn;j<=(n2-n1);j++){                  //这儿和普通的 0-1 背包不同 
                for(int k=1;k<=n;k++){
                        if(w[k]<=j){
                              if(a[j-w[k]]+p[k]<a[j]){
                                         a[j]=a[j-w[k]]+p[k];
                                         }
                                         }
                                         }
                                         }
       if(a[n2-n1]==1000000000)
               cout<<"This is impossible."<<endl;
       else
               cout<<"The minimum amount of money in the piggy-bank is "<<a[n2-n1]<<"."<<endl;
               }
      return 0;
      }  

 

posted on 2012-09-21 11:20  yumao  阅读(213)  评论(0编辑  收藏  举报

导航