【背包问题】PACKING
题目描述
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.
输入
输出
样例输入
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的更多相关文章
- DSY3163*Eden的新背包问题
Description "寄没有地址的信,这样的情绪有种距离,你放着谁的歌曲,是怎样的心心静,能不能说给我听."失忆的Eden总想努力地回忆起过去,然而总是只能清晰地记得那种思念的 ...
- 使用adagio包解决背包问题
背包问题(Knapsack problem) 背包问题(Knapsack problem)是一种组合优化的多项式复杂程度的非确定性问题(NP问题).问题可以描述为:给定一组物品,每种物品都有自己的重量 ...
- bzoj 3163: [Heoi2013]Eden的新背包问题
Description "寄没有地址的信,这样的情绪有种距离,你放着谁的歌曲,是怎样的心心静,能不能说给我听."失忆的Eden总想努力地回忆起过去,然而总是只能清晰地记得那种思念的 ...
- nyoj 106背包问题(贪心专题)
背包问题 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 现在有很多物品(它们是可以分割的),我们知道它们每个物品的单位重量的价值v和重量w(1<=v,w< ...
- [C++11][算法][穷举]输出背包问题的所有可满足解
关于背包问题的题目,前人之述备矣,这里只讨论实现 输入: n ca w_1 v_1 w_2 v_2 ... w_n v_n 其中,n是物品总数,ca是背包大小,w_n是第n个物品的重量,v_n是第n个 ...
- knapsack problem 背包问题 贪婪算法GA
knapsack problem 背包问题贪婪算法GA 给点n个物品,第j个物品的重量,价值,背包的容量为.应选哪些物品放入包内使物品总价值最大? 规划模型 max s.t. 贪婪算法(GA) 1.按 ...
- NOIP2006金明的预算方案[DP 有依赖的背包问题]
题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N元钱就行”.今 ...
- bzoj2748[HAOI2012]音量调节(背包问题的方案)
Description 一个吉他手准备参加一场演出.他不喜欢在演出时始终使用同一个音量,所以他决定每一首歌之前他都要改变一次音量.在演出开始之前,他已经做好了一个列表,里面写着在每首歌开始之前他想要改 ...
- 【动态规划】简单背包问题II
问题 B: [动态规划]简单背包问题II 时间限制: 1 Sec 内存限制: 64 MB提交: 21 解决: 14[提交][状态][讨论版] 题目描述 张琪曼:“为什么背包一定要完全装满呢?尽可能 ...
随机推荐
- MySQL 取分组后每组的最新记录
修改<常用SQL之日期格式化和查询重复数据>中表test1的创建时间,修改后的测试数据如下: 以姓名分组后提取每组最新创建的记录: SELECT a.* FROM test1 AS a, ...
- CentOS6.8安装Docker
在CentOS6.8上安装Docker 1.Docker使用EPEL发布,RHEL系的OS首先要确保已经持有EPEL仓库,否则先检查OS的版本,然后安装相应的EOEL包:如下命令: yum insta ...
- python中heapq对dict进行排序
问题: 想从以下形式的dict中取value最大的2个key-value的key dict_num_num = {0: 0.07374631268436578, 1: 0.16307692307692 ...
- appdomain概念与应用
用appcmd命令显示w3wp.exe进程,很多时候一个应用程序池负责创建一个w3wp.exe进程与之对应,可能还有时候是创建多个w3wp.exe,有待验证. c:\Windows\System32\ ...
- Flutter用dio封装http网络请求,设置统一的请求地址、headers及处理返回内容
封装http请求是项目中经常需要做的,常用于设置通用请求地址.请求headers以及处理返回结果,例如在项目中开发地址.测试地址.上线地址是不一样的,当在封装的请求设置好默认地址之后只需要改一个地址而 ...
- 设计模式----外观(facade)模式
外观(facade)模式外观模式(Facade),为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用.uml图
- Kotlin 中类函数
在kotlin中函数可以在类外部定义也可以在类内部定义,前者即为全局函数,后者,是类成员函数,语法一样 package loaderman.demo class Person { fun demo(n ...
- mongodb MongoDB C#/.NET driver version
The first column lists the driver version(s). C#/.NET Driver Version MongoDB 2.6 MongoDB 3.0 MongoDB ...
- NLP之ROUGE[笔记]
0 前言 [定义]ROUGE:recall-oriented understanding for gisting evaluation,面向召回的要点评估理解 召回率,recall rate;要点,g ...
- [leetcode_easy]558. Quad Tree Intersection
problem 558. Quad Tree Intersection re 1. Leetcode_easy_558. Quad Tree Intersection; 2. Grandyang; e ...