IOI'95
In a certain shop, each kind of product has an integer price. For example, the price of a flower is 2 zorkmids (z) and the price of a vase is 5z. In order to attract more customers, the shop introduces some special offers.
A special offer consists of one or more product items together for a reduced price, also an integer. Examples:
- three flowers for 5z instead of 6z, or
- two vases together with one flower for 10z instead of 12z.
Write a program that calculates the price a customer has to pay for a purchase, making optimal use of the special offers to make the price as low as possible. You are not allowed to add items, even if that would lower the price.
For the prices and offers given above, the (lowest) price for three flowers and two vases is 14z: two vases and one flower for the reduced price of 10z and two flowers for the regular price of 4z.
PROGRAM NAME: shopping
INPUT FORMAT
The input file has a set of offers followed by a purchase.
Line 1: | s, the number of special offers, (0 <= s <= 99). |
Line 2..s+1: | Each line describes an offer using several integers. The first integer is n (1 <= n <= 5), the number of products that are offered. The subsequent n pairs of integers c and k indicate that k items (1 <= k <= 5) with product code c (1 <= c <= 999) are part of the offer. The last number p on the line stands for the reduced price (1 <= p <= 9999). The reduced price of an offer is less than the sum of the regular prices. |
Line s+2: | The first line contains the number b (0 <= b <= 5) of different kinds of products to be purchased. |
Line s+3..s+b+2: | Each of the subsequent b lines contains three values: c, k, and p. The value c is the (unique) product code (1 <= c <= 999). The value k indicates how many items of this product are to be purchased (1 <= k <= 5). The value p is the regular price per item (1 <= p <= 999). At most 5*5=25 items can be in the basket. |
SAMPLE INPUT (file shopping.in)
2
1 7 3 5
2 7 1 8 2 10
2
7 3 2
8 2 5
OUTPUT FORMAT
A single line with one integer: the lowest possible price to be paid for the purchases.
SAMPLE OUTPUT (file shopping.out)
14
题解:背包问题。题目描述中说商品最多有5件,每件商品最多买5件。所以此题就是一个多重背包问题。用一个五维的数组来表示方程。设p[i][j](1<=j<=5)表示第i种促销方案的第j种商品的件数,p[i][0]用来存储第i种方案的总花费,f[b1][b2][b3][b4][b5]=min(f[b1][b2][b3][b4][b5],f[b1-p[i][1]][b2-p[i][2]][b3-p[i][3]][b4-p[i][4]][b5-p[i][5]]+p[i][0])。方程表示购买b1件商品1,b2件商品2,b3件商品3,b4件商品4,b5件商品5花费的最低价格。初始化条件:f[0][0][0][0][0]=0。
1 /* 2 ID:spcjv51 3 PROG:shopping 4 LANG:C 5 */ 6 #include<stdio.h> 7 #include<string.h> 8 #define MAXN 10 9 typedef struct 10 { 11 int c; 12 int m; 13 }NODE; 14 NODE s[MAXN]; 15 long f[MAXN][MAXN][MAXN][MAXN][MAXN]; 16 int sum[105][1005]; 17 int ans,t; 18 long min(long x,long y) 19 { 20 return x<y?x:y; 21 } 22 int main(void) 23 { 24 freopen("shopping.in","r",stdin); 25 freopen("shopping.out","w",stdout); 26 int i,j,n,l,a1,a2,a3,a4,a5,b1,b2,b3,b4,b5; 27 scanf("%d",&ans); 28 memset(sum,0,sizeof(sum)); 29 for(i=1;i<=ans;i++) 30 { 31 scanf("%d",&n); 32 for(j=1;j<=n;j++) 33 { 34 scanf("%d",&l); 35 scanf("%d",&sum[i][l]); 36 } 37 scanf("%d",&sum[i][1001]); 38 } 39 scanf("%d",&t); 40 for(i=1;i<=t;i++) 41 { 42 scanf("%d%d%d",&s[i].c,&s[i].m,&sum[++ans][1001]); 43 sum[ans][s[i].c]=1; 44 } 45 memset(f,1,sizeof(f)); 46 f[0][0][0][0][0]=0; 47 for(i=1;i<=ans;i++) 48 { 49 a1=sum[i][s[1].c]; 50 a2=sum[i][s[2].c]; 51 a3=sum[i][s[3].c]; 52 a4=sum[i][s[4].c]; 53 a5=sum[i][s[5].c]; 54 for(b1=a1;b1<=s[1].m;b1++) 55 for(b2=a2;b2<=s[2].m;b2++) 56 for(b3=a3;b3<=s[3].m;b3++) 57 for(b4=a4;b4<=s[4].m;b4++) 58 for(b5=a5;b5<=s[5].m;b5++) 59 f[b1][b2][b3][b4][b5]=min(f[b1][b2][b3][b4][b5],f[b1-a1][b2-a2][b3-a3][b4-a4][b5-a5]+sum[i][1001]); 60 } 61 printf("%ld\n",f[s[1].m][s[2].m][s[3].m][s[4].m][s[5].m]); 62 return 0; 63 }