第二十七天:背包问题--(0-1背包,完全背包,多重背包)

T1

采药

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

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

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

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

样例输出:
3

Method 1
//
//  main.cpp
//  BackPack0
//
//  Created by Apple on 2019/9/3.
//  Copyright © 2019 Apple_Lance. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#define INF 0x7fffffff

int max(int a, int b){return a>b?a:b;}

struct E{
    int w;
    int v;
}list[101];

int dp[101][1001];

int main(int argc, const char * argv[]) {
    int s, n;
    while(scanf("%d%d", &s, &n) != EOF){
        //输入
        for(int i = 1;i <= n;i++)
            scanf("%d%d", &list[i].w, &list[i].v);
        //初始状态
        for(int i = 0;i < s;i++)
            dp[0][i] = 0;
        //动态规划
        for(int i = 1;i <= n;i++){
            for(int j = s;j >= list[i].w;j--)
                dp[i][j] = max(dp[i-1][j], dp[i-1][j - list[i].w] + list[i].v);
            for(int j = list[i].w - 1;j >= 0;j--)
                dp[i][j] = dp[i-1][j];
        }
        printf("%d", dp[n][s]);
    }
    return 0;
}
Method 2

发现 d p [ i ] [ j ] dp[i][j] 的转移仅与 d p [ i 1 ] [ j l i s t [ i ] . w ] dp[i-1][j-list[i].w] d p [ i 1 ] [ j ] dp[i-1][j] 有关,即仅与二维数组中本行的上一行有关

//
//  main.cpp
//  BackPack1
//
//  Created by Apple on 2019/9/3.
//  Copyright © 2019 Apple_Lance. All rights reserved.
//

#include <iostream>
#include <stdio.h>
using namespace std;
#define INF 0x7fffffff

int max(int a, int b){return  a>b?a:b;}

struct E{
    int w;
    int v;
}list[101];
int dp[1001];

int main(int argc, const char * argv[]) {
    int s, n;
    while (scanf("%d%d", &s, &n) != EOF) {
        for(int i = 1;i <= n;i++)
            scanf("%d%d", &list[i].w, &list[i].v);
        for(int j = 0;j <= s;j++)
            dp[j] = 0;
        for(int i = 1;i <= n;i ++){
            for(int j = s;j >= list[i].w;j--)
                dp[j] = max(dp[j], dp[j - list[i].w] + list[i].v);
        }
        printf("%d\n", dp[s]);
    }
    return 0;
}

T2

Piggy-Bank
时间限制:1 秒
内存限制:32 兆
特殊判题:否
题目描述: 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 pigågy-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.

//
//  main.cpp
//  TotalBackPack
//
//  Created by Apple on 2019/9/3.
//  Copyright © 2019 Apple_Lance. All rights reserved.
//

#include <iostream>
#include <stdio.h>
using namespace std;
#define INF 0x7fffffff

int min(int a, int b){return a<b?a:b;}

struct E{
    int w;
    int v;
}list[501];

int dp[10001];

int main(int argc, const char * argv[]) {
    // insert code here...
    int T;
    scanf("%d", &T);
    while(T--){
        int s, tmp;
        scanf("%d%d", &tmp, &s);
        s -= tmp;
        int n;
        scanf("%d", &n);
        for(int i = 1;i <= n;i++)
            scanf("%d%d", &list[i].v, &list[i].w);
        for(int i = 0;i <= s;i++)
            dp[i] = INF;
        dp[0] = 0;
        for(int i = 1;i <= n;i++)
            for(int j = list[i].w;j<=s;j++)
                if(dp[j - list[i].w] != INF)
                    dp[j] = min(dp[j], dp[j - list[i].w] + list[i].v);
        if(dp[s] != INF)  //若存在一种方案使背包恰好装满, 输出其最小值
            printf ("The minimum amount of money in the piggy-bank is %d.\n", dp[s]);
        else // 若不存在方案
            puts("This is impossible.");
                    
    }
    return 0;
}

T3

珍惜现在,感恩生活
时间限制:1 秒
内存限制:32 兆
特殊判题:否

题目描述: 为了挽救灾区同胞的生命, 心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金 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

//
//  main.cpp
//  Multi-BackPack
//
//  Created by Apple on 2019/9/3.
//  Copyright © 2019 Apple_Lance. All rights reserved.
//

#include <iostream>
#include <stdio.h>
using namespace std;

int max(int a, int b){return a>b?a:b;}

struct E{
    int w;
    int v;
}list[2001];
int dp[101];

int main(int argc, const char * argv[]) {
    int T;
    scanf("%d", &T);
    while(T--){
        int s, n;
        scanf("%d%d", &s, &n);
        int cnt = 0;
        for(int i = 1;i <= n;i++){
            int w, v, k;
            scanf("%d%d%d", &w, &v, &k);
            int c = 1;
            while(k - c > 0){
                k -= c;
                list[++cnt].w = c * w;
                list[cnt].v = c * v;
                c *= 2;
            }
            list[++cnt].w = w * k;
            list[cnt].v = v * k;
        }
        for(int i = 1;i <= cnt;i++)
            for(int j = s;j >= list[i].w;j--)
                dp[j] = max(dp[j], dp[j - list[i].w] + list[i].v);
        printf("%d\n", dp[s]);
    }
    return 0;
}
发布了182 篇原创文章 · 获赞 101 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/lancecrazy/article/details/100523587
今日推荐