Fast Food

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2647    Accepted Submission(s): 1124

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个餐馆间建k个仓库,求所有餐馆到仓库和最小
分析:dp[i][j]表示前i个餐厅建j个仓库并且第j个仓库建在i点花费的最少代价
假设第 j-1个仓库建设在 k,那么前j个花费的代价为dp[k][j-1]+cost(k,i)cost(k,j)表示k-j的所有餐馆到仓库花费的最少代价
这题重要的是预处理
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
#define N 205
using namespace std; int v[N];
int dp[N][]; ///dp[i][j]表示前i个餐厅建j个仓库并且第j个仓库建在i点花费的最少代价
///假设第 j-1个仓库建设在 k,那么前j个花费的代价为dp[k][j-1]+cost(k,i)
///cost(k,j)表示k-j的所有餐馆到仓库花费的最少代价
int main()
{
int n,k;
int t = ;
while(scanf("%d%d",&n,&k)!=EOF,n+k){
for(int i=;i<=n;i++) {
scanf("%d",&v[i]);
}
for(int i=;i<=n;i++){ ///必要的预处理,因为如果算第1个仓库的时候没有处理,后面的就算不出来了
int cost=;
for(int j=;j<=i;j++){
cost+=v[i]-v[j];
}
dp[i][]=cost;
}
for(int j=;j<=k;j++){ ///枚举仓库数,1已经算过了
for(int i=j;i<=n;i++){ ///枚举餐馆,从j开始,因为仓库数从j开始
dp[i][j]=;
for(int m=j-;m<i;m++){
int cost = ;
for(int c = m+;c<i;c++){
cost += min(v[c]-v[m],v[i]-v[c]);
}
dp[i][j] = min(dp[i][j],dp[m][j-]+cost);
}
}
}
int ans = ;
for(int i=;i<=n;i++){ ///还只算到dp[i][k] 后面的餐馆到其距离还未加上去
int cost=;
for(int j=i+;j<=n;j++){
cost+=v[j]-v[i];
}
ans = min(ans,dp[i][k]+cost);
}
printf("Chain %d\nTotal distance sum = %d\n\n",t++,ans);
}
}

最新文章

  1. Java 设计模式 —— 单例模式
  2. Secure Digital
  3. 如何:对 SharePoint 列表项隐藏 ECB 中的菜单项
  4. centos 命令大全
  5. text-indent:-9999px 字体隐藏问题
  6. 连接Oracle的帮助类
  7. java注解框架
  8. [lintcode the-smallest-difference]最小差(python)
  9. 修改Map中确定key对应的value问题
  10. Markdown 学习笔记: Basics
  11. 如何在TableView上添加悬浮按钮
  12. 讲讲金融业务(一)--自助结算终端POS
  13. 在Ubuntu上录制视频和编辑(很全)
  14. 古老server源代码迁移到新server
  15. 一个简单的string类,读书看报系列(一)
  16. Maya人物骨骼创建与蒙皮
  17. 盒子取球C语言 蓝桥杯
  18. Python内置模块的几点笔记
  19. Asp.Net4.5 mvc4(二) 页面创建与讲解
  20. 自然语言处理nlp工具

热门文章

  1. hadoop 2.6.0 伪分布式部署安装遇到的问题
  2. vue 介绍的拓展
  3. Android 多屏幕适配 dp和px的关系
  4. NAPT 分为锥型(Cone)和 对称型(Symmetric)
  5. 【C++ 拾遗】extern 关键字
  6. 理解JavaScript的function
  7. React.js基础知识
  8. 转:mybatis 高级结果映射(http://blog.csdn.net/ilovejava_2010/article/details/8180521)
  9. C++ 智能指针的简单实现
  10. 51nod 1020 逆序排列——dp