链接:https://ac.nowcoder.com/acm/contest/1077/B
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
Farmer John’s cows love their video games! FJ noticed that after playing these games that his cows produced much more milk than usual, surely because contented cows make more milk.
The cows disagree, though, on which is the best game console. One cow wanted to buy the Xbox 360 to play Halo 3; another wanted to buy the Nintendo Wii to play Super Smash Brothers Brawl; a third wanted to play Metal Gear Solid 4 on the PlayStation 3. FJ wants to purchase the set of game consoles (no more than one each) and games (no more than one each – and within the constraints of a given budget) that helps his cows produce the most milk and thus nourish the most children.
FJ researched N (1 <= N <= 50) consoles, each with a console price Pi (1 <= Pi <= 1000) and a number of console-specific games Gi (1 <= Gi <= 10). A cow must, of course, own a console before she can buy any game that is specific to that console. Each individual game has a game price GPj (1 <= GPj price <= 100) and a production value (1 <= PVj <= 1,000,000), which indicates how much milk a cow will produce after playing the game. Lastly, Farmer John has a budget V (1 <= V <= 100,000) which is the maximum amount of money he can spend. Help him maximize the sum of the production values of the games he buys.
Consider one dataset with N=3 consoles and a V=$800 budget. The first console costs $300 and has 2 games with cost $30 and $25 and production values as shown:
Game # Cost Production Value
1 $30 50
2 $25 80
The second console costs $600 and has only 1 game:
Game # Cost Production Value
1 $50 130
The third console costs $400 and has 3 games:
Game # Cost Production Value
1 $40 70
2 $30 40
3 $35 60
Farmer John should buy consoles 1 and 3, game 2 for console 1, and games 1 and 3 for console 3 to maximize his expected production at 210:
Production Value
Budget: $800
Console 1 -$300
Game 2 -$25 80
Console 3 -$400
Game 1 -$40 70
Game 3 -$35 60
-------------------------------------------
Total: 0 (>= 0) 210
输入描述:
-
Line 1: Two space-separated integers: N and V
-
Lines 2…N+1: Line i+1 describes the price of and the games ?available for console i; it contains: Pi, Gi, and Gi pairs of space-separated integers GPj, PVj
输出描述: -
Line 1: The maximum production value that Farmer John can get with his budget.
示例1
输入
复制3 800
300 2 30 50 25 80
600 1 50 130
400 3 40 70 30 40 35 60
输出
复制
210
/*
有依赖的背包:
先把一组看成一个整体,对每组做01背包
然后再对每组里面的物品做01背包
*/
代码1:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5+5;
int dp[52][MAXN];
int main()
{
int N,W;
cin>>N>>W;
int wi,n,w,v;
for(int i = 1; i <= N; i++)
{
cin>>wi>>n;
for(int j = wi; j <= W; j++)//假设要选这组物品,买这个盒子,获得收益为0
{
dp[i][j] = dp[i-1][j-wi] + 0;
}
while(n--)
{
cin>>w>>v;
for(int j = W; j>= w+wi; j--)//此时买了盒子,里面的东西可以随便选
{
dp[i][j] = max(dp[i][j],dp[i][j-w]+v);
}
}
for(int j = 0; j <= W; j++) //也可以不选这组物品
{
dp[i][j] = max(dp[i][j],dp[i-1][j]);
}
}
cout<<dp[N][W]<<endl;
return 0;
}
代码2:
滚动数组空间优化,AC_code:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5+5;
int dp[2][MAXN];
int main()
{
int N,W;
cin>>N>>W;
int wi,n,w,v;
for(int i = 1; i <= N; i++)
{
cin>>wi>>n;
for(int j = wi; j <= W; j++)//假设要选这组物品
{
dp[i&1][j] = dp[(i-1)&1][j-wi] + 0;
}
while(n--)
{
cin>>w>>v;
for(int j = W; j>= w+wi; j--) //因为买了这个盒子,里面的东西可以随便选
{
dp[i&1][j] = max(dp[i&1][j],dp[i&1][j-w]+v);
}
}
for(int j = 0; j <= W; j++) //也可以不选这组物品
{
dp[i&1][j] = max(dp[i&1][j],dp[(i-1)&1][j]);
}
}
cout<<dp[N&1][W]<<endl;
return 0;
}