Problem W UVA 662 二十三 Fast Food
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
System Crawler (2015-08-27)
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 restaurent 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 nrestaurants along the highway as n integers (these are the distances measured from the company's headquarter, which happens to be at the same highway). Furthermore, a number
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. nand k will satisfy ,
,
. 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 an optimal placement of the depots as follows: for each depot output a line containing its position and the range of restaurants it serves. If there is more than one optimal solution, output any of them. After the depot descriptions output a line containing the total distance sum, as defined in the problem text.
Output a blank line after each test case.
Sample Input
6 3
5
6
12
19
20
27
0 0
Sample Output
Chain 1
Depot 1 at restaurant 2 serves restaurants 1 to 3
Depot 2 at restaurant 4 serves restaurants 4 to 5
Depot 3 at restaurant 6 serves restaurant 6
Total distance sum = 8
Miguel Revilla
2000-05-22
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int inf=0x3f3f3f3f; int a[],face[][],dp[][],cost[][]; int put(int k,int n)
{
if(k)
{
int up=face[k][n]+,mid=(up+n)/;
put(k-,face[k][n]);
if(up==n)
printf("Depot %d at restaurant %d serves restaurant %d\n",k,mid,n);
else
printf("Depot %d at restaurant %d serves restaurants %d to %d\n",k,mid,up,n);
}
return ;
} int main()
{
int n,K,mid,cas=;
int i,j,k;
while(scanf("%d %d",&n,&K)!=EOF)
{
memset(cost,,sizeof(cost));
if(n== && K==)
break;
for(i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(i=;i<=n;i++)
{
for(j=i;j<=n;j++)
{
mid=(i+j)/;
for(k=i;k<=j;k++)
{
cost[i][j]=cost[i][j]+abs(a[k]-a[mid]);
}
}
}
for(i=;i<=n;i++)
{
dp[][i]=cost[][i];
} for(i=;i<=K;i++)
{
for(j=i;j<=n;j++)
{
dp[i][j]=inf;
for(k=i-;k<j;k++)
{
if(dp[i][j]>(dp[i-][k]+cost[k+][j]))
dp[i][j]=dp[i-][k]+cost[k+][j],face[i][j]=k;
}
}
} printf("Chain %d\n",cas++);
put(K,n);
printf("Total distance sum = %d\n\n",dp[K][n]);
}
return ;
}
Problem W UVA 662 二十三 Fast Food的更多相关文章
- 【Visual C++】游戏开发五十六 浅墨DirectX教程二十三 打造游戏GUI界面(一)
本系列文章由zhmxy555(毛星云)编写,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/16384009 作者:毛星云 ...
- 木块问题(The Blocks Problem,Uva 101)
不定长数组:vector vector就是一个不定长数组.不仅如此,它把一些常用操作“封装”在了vector类型内部. 例如,若a是一个vector,可以用a.size( )读取它的大小,a.resi ...
- WPF入门教程系列二十三——DataGrid示例(三)
DataGrid的选择模式 默认情况下,DataGrid 的选择模式为“全行选择”,并且可以同时选择多行(如下图所示),我们可以通过SelectionMode 和SelectionUnit 属性来修改 ...
- Bootstrap <基础二十三>页面标题(Page Header)
页面标题(Page Header)是个不错的功能,它会在网页标题四周添加适当的间距.当一个网页中有多个标题且每个标题之间需要添加一定的间距时,页面标题这个功能就显得特别有用.如需使用页面标题(Page ...
- Web 前端开发精华文章推荐(HTML5、CSS3、jQuery)【系列二十三】
<Web 前端开发精华文章推荐>2014年第2期(总第23期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML5 ...
- iOS安全攻防(二十三):Objective-C代码混淆
iOS安全攻防(二十三):Objective-C代码混淆 class-dump能够非常方便的导出程序头文件,不仅让攻击者了解了程序结构方便逆向,还让着急赶进度时写出的欠完好的程序给同行留下笑柄. 所以 ...
- WCF技术剖析之二十三:服务实例(Service Instance)生命周期如何控制[下篇]
原文:WCF技术剖析之二十三:服务实例(Service Instance)生命周期如何控制[下篇] 在[第2篇]中,我们深入剖析了单调(PerCall)模式下WCF对服务实例生命周期的控制,现在我们来 ...
- Bootstrap入门(二十三)JS插件1:模态框
Bootstrap入门(二十三)JS插件1:模态框 1.静态实例 2.动态实例 3.模态框的尺寸和效果 4.包含表单的模态框 模态框经过了优化,更加灵活,以弹出对话框的形式出现,具有最小和最实用的功能 ...
- JAVA之旅(二十三)——System,RunTime,Date,Calendar,Math的数学运算
JAVA之旅(二十三)--System,RunTime,Date,Calendar,Math的数学运算 map实在是太难写了,整理得我都晕都转向了,以后看来需要开一个专题来讲这个了,现在我们来时来学习 ...
随机推荐
- ajax中网页传输(二)JSON——下拉列表显示练习
以json返回数据类型显示“民族下拉列表” 第一:body页面显示部分 <title>JSON下拉显示Nation表中的数据</title> <script src=&q ...
- 将服务费用DIY到底----走出软件作坊:三五个人十来条枪 如何成为开发正规军(十)[转]
前一段时间,讲了一系列开发经理.实施经理.服务经理的工具箱:开发经理的工具箱---走出软件作坊:三五个人十来条枪 如何成为开发正规军(三) ,实施经理的工具箱--走出软件作坊:三五个人十来条枪 如何成 ...
- PS4 的下载速度问题
折腾了好久了 AC68u路由自启动修改 hosts 问题,打算FQ另外改善 ps4 下载速度太慢问题. 后来看到几个dns, 直接修改后就速度超快,也不用在路由中添加了, 直接在 ps4 中网络设置中 ...
- Makefile,如何传递宏定义DEBUG【转】
转自:http://blog.csdn.net/linuxheik/article/details/8051598 版权声明:本文为博主原创文章,未经博主允许不得转载. Makefile,如何传递宏定 ...
- Oracle中merge into的使用
http://blog.csdn.net/yuzhic/article/details/1896878 http://blog.csdn.net/macle2010/article/details/5 ...
- STM32F0系列MCU中断向量表的重映射
最近使用了一款Cortex-M0内核的芯片STM32F030CC,发现它中断向量表的重映射方法与STM32F10x系列的有所区别,在这里记录与分享一下. 由于需要通过IAP进行固件升级,所以芯片的FL ...
- yii2通过foreach循环遍历在一个用户组中取出id去另一表里查寻信息并且带着信息合并原数组信息---案例
yii2通过foreach循环遍历在一个用户组中取出id去另一表里查寻信息并且带着信息合并元数组信息---案例 public function actionRandomLists(){ //查询到了所 ...
- IIS管理网站浏览
7.“/”应用程序中的服务器错误. 分析器错误 说明: 在分析向此请求提供服务所需资源时出错.请检查下列特定分析错误详细信息并适当地修改源文件.分析器错误消息: 文件“/Default.aspx.cs ...
- Oracle存储过程单步调试方法
oracle存储过程单步调试的方法 1.在要调试的过程上单击test,如下图所示: 2.出现如下界面时单击最左上方的按钮:,如下图所示: 3.单击后呈现如下画面: 其中: 表示要停止test; 表示要 ...
- 【转】MYSQL入门学习之三:全文本搜索
转载地址:http://www.2cto.com/database/201212/173873.html 一.理解全文本搜索 www.2cto.com 1.MyISAM支持全文本搜索,而Inn ...