题目描述

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——代码

【背包问题】PACKING的更多相关文章

  1. DSY3163*Eden的新背包问题

    Description "寄没有地址的信,这样的情绪有种距离,你放着谁的歌曲,是怎样的心心静,能不能说给我听."失忆的Eden总想努力地回忆起过去,然而总是只能清晰地记得那种思念的 ...

  2. 使用adagio包解决背包问题

    背包问题(Knapsack problem) 背包问题(Knapsack problem)是一种组合优化的多项式复杂程度的非确定性问题(NP问题).问题可以描述为:给定一组物品,每种物品都有自己的重量 ...

  3. bzoj 3163: [Heoi2013]Eden的新背包问题

    Description "寄没有地址的信,这样的情绪有种距离,你放着谁的歌曲,是怎样的心心静,能不能说给我听."失忆的Eden总想努力地回忆起过去,然而总是只能清晰地记得那种思念的 ...

  4. nyoj 106背包问题(贪心专题)

    背包问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 现在有很多物品(它们是可以分割的),我们知道它们每个物品的单位重量的价值v和重量w(1<=v,w< ...

  5. [C++11][算法][穷举]输出背包问题的所有可满足解

    关于背包问题的题目,前人之述备矣,这里只讨论实现 输入: n ca w_1 v_1 w_2 v_2 ... w_n v_n 其中,n是物品总数,ca是背包大小,w_n是第n个物品的重量,v_n是第n个 ...

  6. knapsack problem 背包问题 贪婪算法GA

    knapsack problem 背包问题贪婪算法GA 给点n个物品,第j个物品的重量,价值,背包的容量为.应选哪些物品放入包内使物品总价值最大? 规划模型 max s.t. 贪婪算法(GA) 1.按 ...

  7. NOIP2006金明的预算方案[DP 有依赖的背包问题]

    题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N元钱就行”.今 ...

  8. bzoj2748[HAOI2012]音量调节(背包问题的方案)

    Description 一个吉他手准备参加一场演出.他不喜欢在演出时始终使用同一个音量,所以他决定每一首歌之前他都要改变一次音量.在演出开始之前,他已经做好了一个列表,里面写着在每首歌开始之前他想要改 ...

  9. 【动态规划】简单背包问题II

    问题 B: [动态规划]简单背包问题II 时间限制: 1 Sec  内存限制: 64 MB提交: 21  解决: 14[提交][状态][讨论版] 题目描述 张琪曼:“为什么背包一定要完全装满呢?尽可能 ...

随机推荐

  1. Spring Cloud Gateway(二):Spring Cloud Gateway整合Eureka应用

    Spring Cloud Gateway 应用概述 下面的示例启动两个服务:gataway-server 和 user-service 都注册到注册中心 Eureka上,客户端请求后端服务[user- ...

  2. AE开发之shp转txt

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. linux java -jar

    常见命令1 nohup java -jar /xxx/xxx/xxx.jar >/dev/>& & 分析各个指令代表什么意思: >,重写文件,如果文件里面有内容会覆盖 ...

  4. Kafka - SASL认证

    kafka SASL认证配置 1.找到kafka安装根目录,在config文件夹下创建kafka_server_jaas.conf,写入 KafkaServer { org.apache.kafka. ...

  5. kvm安装及简单使用

    1 cat /etc/redhat-release      CentOS release 6.4 (Final)2 egrep ‘vmx|svm’ /proc/cpuinfo3 yum -y ins ...

  6. Npoi Web 项目中(XSSFWorkbook) 导出出现无法访问已关闭的流

    NPOI生产.xlsx文件件时,在使用book.Write(ms);后,会关闭流,这样导致再次使用Respons输出流的时候就出错了. 造成关闭流的主要原因有时其实是跨域,同域是没有问题的. //新建 ...

  7. StateListDrawable

    可供设置的属性如下: drawable:引用的Drawable位图,我们可以把他放到最前面,就表示组件的正常状态~ state_focused:是否获得焦点 state_window_focused: ...

  8. set serveroutput on 命令

    使用set serveroutput on 命令设置环境变量serveroutput为打开状态,从而使得pl/sql程序能够在SQL*plus中输出结果 使用函数dbms_output.put_lin ...

  9. jExcelAPI 操作 Excel 文件

    在开源世界中,有两套比较有影响的API可 供使用,一个是POI,一个是jExcelAPI.其中功能相对POI比较弱一点.但jExcelAPI对中文支持非常好,API是纯Java的, 并不 依赖Wind ...

  10. ORA-03114: not connected to ORACLE

    PlSql Developer出现这个问题的时候,只要重新连接一些数据库就行了!