PACKING【二维01背包】

PACKING
时间限制: 1 Sec 内存限制: 128 MB
提交: 278 解决: 24
[提交] [状态] [命题人:admin]
题目描述
It was bound to happen. Modernisation has reached the North Pole. Faced with escalating costs for feeding Santa Claus and the reindeer, and serious difficulties with security, NP Management has decided to do away with the traditional sleigh and adopt delivery by drone (magic, superfast drone).
Lack of investment capital means that the new system will start small, and hopefully grow in the years to come. For the first test run in 2017 there will be only two drones and they will have limited carrying capacity. PR is, of course, all important. There will be disappointment, and NP Management has decided to focus on delivering only the most expensive toys to the richest children, so as to focus the worst of the disappointment on those who have the greatest experience of coping (the poor).
Choosing the presents to deliver is your problem. You are being asked to develop an algorithm to select the cargo to deliver, given weight limits for each of the drones and a list of candidate presents with weights and values. Your goal is to maximise the value of gifts delivered.

输入
Input will consist of a series of problems. The first line of the input holds a single integer P being the number of problems. Then for each problem there will be three lines of input. The first line holds three integers: N (1 <= N <= 100) being the number of candidate presents; W1 and W2 (1 <= W1, W2 <= 1000) being the weight limits of the two drones respectively. The second line holds N integers (1 <= wi <= 100) being the weights of each of the candidate presents and the third line holds N integers (1 <= vi <= 100) being the values of the presents (in thousand dollar units). All lines are formatted with single spaces between numbers and no leading or trailing spaces.

输出
For each problem your program should output one line with the text “Problem “ and the number of the problem (counting from 1) followed by a colon, a space and the total value of presents shipped by the drone pair.

样例输入
复制样例数据
2
4 9 4
3 4 5 6
5 7 9 10
6 9 11
3 4 5 6 3 4
2 3 4 5 3 3
样例输出
Problem 1: 22
Problem 2: 16

题目大意:
先输入一个数字 T T T,代表有 T T T组测试数据,对于每组测试样例,第一行输入三个整数 n , w 1 , w 2 n,w1,w2 n,w1,w2,分别代表有 n n n件物品,两个无人机的载重量分别为 w 1 , w 2 w1,w2 w1,w2,然后第二行输入 n n n个整数,代表 n n n件物品的各自的重量,第三行输入 n n n个整数,代表 n n n件物品的价值,问这两个无人机最多能带多大价值的物品。

解题思路:
直接使用二维背包即可, d p [ i ] [ j ] dp[i][j] dp[i][j]代表第一个背包载重为 i i i第二个背包载重为 j j j时的最大价值,但直接写会T掉,所以需要加一句O3优化,就是加一句头文件,博主也不懂为什么。。。。,反正这样就能过,学到了学到了。

代码:

#pragma GCC optimize(3,"Ofast","inline")
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
int dp[2100][2100];
int wi[120],vi[120];
int main() 
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif
    //freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(0),cin.tie(0);
    int T;
    cin>>T;
    rep(t,1,T) {
    	ms(dp);
    	int n,w1,w2;
    	cin>>n>>w1>>w2;
    	rep(i,1,n) cin>>vi[i];
    	rep(i,1,n) cin>>wi[i];
    	rep(i,1,n) {
    		for(int j=w1;j>=0;j--) {
                for(int k=w2;k>=0;k--) {
                    if(j>=vi[i]) {
                        dp[j][k]=max(dp[j][k],dp[j-vi[i]][k]+wi[i]);
                    }
                    if(k>=vi[i]) {
                        dp[j][k]=max(dp[j][k],dp[j][k-vi[i]]+wi[i]);
                    }
                }
    		}
    	}
        cout<<"Problem "<<t<<": "<<dp[w1][w2]<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值