Fast Food

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 2173    Accepted Submission(s): 930

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 3
5
6
12
19
20
27
0 0
 
Sample Output
Chain 1
Total distance sum = 8
 
Source

解题思路:

题意为 一条路上有 n个商店,每一个商店有一个x坐标位置,有k个仓库,要把k个仓库安放在n个位置中的k个上面,每一个商店都向近期的仓库来获得补给,求怎么安放这k个仓库,使得每一个商店到相应仓库的距离仅仅和加起来最小,输出最小值。

解决本题要意识到两点:

1. 假设要在第i个位置和第j个位置之间安放仓库,那么要把它安放在  (i+j)/2 个位置上,(i+j)/2为整数, 才干保证从i到j个商店到仓库之间的距离之和最短。

2.假设依照题意把k个仓库安放在n个位置上,使得距离和最短,这样求得了最小值, 那么一定符合题意:每一个商店都向近期的仓库来获得补给,由于假设不是向近期的,距离和肯定不是最短

用dp[i][j] 代表 前j个商店,有i个仓库

那么状态转移方程为:

dp [ i  ]  [ j ] = min ( dp [ i ] [ j ] ,   dp  [ i-1 ]  [ k]  + cost [ k+1 ]  [ j ] )   i-1<=k<=j-1

dp[i][j] 要从前一个状态推出来,及前k个商店有i-1个仓库,k是不确定的,但能够确定它的范围,最小是i-1 (一个商店位置上放一个仓库),最大是j-1 ( 把第i个仓库放在第j个位置上) ,    cost [ i ] [ j ]为在i ,j之间放一个仓库的最小距离和,即前面提到的第1点。

代码:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cmath>
#include <string.h>
using namespace std;
int n,k;
int dp[32][210];//dp[i][j]前j个商店有i个仓库
int dis[210];
const int inf=0x3f3f3f3f; int cost(int i,int j)
{
int ans=0;
int mid=dis[(i+j)/2];
for(int k=i;k<=j;k++)
ans+=abs(dis[k]-mid);
return ans;
} int main()
{
int c=1;
while(scanf("%d%d",&n,&k)!=EOF)
{
if(!n||!k)
break;
for(int i=1;i<=n;i++)
scanf("%d",&dis[i]);
memset(dp,inf,sizeof(dp));
for(int i=1;i<=n;i++)
dp[1][i]=cost(1,i);
for(int i=2;i<=k;i++)//第i个仓库
for(int j=1;j<=n;j++)//前j个商店
{
for(int k=i-1;k<=j-1;k++)
dp[i][j]=min(dp[i][j],dp[i-1][k]+cost(k+1,j));
} printf("Chain %d\n",c++);
printf("Total distance sum = %d\n",dp[k][n]);
printf("\n");
}
return 0;
}

[ACM] HDU 1227 Fast Food (经典Dp)的更多相关文章

  1. hdu 1227 Fast Food(DP)

    题意: X轴上有N个餐馆.位置分别是D[1]...D[N]. 有K个食物储存点.每一个食物储存点必须和某个餐厅是同一个位置. 计算SUM(Di-(离第i个餐厅最近的储存点位置))的最小值. 1 < ...

  2. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  3. HDU 1227 Fast Food

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1227 题意:一维坐标上有n个点,位置已知,选出k(k <= n)个点,使得所有n个点与选定的点中 ...

  4. HDU 1227 Fast Food (DP)

    题目链接 题意 : 有n个饭店,要求建k个供应点,要求每个供应点一定要建造在某个饭店的位置上,然后饭店都到最近的供应点拿货,求出所有饭店到最近的供应点的最短距离. 思路 : 一开始没看出来是DP,后来 ...

  5. [ACM] hdu 4405 Aeroplane chess (概率DP)

    Aeroplane chess Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 ...

  6. [ACM] hdu 5045 Contest (减少国家Dp)

    Contest Problem Description In the ACM International Collegiate Programming Contest, each team consi ...

  7. HDU 4064 Carcassonne(插头DP)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4064 Problem Description Carcassonne is a tile-based ...

  8. HDU 2859 Phalanx(对称矩阵 经典dp样例)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2859 Phalanx Time Limit: 10000/5000 MS (Java/Others)  ...

  9. HDU 4616 Game(经典树形dp+最大权值和链)

    http://acm.hdu.edu.cn/showproblem.php?pid=4616 题意:给出一棵树,每个顶点有权值,还有存在陷阱,现在从任意一个顶点出发,并且每个顶点只能经过一次,如果经过 ...

随机推荐

  1. SE 2014年5月25日

    如图配置 两实验 R1模拟总部,R2 与R3模拟分部 实验一  要求使用 IPSec VPN 主模式,使得总部与两分部内网可相互通讯 步骤: 1.  配置默认路由 [RT1]ip route-stat ...

  2. Selenium之利用Excel实现参数化

    Selenium之利用Excel实现参数化 说明:我是通过Workbook方式来读取excel文件的,这次以登陆界面为例 备注:使用Workbook读取excel文件,前提是excel需要2003版本 ...

  3. Codeforces 549G. Happy Line 馋

    非常有趣的贪婪: Let's reformulate the condition in terms of a certain height the towers, which will be on t ...

  4. Knockout应用开发指南 第四章:模板绑定

    原文:Knockout应用开发指南 第四章:模板绑定 模板绑定The template binding 目的 template绑定通过模板将数据render到页面.模板绑定对于构建嵌套结构的页面非常方 ...

  5. POJ 1287 Networking(最小生成树)

    题意  给你n个点 m条边  求最小生成树的权 这是最裸的最小生成树了 #include<cstdio> #include<cstring> #include<algor ...

  6. 面试题 收集请求k千里马

    收集请求k最大值 个人信息:就读于燕大本科软件project专业 眼下大三; 本人博客:google搜索"cqs_2012"就可以; 个人爱好:酷爱数据结构和算法,希望将来从事算法 ...

  7. hdu2818行列匹配+排序

    题意:给定一个矩阵,矩阵上有的数字是1,有的是0,给定两种操作,交换某两行或者某两列,问是否能置换出对角线为1的矩阵 题解:能够置换出对角线是1的矩形要求有n个1既不在同一行也不再同一列,即行列匹配, ...

  8. Android定义自己的面板共享系统

    在Android分享知道有一个更方便的方法.调用的共享面板来分享我们的应用程序的系统.主要实现例如,下面的: public Intent getShareIntent(){ Intent intent ...

  9. Coreseek:部门查询和增量索引代替实时索引

    1.行业调查 索引系统需要通过主查询来获取所有的文档信息,一个简单的实现是整个表的数据到内存,但是这可能会导致整个表被锁定,并且使其它操作被阻止(例如:在MyISAM格款式上INSERT操作).同时, ...

  10. 【HTML+CSS】(1)基本语法

    HTML基金会 <em>他强调标签,<strong>加粗标签 <q>短文本引用.<blockquote>长文本引用,这两个标签会让文字带双引號.   空 ...