题目描述
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.
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
【题解】:
利用两个背包处理,放和不放,开两维分别表示两个不同的背包的容量。
1 import java.math.BigInteger; 2 import java.util.Scanner; 3 4 import javax.crypto.CipherInputStream; 5 6 public class Main{ 7 8 public static void main(String[] args) { 9 Scanner cin = new Scanner(System.in); 10 11 int T = cin.nextInt(); 12 for(int cas=1;cas<=T;cas++) 13 { 14 int[][] dp = new int[1010][1010]; 15 int []v = new int[110]; 16 int []w = new int[110]; 17 int n = cin.nextInt(); 18 int V1 = cin.nextInt(); 19 int V2 = cin.nextInt(); 20 for(int i=1;i<=n;i++) 21 for(int j=1;j<=n;j++) dp[i][j] = -1; 22 dp[0][0] = 0; 23 for(int i=0;i<n;i++) w[i] = cin.nextInt(); 24 for(int i=0;i<n;i++) v[i] = cin.nextInt(); 25 26 for(int i=0;i<n;i++) 27 { 28 for(int j=V1;j>=0;j--) 29 { 30 for(int k=V2;k>=0;k--) 31 { 32 if(k>=w[i]) dp[j][k] = Math.max(dp[j][k],dp[j][k-w[i]]+v[i]); 33 if(j>=w[i]) dp[j][k] = Math.max(dp[j][k],dp[j-w[i]][k]+v[i]); 34 } 35 } 36 } 37 int Ans = 0; 38 for(int i=0;i<=V1;i++) 39 for(int j=0;j<=V2;j++) 40 Ans = Math.max(Ans, dp[i][j]); 41 System.out.println("Problem "+cas+": "+Ans); 42 43 } 44 45 } 46 }