CSU---1086 多维01背包问题[DP]

1086: 超市购物

Time Limit: 1 Sec  Memory Limit: 128 MB
SUBMIT: 153  Solved: 64
[SUBMIT][STATUS]

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

HINT

 

Source

CSU Monthly 2012 Jul.

 

 

 刚开始看了同学的错的代码,后来改的。

code:

 1 #include <iostream>   
 2 #include <iomanip>   
 3 #include <fstream>   
 4 #include <sstream>   
 5 #include <algorithm>   
 6 #include <string>   
 7 #include <set>   
 8 #include <utility>   
 9 #include <queue>   
10 #include <stack>   
11 #include <list>   
12 #include <vector>   
13 #include <cstdio>   
14 #include <cstdlib>   
15 #include <cstring>   
16 #include <cmath>   
17 #include <ctime>   
18 #include <ctype.h> 
19 using namespace std;
20 
21 int f[35][2010];
22 
23 int main()
24 {
25     int k,m;
26     int vi,wi;
27     int cnt=1;
28     while(cin>>k>>m)
29     {
30         memset(f,0,sizeof(f));
31         int i,j;
32         for(i=1;i<=k;i++)
33         {
34             int n;
35             cin>>n;
36             for(j=1;j<=m;j++)
37                 f[i][j]=f[i-1][j];
38             while(n--)
39             {
40                 cin>>vi>>wi;
41                 for(j=vi;j<=m;j++)
42                     f[i][j]=f[i-1][j-vi]+wi>f[i][j]?f[i-1][j-vi]+wi:f[i][j];
43             }
44         }
45         if(cnt!=1)
46             cout<<endl;
47         cout<<"Case "<<cnt++<<": "<<f[k][m]<<endl;
48     }
49     return 0;
50 }

 

posted @ 2012-07-23 18:23  max_xbw  阅读(508)  评论(0编辑  收藏  举报