【背包问题】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[提交][状态][讨论版] 题目描述 张琪曼:“为什么背包一定要完全装满呢?尽可能 ...
随机推荐
- gitlab 配置.ssh实现免密登陆
首次配置gitlab的.ssh时 安装gitbash 通过gitbash 配置.ssh 打开gitbash,输入如下命令生成ssh,邮箱换成自己的 ssh-keygen -t rsa -C " ...
- Node解析之----模块机制篇
开篇前,我们先来看张图, 看node与W3C组织.CommonJS组织.ECMAScript之间的关系. Node借鉴来CommonJS的Modules规范实现了一套非常易用的模块系统,NPM对Pac ...
- 图论——最小生成树:Prim算法及优化、Kruskal算法,及时间复杂度比较
最小生成树: 一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的最少的边.简单来说就是有且仅有n个点n-1条边的连通图. 而最小生成树就是最小权 ...
- 6.3 MRUnit写Mapper和Reduce的单元测试
1.1 MRUnit写单元测试 作用:一旦MapReduce项目提交到集群之后,若是出现问题是很难定位和修改的,只能通过打印日志的方式进行筛选.又如果数据和项目较大时,修改起来则更加麻烦.所以,在将 ...
- arcgis python 标注
import arcpy mxd = arcpy.mapping.MapDocument("current") lyr = arcpy.mapping.ListLayers(mxd ...
- 《Flutter 实战》开源电子书
<Flutter 实战>开源电子书 转 https://blog.csdn.net/OQjya206rsQ71/article/details/86619630 关于 Flutter ...
- Flex 弹性盒子布局使用教程
本文转载于<https://blog.csdn.net/lyznice/article/details/53981062>
- 10 Flutter仿京东商城项目 商品分类跳转到商品列表传值 商品列表页面布局
pages下面新建: ProductList.dart import 'package:flutter/material.dart'; import '../services/ScreenAdaper ...
- 用ConfigMap管理配置(10)
一.ConfigMap介绍管理配置: ConfigMap介绍 Secret 可以为 Pod 提供密码.Token.私钥等敏感数据:对于一些非敏感数据,比如应用的配置信息,则可以用 ConfigMa ...
- Apache设置静态文件的失效时间
步骤1:启用expires模块 [root@zlinux logs]# vim httpd.conf LoadModule expires_module modules/mod_expires.so ...