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个),可以混合着买,也可以单个买,但是不能多买(哪怕多买更便宜),问要买这些商品最少花费是多少
算法:
用5维dp来保存,弄一个买什么的转移,对每一个商品对应到1~5编号,方便处理很多
对于商品数量小于5的,相当于最后一个商品要买0个。
再次提醒,不能多买。
/*
ID:cqz15311
LANG:C++
PROG:shopping
*/
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<cstring>
using namespace std;
int s,cc,kk,dp[6][6][6][6][6],pp[6],t[6],c[206][6],p[206];
int nn[6],tt;
int ord(int x){
for (int i=1;i<=tt;i++){
if (x == nn[i]) return i;
}
tt++;
// printf("nn[%d] = %d\n",tt,x);
nn[tt] = x;
return tt;
}
int main(){
freopen("shopping.in","r",stdin);
freopen("shopping.out","w",stdout);
scanf("%d",&s);
memset(c,0,sizeof(c));
tt = 0;
for (int i=1;i<=s;i++){
int n,cc,kk;
scanf("%d",&n);
for (int j=1;j<=n;j++){
scanf("%d%d",&cc,&kk);
c[i][ord(cc)] = kk;
}
scanf("%d",&p[i]);
// puts("************");
}
int b;
scanf("%d",&b);
for (int i=1;i<=b;i++){
int cc;
scanf("%d",&cc);
scanf("%d%d",&t[ord(cc)],&pp[ord(cc)]);
s++;
p[s] = pp[ord(cc)];
c[s][ord(cc)] = 1;
}
for (int i1=0;i1<=t[1];i1++)
for (int i2=0;i2<=t[2];i2++)
for (int i3=0;i3<=t[3];i3++)
for (int i4=0;i4<=t[4];i4++)
for (int i5=0;i5<=t[5];i5++){
dp[i1][i2][i3][i4][i5] = i1 * pp[1] + i2 * pp[2] + i3 * pp[3] + i4 * pp[4] + i5 * pp[5];
for (int i=1;i<=s;i++){
// printf("*%d %d %d*\n",i,p[i],c[i][1]);
int t1 = i1 - c[i][1];
int t2 = i2 - c[i][2];
int t3 = i3 - c[i][3];
int t4 = i4 - c[i][4];
int t5 = i5 - c[i][5];
if ((t1 >= 0) && (t2 >= 0) && (t3 >= 0) && (t4 >= 0) && (t5 >= 0))
dp[i1][i2][i3][i4][i5] = min(dp[i1][i2][i3][i4][i5],dp[t1][t2][t3][t4][t5] + p[i]);
}
// printf("dp[%d][%d][%d][%d][%d] = %d\n",i1,i2,i3,i4,i5,dp[i1][i2][i3][i4][i5]);
}
printf("%d\n",dp[t[1]][t[2]][t[3]][t[4]][t[5]]);
fclose(stdin);
fclose(stdout);
return 0;
}
标算好像有一个用记忆DFS的??