【USACO 3.3】(shopping) Shopping Offers

------------Prob-------------

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, ortwo 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.

-------------Solution------------

多重背包

存储上要动点脑筋

然后就是一个V587的五重循环了~~~~~

但程序没有写出一贯写背包的风格、、、呵呵、、果然是被奇怪的存储方式干扰了

-------------Code----------------

/*
ID:zst_0111
PROB:shopping
LANG:C++
*/
#include<stdio.h>
#include<stdlib.h>

int a[102][1002];
int b[7][2];
int f[6][6][6][6][6];
int cast[102];

int min(int x,int y)
{
    return x<y?x:y;
}

int main()
{
    freopen("shopping.in","r",stdin);
    freopen("shopping.out","w",stdout);
   
    int i,j,k,m,n;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        int len;
        scanf("%d",&len);
        for(j=1;j<=len;j++)
        {
            scanf("%d",&k);
            scanf("%d",&a[i][k]);
        }
        scanf("%d",&cast[i]);
    }
    scanf("%d",&m);
    for(i=1;i<=m;i++)
    {
        scanf("%d%d%d",&b[i][0],&b[i][1],&cast[++n]);
        a[n][b[i][0]]=1;
    }
    int i1,i2,i3,i4,i5;
    for(i1=0;i1<6;i1++)
        for(i2=0;i2<6;i2++)
            for(i3=0;i3<6;i3++)
                for(i4=0;i4<6;i4++)
                    for(i5=0;i5<6;i5++)
                        f[i1][i2][i3][i4][i5]=214748364;
    int n1,n2,n3,n4,n5;
    f[0][0][0][0][0]=0;
    for(i=1;i<=n;i++)
    {
        n1=a[i][b[1][0]];
        n2=a[i][b[2][0]];
        n3=a[i][b[3][0]];
        n4=a[i][b[4][0]];
        n5=a[i][b[5][0]];
        for(i1=n1;i1<=b[1][1];i1++)
            for(i2=n2;i2<=b[2][1];i2++)
                for(i3=n3;i3<=b[3][1];i3++)
                    for(i4=n4;i4<=b[4][1];i4++)
                        for(i5=n5;i5<=b[5][1];i5++)
                            f[i1][i2][i3][i4][i5]=min(f[i1][i2][i3][i4][i5],
                            f[i1-n1][i2-n2][i3-n3][i4-n4][i5-n5]+cast[i]);
    }
    printf("%d\n",f[b[1][1]][b[2][1]][b[3][1]][b[4][1]][b[5][1]]);
    return 0;
}

posted on 2011-09-04 22:17  青色有角三倍速  阅读(170)  评论(0编辑  收藏  举报