zoj 1196 Fast Food 变种区间dp

好吧,其实我觉得这个题在一开始想的时候与区间dp还是有点差别的……虽然写出来跟区间dp确实差不多

Fast Food Time Limit: 2 Seconds      Memory Limit: 65536 KB 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 3
5
6
12
19
20
27
0 0


Sample Output

Chain 1
Total distance sum = 8



题意:在一条路上有n个餐馆,n个餐馆的位置给出,现在让你建k个仓库,每个餐馆会由最近的仓库进货,进货的代价为pos餐馆-pos仓库。现在让你计算最小的总进货代价

思路:一开始想到的是用区间合并的方法,这个问题相当于将n个递增的数划分进k个集合,在每个集合里可以很容易想到在序号最中间的餐馆建仓库是最优的,假设这个区间是(i,j)那么在(i+j)/2是最优的,如果往左移动x个单位,那么所有右边的餐馆都加上x个单位,但是左边减去的代价是小于等于x*左边餐馆数的,往右同理。所以在(i+j)/2位置建仓库一定是代价最小的一种情况。但是如果枚举区间的话,最多有c(199,29)种情况,这肯定是超时的。

           这时就想到用dp优化了,设dp[i][j]为前i个餐馆建j个仓库的最小代价,那么转移方程就可以写为dp[i][j]=min{dp[i][j],dp[k][j-1]+dis[k+1][i],k=j-1,j……i-1},(dis[i][j]表示在i,j间建立仓库所需的最小代价),其实跟区间dp的思想一样,枚举出分割餐馆的k,前一部分用已求出的dp[k][j-1]得出,后一部分再用dis数组得出。至于求dis数组,只要200*200*200的复杂度,是不会超时的。

       代码如下:

       

#include<stdio.h>
#include<string.h>
#include<algorithm>
int dp[202][31],dis[202][202],map[202];
int main()
{
	int i,j,k,m,n,t;
    t=0;
	while(~scanf("%d%d",&n,&k))
	{ memset(dp,0,sizeof(dp));
	  memset(dis,0,sizeof(dis));
	  if(n==0 && k==0)break;
	  t++;
	  printf("Chain %d\n",t);
	  for(i=1;i<=n;i++)
	   scanf("%d",&map[i]);
	  for(i=1;i<=n;i++)
	   for(j=1;j<=n;j++) 
	    {   int sum=0;
	    	int temp=(i+j)/2;
	    	for(int i1=i;i1<temp;i1++)sum+=(map[temp]-map[i1]);
	    	for(int i1=temp+1;i1<=j;i1++)sum+=map[i1]-map[temp];
	    	dis[i][j]=sum;
		}
	  for(i=1;i<=n;i++)dp[i][0]=0x1f1f1f;	
	  for(i=1;i<=n;i++)
	    for(j=1;j<=i && j<=k;j++)
		 {
		 	 dp[i][j]=0x1f1f1f;
			 for(int k1=j-1;k1<=i-1;k1++)
			  if(dp[k1][j-1]+dis[k1+1][i]<dp[i][j])
			   dp[i][j]=dp[k1][j-1]+dis[k1+1][i];  
			 }
	 printf("Total distance sum = %d\n\n",dp[n][k]);		 	
	}
	return 0;
 } 


dp是世界上最好的算法!!!

你可能感兴趣的:(dp,ACM,ZOJ)