关于【背包问题】

采药(0-1背包)

题目描述:

辰辰是个很有潜能、天资聪颖的孩子,他的梦想是称为世界上最伟大的医师。
为此,他想拜附近最有威望的医师为师。医师为了判断他的资质,给他出了一个难题。
医师把他带到个到处都是草药的山洞里对他说:
“孩子,这个山洞里有一些不同的草药,采每一株都需要一些时间,每一株也有它自身的价值。
我会给你一段时间,在这段时间里,你可以采到一些草药。如果你是一个聪明的孩子,你应该可以让采到的草药的总价值最大。”
如果你是辰辰,你能完成这个任务吗?

输入:

输入的第一行有两个整数T(1 <= T <= 1000)和M(1 <= M <= 100),T代表总共能够用来采药的时间,M代表山洞里的草药的数目。
接下来的M行每行包括两个在1到100之间(包括1和100)的的整数,分别表示采摘某株草药的时间和这株草药的价值。

输出:

可能有多组测试数据,对于每组数据,
输出只包括一行,这一行只包含一个整数,表示在规定的时间内,可以采到的草药的最大总价值。 

样例输入:
70 3
71 100
69 1
1 2
样例输出:
3

Code:
#include <iostream>
 
using namespace std;
 
struct medicine{
    int weight;
    int value;
};
 
int maxVal(int a,int b){
    return a>b?a:b;
}
 
int main()
{
    int T,M;
    medicine arr[110];
    int dp[110][1010];
    while(cin>>T>>M){
        for(int cnt=1;cnt<=M;++cnt){
            cin>>arr[cnt].weight>>arr[cnt].value;
        }
        for(int cnt=0;cnt<=T;++cnt)
            dp[0][cnt]=0;
        for(int i=1;i<=M;++i){
            for(int j=T;j>=arr[i].weight;--j){
                dp[i][j]=maxVal(dp[i-1][j],dp[i-1][j-arr[i].weight]+arr[i].value);
            }
            for(int j=arr[i].weight-1;j>=0;--j){
                dp[i][j]=dp[i-1][j];
            }
        }
        cout<<dp[M][T]<<endl;
    }
    return 0;
}
 
/**************************************************************
    Problem: 1123
    User: lcyvino
    Language: C++
    Result: Accepted
    Time:140 ms
    Memory:1884 kb
****************************************************************/

 

可以在空间上更优化:

Code:
#include <iostream>
 
using namespace std;
 
struct medicine{
    int weight;
    int value;
};
 
int maxVal(int a,int b){
    return a>b?a:b;
}
 
int main()
{
    int dp[1010];
    medicine arr[110];
    int T,M;
    while(cin>>T>>M){
        for(int cnt=1;cnt<=M;++cnt)
            cin>>arr[cnt].weight>>arr[cnt].value;
        for(int cnt=0;cnt<=T;++cnt)
            dp[cnt]=0;
        for(int i=1;i<=M;++i){
            for(int j=T;j>=arr[i].weight;--j){
                dp[j]=maxVal(dp[j],dp[j-arr[i].weight]+arr[i].value);
            }
        }
        cout<<dp[T]<<endl;
    }
    return 0;
}
 
 
 
/**************************************************************
    Problem: 1123
    User: lcyvino
    Language: C++
    Result: Accepted
    Time:130 ms
    Memory:1520 kb
****************************************************************/

 Piggy-Bank(完全背包)

题目描述:

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!

输入:

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.

输出:

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

样例输入:
3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4
样例输出:
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.

Code:
#include <iostream>
#define INF 0x7fffffff
 
using namespace std;
 
struct Coin{
    int weight;
    int value;
};
 
int minVal(int a,int b){
    return a<b?a:b;
}
 
int main(){
    const int arrSize=10010;
    Coin arr[arrSize];
    int dp[arrSize];
    int T;
    cin>>T;
    while(T--){
        int temp,s;
        cin>>temp>>s;
        s-=temp;
        int n;
        cin>>n;
        for(int i=1;i<=n;++i){
            cin>>arr[i].value>>arr[i].weight;
        }
        for(int i=1;i<=s;++i){
            dp[i]=INF;
        }
        dp[0]=0;
        for(int i=1;i<=n;++i){
            for(int j=arr[i].weight;j<=s;++j){
                if(dp[j-arr[i].weight]!=INF){
                    dp[j]=minVal(dp[j],dp[j-arr[i].weight]+arr[i].value);
                }
            }
        }
        if(dp[s]!=INF){
            cout<<"The minimum amount of money in the piggy-bank is "<< dp[s]<<"."<<endl;
        }else{
            cout<<"This is impossible."<<endl;
        }
    }
    return 0;
}
/**************************************************************
    Problem: 1454
    User: lcyvino
    Language: C++
    Result: Accepted
    Time:70 ms
    Memory:1564 kb
****************************************************************/

 

珍惜现在,感恩生活(多重背包)

题目描述:

为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品,其价格不等,并且只能整袋购买。请问:你用有限的资金最多能采购多少公斤粮食呢?

输入:

输入数据首先包含一个正整数C,表示有C组测试用例,每组测试用例的第一行是两个整数n和m(1<=n<=100, 1<=m<=100),分别表示经费的金额和大米的种类,然后是m行数据,每行包含3个数p,h和c(1<=p<=20,1<=h<=200,1<=c<=20),分别表示每袋的价格、每袋的重量以及对应种类大米的袋数。

输出:

对于每组测试数据,请输出能够购买大米的最多重量,你可以假设经费买不光所有的大米,并且经费你可以不用完。每个实例的输出占一行。

样例输入:
1
8 2
2 100 4
4 100 2
样例输出:
400

Code:
#include <iostream>
 
using namespace std;
 
struct goods{
    int weight;  //价格
    int value; //重量
};
 
int maxVal(int a,int b){
    return a>b?a:b;
}
 
int main()
{
    int N;
    int n,m;
    int weight,value,k;
    const int arrSize=100000;
    goods arr[arrSize];
    int dp[arrSize];
    while(cin>>N){
        while(N--){
            cin>>n>>m;   //n为经费的金额,m为大米的种类
            int cnt=0;
            for(int i=1;i<=m;++i){
                cin>>weight>>value>>k;
                int c=1;
                while(k-c>0){
                    k=k-c;
                    arr[++cnt].value=value*c;
                    arr[cnt].weight=weight*c;
                    c=c*2;
                }
                arr[++cnt].value=k*value;
                arr[cnt].weight=k*weight;
            }
            for(int i=0;i<=n;++i){
                dp[i]=0;
            }
            for(int i=1;i<=cnt;++i){
                for(int j=n;j>=arr[i].weight;--j){
                    dp[j]=maxVal(dp[j],dp[j-arr[i].weight]+arr[i].value);
                }
            }
            cout<<dp[n]<<endl;
        }
    }
    return 0;
}
 
/**************************************************************
    Problem: 1455
    User: lcyvino
    Language: C++
    Result: Accepted
    Time:10 ms
    Memory:2612 kb
****************************************************************/

 

posted @ 2014-12-28 12:38  tinylcy  阅读(195)  评论(0编辑  收藏  举报