J: 超市购物
Description
上次去超市扫荡回来的东西用完了,Staginner又得跑超市一趟,出发前他列了一张购物清单,打算去买K种不同的商品,每种买一件。到了超市,Staginner发现每种商品有N个品牌,每个品牌此商品的价格为Vi,对Staginner的作用值为Wi,他会从这N个品牌里面挑一个品牌买。这时,Staginner突然想起出门时只带了M元钱,又懒得去取钱了,所以不一定能买完K种商品,只好尽可能地让买的东西对自己的总作用值ans最大。
Input
多组样例。
第一行两个整数K,M代表Staginner想买的不同种类商品的数目和他带的钱 (0 < K <= 30, 0 < M <= 2000)
以下输入分为K个部分,代表K种商品。
每个部分第一行为一个数字N,代表第k种商品的N个品牌,N不大于10。之后跟着N行,每行两个数字,代表物品的价格Vi和作用值Wi。其中 0 < Vi < M。
Output
输出Case #: 最大总作用值,每两个样例之间有一个空行。
Sample Input
3 100
3
50 600
20 700
30 800
2
30 500
40 600
1
60 200
2 500
2
200 1000
260 1200
1
280 300
Sample Output
Case 1: 1400
Case 2: 1300
裸的分组背包问题,记得注意输出格式
背包问题建议看看《背包九讲》,写自己模板的话我推荐写一下HihoCoder的奖券兑换,裸的多重背包题
#include <bits/stdc++.h>
#define N 10100
#define INF 0x3f3f3f3f
#define LL long long
#define mem(a,n) memset(a,n,sizeof(a))
#define fread freopen("in.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
int cost[33][11],amount[33],val[33][11],dp[2222];
int main()
{
ios::sync_with_stdio(false);
int kase=0,k,mon;
while(cin>>k>>mon){
if(kase){
cout<<endl;
}
mem(dp,0);
for(int i=0;i<k;++i){
cin>>amount[i];
for(int j=0;j<amount[i];++j){
cin>>cost[i][j]>>val[i][j];
}
}
for(int i=0;i<k;++i){
for(int j=mon;j>=0;--j){
for(int l=0;l<amount[i];++l){
if(j>=cost[i][l]){
dp[j]=max(dp[j],dp[j-cost[i][l]]+val[i][l]);
}
}
}
}
cout<<"Case "<<++kase<<": "<<dp[mon]<<endl;
}
return 0;
}
/**********************************************************************
Problem: 1086
User: CSUzick
Language: C++
Result: AC
Time:48 ms
Memory:1700 kb
**********************************************************************/