现在的位置: 首页 > 综合 > 正文

USACO SECTION 3.3 Shopping Offers

2017年11月23日 ⁄ 综合 ⁄ 共 3538字 ⁄ 字号 评论关闭
文章目录

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.

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
/*
ID: conicoc1
LANG: C
TASK: shopping
*/

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int Pro[1000][2]; //0为需求数,1为价格 
int ProN[6];
int Offers[100][6][2];    //i种优惠方式,j种商品,0为商品编号,1为数量
int OfferPrice[100]; 
int SpecialOffers,Products;
int MinPrice=25000;
int flag[10000];

int transform()
{
	int i,temp=1,sum=0;
	for(i=1;i<=5;i++){
		sum+=Pro[ProN[i]][0]*temp;
		temp*=6;
	}	
	return sum;
}

void Act(int Choose)
{
	int i;
	for(i=1;i<=5;i++){
		Pro[Offers[Choose][i][0]][0]-=Offers[Choose][i][1];
	}
} 

void ReAct(int Choose)
{
	int i;
	for(i=1;i<=5;i++){
		Pro[Offers[Choose][i][0]][0]+=Offers[Choose][i][1];
	}
}

int Judge(int Choose)
{
	int i;
	for(i=1;i<=5;i++){
		if(Pro[Offers[Choose][i][0]][0]<Offers[Choose][i][1])
			return 0;
	}
	return 1;
}

void DFS(int NowPrice)
{
	int temp,i;
	/*剪枝2*/
	temp=transform();
	if(flag[temp]<=NowPrice&&flag[temp]!=0)
		return;
	flag[temp]=NowPrice;
	for(i=1;i<=SpecialOffers;i++){
		/*剪枝1*/ 
		if(!Judge(i)){
			MinPrice=(NowPrice<MinPrice?NowPrice:MinPrice);   //无法使用该优惠方式 
			continue;
		}
		
		Act(i);
		DFS(NowPrice-OfferPrice[i]);
		ReAct(i);
	}
}

int main()
{
	FILE *fin,*fout;
	fin=fopen("shopping.in","r");
	fout=fopen("shopping.out","w");
	int NowPrice;
	int i,j,temp,sum;
	fscanf(fin,"%d",&SpecialOffers);
	for(i=1;i<=SpecialOffers;i++){
		fscanf(fin,"%d",&temp);
		for(j=1;j<=temp;j++){
			fscanf(fin,"%d %d",&Offers[i][j][0],&Offers[i][j][1]);
		}
		fscanf(fin,"%d",&OfferPrice[i]);
	}
	
	fscanf(fin,"%d",&Products);
	for(i=1;i<=Products;i++){
		fscanf(fin,"%d",&ProN[i]);
		fscanf(fin,"%d %d",&Pro[ProN[i]][0],&Pro[ProN[i]][1]);

	}
	
	for(i=1;i<=SpecialOffers;i++){
			for(j=1,sum=0;j<=5;j++){
			sum+=Offers[i][j][1]*Pro[Offers[i][j][0]][1];	
		}
		OfferPrice[i]=sum-OfferPrice[i];      //计算优惠差价 
	}
		
    for(i=1,NowPrice=0;i<=1000;i++){
    	NowPrice+=Pro[i][0]*Pro[i][1];
    }
    MinPrice=NowPrice;
    
    DFS(NowPrice);
    
    fprintf(fout,"%d\n",MinPrice);
	return 0;
}

一开始想DP想不出,没有发现物品种类和需求量都是1-5

用深搜的时候考虑递归深度的时候发现。。最多递归25层,每层最多99个分支,显然要剪枝

最后还是开了个HASH数组,当当前价格数比记录的价格大时进行剪枝

这样考虑的话还是用DP来吧

用五维数组记录当前需要购买的每件物品的数量,值为最小价格

初始化DP[0][0][0][0][0]=0; 用五重循环,记得要把单个物品的原始价格也记入优惠方案中

 

 

抱歉!评论已关闭.