题目描述

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

 
【题解】:
利用两个背包处理,放和不放,开两维分别表示两个不同的背包的容量。
 
 
 import java.math.BigInteger;
import java.util.Scanner; import javax.crypto.CipherInputStream; public class Main{ public static void main(String[] args) {
Scanner cin = new Scanner(System.in); int T = cin.nextInt();
for(int cas=1;cas<=T;cas++)
{
int[][] dp = new int[1010][1010];
int []v = new int[110];
int []w = new int[110];
int n = cin.nextInt();
int V1 = cin.nextInt();
int V2 = cin.nextInt();
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++) dp[i][j] = -1;
dp[0][0] = 0;
for(int i=0;i<n;i++) w[i] = cin.nextInt();
for(int i=0;i<n;i++) v[i] = cin.nextInt(); for(int i=0;i<n;i++)
{
for(int j=V1;j>=0;j--)
{
for(int k=V2;k>=0;k--)
{
if(k>=w[i]) dp[j][k] = Math.max(dp[j][k],dp[j][k-w[i]]+v[i]);
if(j>=w[i]) dp[j][k] = Math.max(dp[j][k],dp[j-w[i]][k]+v[i]);
}
}
}
int Ans = 0;
for(int i=0;i<=V1;i++)
for(int j=0;j<=V2;j++)
Ans = Math.max(Ans, dp[i][j]);
System.out.println("Problem "+cas+": "+Ans); } }
}

JAVA——代码

最新文章

  1. 使用Rest访问Redis中的数据
  2. Text文档编码识别方法
  3. HTML5--页面自动居中
  4. SpringBoot 快速入门
  5. HP 电脑装 纯净版的win7
  6. ASP.NET MVC那些事
  7. 两个EXCEL文件对比去重
  8. Angularjs 使用filter格式化输出href
  9. POJ 3928 Ping pong
  10. CentOS 7 安装 mariaDB
  11. Code Snippet Library
  12. tomcat各目录(文件)作用
  13. 将home多余的空间分配到&quot;/&quot;分区下
  14. Mysql 查询条件中字符串尾部有空格也能匹配上的问题
  15. Spring MVC详解
  16. mysql 表分区技术
  17. Linux 软件安装到 /usr,/usr/local/ 还是 /opt 目录?
  18. 1-添加自己的Lua执行函数(ESP8266-SDK开发(lua版本))
  19. opencv学习之路(18)、霍夫变换
  20. 在Linux系统下mail命令的用法

热门文章

  1. white-space 标签 使用
  2. php操作成功返回当前页并刷新
  3. c++ Container print
  4. 关于对VGA、DVI、HDMI的区别
  5. 仙剑奇侠传1系列:2.编译主程序SDLPAL及SDL
  6. iOS自定义下拉列表
  7. pch文件的添加
  8. 数据集成、变换、归约及相关MATLAB工具箱函数
  9. Docker三
  10. selenium3关于ddt驱动之读取json文件。。。