您的位置:首页 > 其它

Problem 1005 Fast Food

2014-04-10 23:40 197 查看
题目:
Problem 1005 Fast Food

Accept: 714 Submit: 1980

Time Limit: 1000 mSec Memory Limit : 32768 KB



Problem Description

The fastfood chain McBurger owns several restaurants along a highway. Recently, they have decided to build several depots along the highway, each one located at a restaurant and supplying several of the restaurants with the needed ingredients. Naturally,
these depots should be placed so that the average distance between a restaurant and its assigned depot is minimized. You are to write a program that computes the optimal positions and assignments of the depots.

To make this more precise, the management of McBurger has issued the following specification: You will be given the positions of n restaurants along the highway as n integers d1 < d2 < ... < dn (these are the distances measured from the company's headquarter,
which happens to be at the same highway). Furthermore, a number k (k <= n) will be given, the number of depots to be built.

The k depots will be built at the locations of k different restaurants. Each restaurant will be assigned to the closest depot, from which it will then receive its supplies. To minimize shipping costs, the total distance sum, defined as



must be as small as possible.

Write a program that computes the positions of the k depots, such that the total distance sum is minimized.



Input

The input file contains several descriptions of fastfood chains. Each description starts with a line containing the two integers n and k. n and k will satisfy 1 <= n <= 200, 1 <= k <= 30, k <= n. Following this will n lines containing
one integer each, giving the positions di of the restaurants, ordered increasingly.

The input file will end with a case starting with n = k = 0. This case should not be processed.



Output

For each chain, first output the number of the chain. Then output a line containing the total distance sum.

Output a blank line after each test case.



Sample Input

6 356121920270 0



Sample Output

Chain 1Total distance sum = 8//题目摘自http://acm.fzu.edu.cn/problem.php?pid=1005



内容是学习http://blog.csdn.net/e6894853/article/details/9791249 然后个别优化计算,中间出现depot开得不过大,原来[31][222]出现-》runtime error 错误,后面艰难地发现了,结果,竟然输出格式错误,少了换行

#include<stdio.h>
int main()
{
  int depot[222][222],min[31][222],n,k,front,now,putin,j,add=0;
  while((scanf("%d%d",&n,&k)),n!=0)
  {
	  add++;
	  for(putin=1;putin<=n;putin++)
	  {scanf("%d",&depot[0][putin]);}
	  for(front=1;front<=n;front++)
	  {
		  depot[front][front]=0;
		  j=n-k+1+front;
		  for(now=1+front;now<=j;now++)
		  {
			  depot[front][now]=depot[front][now-1]
				  +depot[0][now]-depot[0][(now+front)/2]; 
		  }     
	  }
	  for(putin=1;putin<=n-k+1;putin++)/*n-k+1 中的+1*/
	  {
		  min[1][putin]=depot[1][putin];
	  }
      for(putin=2;putin<=k;putin++)
	  {
		  min[putin][putin]=0;
		  j=n-k+1+putin;
		  for(now=putin+1;now<=j;now++)
		  {
			  min[putin][now]=100000000;
			  for(front=putin-1;front<now;front++)
			  {
			  depot[0][0]=min[putin-1][front]+depot[front+1][now];
			  if(depot[0][0]<min[putin][now])min[putin][now]=depot[0][0];
			  }
		  }
	  }
	  printf("Chain %d\nTotal distance sum = %d\n\n",add,min[k]
);
  }
  return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: