usaco Chapter 3 section 3.3 Shopping Offers

来源:互联网 发布:计算机二级vb题库 编辑:程序博客网 时间:2024/05/17 08:39

/*

Shopping Offers
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.

我又偷懒了,用了动态规划,直接上。哎一开始把数组开小了,WA了3次。

*/
/*
ID: niepeng1
LANG: C++
TASK:shopping
*/
#include <iostream>
#include<stdio.h>
#include <memory.h>
#include <stdlib.h>
using namespace std;
#define min(a,b) a<b?a:b
FILE *in,*out;
int map[6][6][6][6][6];
struct Node{
 int count;
 int num;
};
Node offer[100][6];//
int point[1000],offernum,itemnum;
int item[5][2];
int dest[5];
int tem[5];
int main()
{
 int i,j,i0,i1,i2,i3,i4;
 in=fopen("shopping.in","r");
 out=fopen("shopping.out","w");
 fscanf(in,"%d",&offernum);
 for(i=0;i<offernum;i++)
 {
  fscanf(in,"%d",&offer[i][0].count);//该offer中的物品数目
  for(j=1;j<=offer[i][0].count;j++)
   fscanf(in,"%d %d",&offer[i][j].count,&offer[i][j].num);
  fscanf(in,"%d",&offer[i][0].num);//该offer中的物品总共价格
 }
 fscanf(in,"%d",&itemnum);

 for(i=0;i<itemnum;i++)
 {
  fscanf(in,"%d %d %d",&item[i][0],&dest[i],&item[i][1]);
  point[item[i][0]]=i;//7 指向 0
  memset(tem,0,sizeof(tem));
  
  for(j=0;j<=5;j++,tem[i]++)
  {   
   map[tem[0]][tem[1]][tem[2]][tem[3]][tem[4]]=item[i][1]*j;
  }
 }
// int ttem
 for(i0=0;i0<=5;i0++)
  for(i1=0;i1<=5;i1++)
   for(i2=0;i2<=5;i2++)
    for(i3=0;i3<=5;i3++)
     for(i4=0;i4<=5;i4++)
     {
      map[i0][i1][i2][i3][i4]=map[i0][0][0][0][0]+map[0][i1][0][0][0]+map[0][0][i2][0][0]+map[0][0][0][i3][0]+map[0][0][0][0][i4];       
     }
 for(i=0;i<offernum;i++)
 {
  memset(tem,0,sizeof(tem));
  for(j=1;j<=offer[i][0].count;j++)
  {
   tem[point[offer[i][j].count]]=offer[i][j].num;
  }

  for(i0=0;i0<=5;i0++)
   for(i1=0;i1<=5;i1++)
    for(i2=0;i2<=5;i2++)
     for(i3=0;i3<=5;i3++)
      for(i4=0;i4<=5;i4++)
      {
       
       if(i0+tem[0]<=5&&i1+tem[1]<=5&&i2+tem[2]<=5&&i3+tem[3]<=5&&i4+tem[4]<=5)
       {
        if(map[i0+tem[0]][i1+tem[1]][i2+tem[2]][i3+tem[3]][i4+tem[4]]==0)
         map[i0+tem[0]][i1+tem[1]][i2+tem[2]][i3+tem[3]][i4+tem[4]]=map[i0][i1][i2][i3][i4]+offer[i][0].num;
        else
         map[i0+tem[0]][i1+tem[1]][i2+tem[2]][i3+tem[3]][i4+tem[4]]
          = min(map[i0][i1][i2][i3][i4]+offer[i][0].num,map[i0+tem[0]][i1+tem[1]][i2+tem[2]][i3+tem[3]][i4+tem[4]]);
       }
       
      }
 }

 fprintf(out,"%d/n",map[dest[0]][dest[1]][dest[2]][dest[3]][dest[4]]);
 fclose(in);fclose(out);
 return 0;
}

原创粉丝点击