1049 - Remember the A La Mode!

Hugh Samston owns the ``You Want It, Hugh Got It" catering service, which has been asked to supply desserts for the participants in this year's ICPC World Finals. Hugh will provide pie slices topped with ice cream at the various social functions scheduled throughout the week. As with any other dedicated entrepreneur, Hugh would like to offer the best service possible, so he has ordered a wide variety of pies and ice creams to satisfy even the most eclectic tastes.

Hugh plans to serve each pie slice with a single scoop of ice cream, leaving the exact combination up to the whim of the customer. But of course, as with any other dedicated entrepreneur, Hugh would also like to make as much profit as possible from this enterprise. He knows ahead of time how much profit he can make on each combination of pie slice and ice cream scoop, as well as which combinations of pie and ice cream should never be put together (example: Peppermint Banana Chunk ice cream on Key Lime pie).

Given this information, along with the number of slices and scoops he has of each variety of pie and ice cream, Hugh figures he can determine both the minimum and maximum profits he can expect. Since he hopes to be the caterer at subsequent World Finals, he would like a general program to solve this and future problems.

Input 

Input will consist of multiple problem instances. Each problem instance will start with a line containing two integers P (P$ \le$50) and I (I$ \le$50), indicating the number of types of pie and ice cream, respectively. The next line will contain P integers indicating the number of slices available for each of the pie types. The line after that will contain I integers indicating the number of scoops available for each of the ice cream types. The total number of pie slices will always equal the total number of ice cream scoops available, and it is assumed that all pie slices and ice cream scoops will be used.

Each problem instance will end with P lines each containing I floating point numbers indicating the profit for each pie/ice cream combination: the first value indicates the profit if a slice of pie type 0 is topped with a scoop of ice cream type 0; the next value indicates the profit if a slice of pie type 0 is topped with a scoop of ice cream type 1, and so on. A profit value of `-1' indicates that no combinations of that pie type and ice cream type should ever be sold. All other integers (number of slices for each type of pie and number of scoops for each type of ice cream) will be less than or equal to 100 and the profit on each one of the pie/ice cream combinations (other than `-1') will be larger than 0 and less than or equal to 10, with at most two digits after the decimal point.

The last problem instance is followed by a line containing two zeroes.

Output 

For each problem instance, output the problem number (starting at 1) followed by the minimum and maximum profits, using the format shown in the sample output. Display all numbers with two fractional digits. All problem instances are guaranteed to have at least one solution using all of the pie slices and ice cream scoops.

Sample Input 

2 3 
40 50 
27 30 33 
1.11 1.27 0.70 
-1 2 0.34 
4 4 
10 10 10 10 
10 10 10 10 
1.01 -1 -1 -1 
-1 1.01 -1 -1 
-1 -1 1.01 -1 
-1 -1 -1 1.01 
0 0

Sample Output 

Problem 1: 91.70 to 105.87 
Problem 2: 40.40 to 40.40

Claimer: The data used in this problem is unofficial data prepared by Derek Kisman. So any mistake here does not imply mistake in the offcial judge data. Only Derek Kisman is responsible for the mistakes. Report mistakes to dkisman@acm.org







#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int profit[55][55],pies[55],ices[55],pies_left[55],ices_left[55],profit_p[55],profit_i[55],
	combins[55][55],from_p[55],from_i[55],n,m,i,j,k,new_profit,sum,cases,inf,updated;
double total_profit,x;

void go()
{
	memset(combins,0,sizeof(combins));
	total_profit=0;
	for(i=0;i<sum;i++)
	{
		for(j=0;j<m;j++)
			profit_i[j]=inf;
		for(j=0;j<n;j++)
		{
			from_p[j]=-1;
			profit_p[j]=inf;
			if(pies_left[j])
				profit_p[j]=0;
		}
		updated=1;
		while(updated)
		{
			updated=0;
			for(j=0;j<n;j++) if(profit_p[j]>inf)
				for(k=0;k<m;k++) if(profit[j][k]>0)
				{
					new_profit=profit_p[j]+profit[j][k];
					if(profit_i[k]==inf||new_profit<profit_i[k])
					{
						profit_i[k]=new_profit;
						from_i[k]=j;
						updated=1;
					}
				}
			for(j=0;j<m;j++) if(profit_i[j]>inf)
				for(k=0;k<n;k++) if(combins[k][j])
				{
					new_profit=profit_i[j]-profit[k][j];
					if(profit_p[k]==inf||new_profit<profit_p[k])
					{
						profit_p[k]=new_profit;
						from_p[k]=j;
						updated=1;
					}
				}
		}
		k=0;
		for(j=1;j<m;j++)
			if(profit_i[k]==inf||!ices_left[k]||profit_i[j]<profit_i[k]&&ices_left[j])
				k=j;
		ices_left[k]--;
		total_profit+=profit_i[k];
		while(1)
		{
			j=from_i[k];
			combins[j][k]++;
			if(from_p[j]<0)
				break;
			k=from_p[j];
			combins[j][k]--;
		}
		pies_left[j]--;
	}
}

int main()
{
	inf=-(1<<30);
	while(scanf("%d%d",&n,&m)&&n)
	{
		sum=0;
		for(i=0;i<n;i++)
		{
			scanf("%d",&pies[i]);
			pies_left[i]=pies[i];
			sum+=pies_left[i];
		}
		for(i=0;i<m;i++)
		{
			scanf("%d",&ices[i]);
			ices_left[i]=ices[i];
		}
		for(i=0;i<n;i++)
			for(j=0;j<m;j++)
			{
				scanf("%lf",&x);
				profit[i][j]=int(x*100+0.1);
			}
		go();
		printf("Problem %d: %.2lf to ",++cases,total_profit/1e2);
		memcpy(pies_left,pies,sizeof(pies));
		memcpy(ices_left,ices,sizeof(ices));
		for(i=0;i<n;i++)
			for(j=0;j<m;j++)
				if(profit[i][j]>0)
					profit[i][j]=1001-profit[i][j];
		go();
		printf("%.2lf\n",(sum*1001-total_profit)/1e2);
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值