upc 9601 PACKING 一个顶俩背包
upc9601
这个题当初比赛时数据水的一批,比完赛两个hack数据hack掉了所有的ac哈哈哈哈哈,当时把两个背包合成一个就可以过
然后就真一个顶俩背包了
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.
题解
俩01背包,双重选择变为不拿物品i,拿i放背包1,拿i放背包2的三重选择
改一下模板即可,不用氧气优化好像过不了
这个题用STL里面的max函数过不了,不优化也过不了,都会t
#include <stdio.h> #pragma GCC optimize(2)//此题的关键所在! int w[128], v[128]; int dp[1024][1024]; int main() { int t, n, w1, w2, t0 = 1; scanf("%d", &t); while (t--) { scanf("%d%d%d", &n, &w1, &w2); for (int i = 1; i <= n; i++) scanf("%d", &w[i]); for (int i = 1; i <= n; i++) scanf("%d", &v[i]); for (int i = 0; i <= w1; i++) for (int j = 0; j <= w2; j++) dp[i][j] = 0; for (int k = 1; k <= n; k++) for (int 7ff7 i = w1; i >= 0; i--) for (int j = w2; j >= 0; j--) { if (i >= w[k]) dp[i][j] = dp[i][j] > dp[i - w[k]][j] + v[k] ? dp[i][j] : dp[i - w[k]][j] + v[k]; if (j >= w[k]) dp[i][j] = dp[i][j] > dp[i][j - w[k]] + v[k] ? dp[i][j] : dp[i][j - w[k]] + v[k]; } printf("Problem %d: %d\n", t0++, dp[w1][w2]); } return 0; }
- 装箱(背包)问题(Packing Problem)
- 背包DP——upc垃圾陷阱
- HDU 2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 (多重背包模板)
- 背包精讲之——01背包
- Modulo Sum(背包 + STL)
- hdu 1203+2955【背包概率变形】
- 【完全背包】纯洁的买卖
- 01背包 装满 hdu 1079 The Balance
- 01背包代码
- hdu 6082 度度熊与邪恶大魔王(完全背包)
- HDU 1114 Piggy-Bank (dp问题之完全背包问题)
- HDU-2844-Coins(多重背包)
- poj 1276(多重背包)
- [算法与数据结构] - No.14 动态规划之多重背包
- HDU 1171 Big Event in HDU(多重背包)
- 超大背包问题(01背包)
- hdu 1712(分组背包)
- 动态规划☞背包问题(⊙o⊙)…
- HDU 4003 Find Metal Mineral(树形DP+分组背包)
- poj1276--Cash Machine(多重背包判可达)