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
題目大意:
先輸入一個數字TTT,代表有TTT組測試數據,對于每組測試樣例,第一行輸入三個整數n,w1,w2n,w1,w2n,w1,w2,分別代表有nnn件物品,兩個無人機的載重量分別為w1,w2w1,w2w1,w2,然后第二行輸入nnn個整數,代表nnn件物品的各自的重量,第三行輸入nnn個整數,代表nnn件物品的價值,問這兩個無人機最多能帶多大價值的物品。
解題思路:
直接使用二維背包即可,dp[i][j]dp[i][j]dp[i][j]代表第一個背包載重為iii第二個背包載重為jjj時的最大價值,但直接寫會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_JUDGEfreopen("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;
}