LG_2967_[USACO09DEC]視訊遊戲的麻煩Video Game Troubles
題目描述
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 P_i (1 <= P_i <= 1000) and a number of console-specific games G_i (1 <= G_i <= 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 GP_j (1 <= GP_j price <= 100) and a production value (1 <= PV_j <= 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=800budget.Thefirstconsolecosts800 budget. The first console costs 800budget.Thefirstconsolecosts300 and has 2 games with cost 30and30 and 30and25 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
農夫約翰的奶牛們遊戲成癮!本來約翰是想要按照調教獸的做法拿她們去電擊戒癮的,可是 後來他發現奶牛們玩遊戲之後比原先產更多的奶.很明顯,這是因為滿足的牛會產更多的奶.
但是,奶牛們在哪個才是最好的遊戲平臺這個問題上產生了巨大的分歧.約翰想要在給定的 預算內購入一些遊戲平臺和一些遊戲,使他的奶牛們生產最多的奶牛以養育最多的孩子.
約翰研究了N種遊戲平臺,每一種遊戲平臺的價格是Pi 並且每一種遊戲平臺有Gi個只能在這種平臺上執行的遊戲.很明顯,奶牛必須 先買進一種遊戲平臺,才能買進在這種遊戲平臺上執行的遊戲.每一個遊戲有一個遊戲的價 格GPi並且有一個產出值PVj< 1000000),表示一隻牛在玩這個遊戲之後會產出多少牛奶.最後,農夫約翰的預算為V<100000),即他最多可以花費的金錢.請 幫助他確定應該買什麼遊戲平臺和遊戲,使得他能夠獲得的產出值的和最大.
輸入輸出格式
輸入格式
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: P_i, G_i, and G_i pairs of space-separated integers GP_j, PV_j
輸出格式
- Line 1: The maximum production value that Farmer John can get with his budget.
樣例
INPUT
3 800
300 2 30 50 25 80
600 1 50 130
400 3 40 70 30 40 35 60
OUTPUT
210
SOLUTION
多重揹包dp
考場上寫崩掉了,沒事瞎用樹形dp的正在面壁思過中。。。
其實本體的思路應該是非常清晰的,所以這裡重點還是看一下程式碼的實現吧。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
typedef long long LL;
#define Max(a,b) ((a>b)?a:b)
#define Min(a,b) ((a<b)?a:b)
inline int read(){
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9') {x=x*10+ch-48;ch=getchar();}
return x*f;}
const int N=550,M=101000;
short n,m,V,cnt=0,id=0;
int f[2][M],ans=0;
int main(){
int i,j;
n=read();V=read();memset(f,0,sizeof(f));
for (i=1;i<=n;++i){
int p=read(),g=read();
for (j=p;j<=V;++j) f[i&1][j]=f[(i-1)&1][j-p];//先扣去買這個平臺的遊戲的平臺費用
while (g--){
int cst=read(),pdc=read();
for (j=V;j>=cst+p;--j)
f[i&1][j]=Max(f[i&1][j],f[i&1][j-cst]+pdc);//正常的揹包轉移
}
for (j=0;j<=V;++j) f[i&1][j]=Max(f[i&1][j],f[(i-1)&1][j]);//或者我們索性不買這個平臺
}
printf("%d\n",f[n&1][V]);
return 0;
}