题目描述

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. flask中用类的方式构造视图函数

    from flask import Flask from flask.views import MethodView app = Flask(__name__) class IndexView(Met ...

  2. httpd Apache服务

    TCP/IP协议 跨Internet的主机间通讯 在建立通信连接的每一端,进程间的传输要有两个标志: IP地址和端口号,合称为套接字地址 socket address 客户机套接字地址定义了一个唯一的 ...

  3. go -- application/x-www-form-urlencoded发送post数据

  4. 用JS判断号码

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Android控件RecyclerView的基本用法

    Android控件RecyclerView的基本用法 转 https://www.jianshu.com/p/e71a4b73098f   github: https://github.com/Cym ...

  6. java引用如果是成员变量则引用本身不保存在栈上的汇编级调试证明

    很久很久没有更新博客了,因为发生太多太多猝不及防的事情,再加上自己本身也特别忙,这里补上一直想发的自己觉得很有意义的一次探索过程. 就是很多java开发人员都曾被误导的一个点——“如果一个变量是引用, ...

  7. dbtreeview

    http://www.delphipages.com/comp/dynamic_dbtreeview-6302.html https://files.cnblogs.com/files/jijm123 ...

  8. XGBoost原理详解

    原文:https://blog.csdn.net/qq_22238533/article/details/79477547

  9. swift 第二课 基础知识-2

    setter 和getter 的使用--> 适合计算使用 struct Point { var x = 0.0, y=0.0 } struct Size { var width = 0.0, h ...

  10. python进阶--多线程多进程

    一.线程和进程 进程是拥有独立内存,能够独立运行的最小单位,也是程序执行的最小单位,线程是程序运行过程中,一个单一的顺序控制流程,是程序执行流的最小单位,一个进程至少包含一个线程,多线程共享进程的内存 ...