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);
}
}

hdu 1227(动态规划)的更多相关文章

  1. HDU 1227 Fast Food

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

  2. hdu 1087 动态规划之最长上升子序列

    http://acm.hdu.edu.cn/showproblem.php?pid=1087 Online Judge Online Exercise Online Teaching Online C ...

  3. HDU 1003 动态规划

    http://acm.hdu.edu.cn/showproblem.php?pid=1003 这几天开始刷动归题目,先来一道签到题 然而做的并不轻松, 没有注意到边界问题, WA了几发才发现 #inc ...

  4. hdu 4055 && hdu 4489 动态规划

    hdu 4055: 一开始我想的递推方向想得很复杂,看了别人的博客后才醍醐灌顶: 参照他的思路和代码: #include<cstdio> #include<cstring> # ...

  5. hdu 4745 动态规划

    思路:特水的一个最长回文子序列动态规划.比赛时硬卡第一题,49WA后终于AC,可惜没时间做这题,结果成绩也就可想而知了.兔子跳一样权值的石头,并且一个正跳,一个反跳,这不就是个回文子序列吗?????! ...

  6. hdu 4711 动态规划

    思路:其实这题是个挺水的动态规划,一开始就能AC,可是不知道错哪了,瞎改瞎交,WA了数十次.AC之后怎么改都是AC,也不知道改了什么地方,郁闷死了~~~难道开始时的测试数据有问题??? dp[i][j ...

  7. HDU 6076 (动态规划)

    HDU 6076 Security Check Problem : 有两个长度为n的队列过安检,每个人有一个特征值.如果两个队列中的第一个人的特征值之差小于等于k,那么一次只能检查其中一个人,否则一次 ...

  8. HDU 1171 Big Event in HDU (动态规划、01背包)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  9. hdu 4719 动态规划

    思路:dp[i]表示到第i个点为结尾能获得的最大值,那么dp[i]=h[i]*h[i]+dp[i-x]-h[i-x];(i-l<=x<=i);那么我们可以转换下,以dp[i]-h[i]为新 ...

随机推荐

  1. APP与智能手表是如何通信的【本文摘抄自深圳尚锐科技】

    APP与智能手表是如何通信的 1. Android 与服务器的通信方式主要有两种,一种是http 通信 ,一种是socket 通信. 两者的最大差异在于,http连接使用的是“请求—响应方式”,即在请 ...

  2. lintcode-103-带环链表 II

    带环链表 II 给定一个链表,如果链表中存在环,则返回到链表中环的起始节点的值,如果没有环,返回null. 样例 给出 -21->10->4->5, tail connects to ...

  3. EXT.NET各个版本下载地址

    官网下载地址为:http://www.nuget.org/packages/Ext.NET

  4. webstorm-前端javascript开发神器中文教程和技巧分享(转)

    webstorm是一款前端javascript开发编辑的神器,此文介绍webstorm的中文教程和技巧分享. webstorm8.0.3中文汉化版下载: 百度网盘下载:http://pan.baidu ...

  5. slf4j使用log4j学习笔记

    一,介绍 SLF4J 简单日记门面(Facade)SLF4J是为各种loging APIs提供一个简单统一的接口,从而使得最终用户能够在部署的时候配置自己希望的loging APIs实现. Loggi ...

  6. NIO Q&A(持续补充。。。。)

    Q:NIO是非阻塞的.但调用的selector.select()方法会阻塞.这和NIO非阻塞岂不是矛盾了? A:非阻塞指的是 IO 事件本身不阻塞,但是获取 IO 事件的 select 方法是需要阻塞 ...

  7. WebSocket简单介绍(WebSocket 实战)(3)

    这一节里我们用一个案例来演示怎么使用 WebSocket 构建一个实时的 Web 应用.这是一个简单的实时多人聊天系统,包括客户端和服务端的实现.客户端通过浏览器向聊天服务器发起请求,服务器端解析客户 ...

  8. BZOJ4584 APIO2016赛艇(动态规划+组合数学)

    如果值域不大,容易想到设f[i][j]为第i个学校选了j的方案数,枚举上一个学校是哪个选了啥即可,可以前缀和优化.于是考虑离散化,由于离散化后相同的数可能可以取不同的值,所以枚举第一个和其所选数(离散 ...

  9. 8086汇编语言 调用声卡播放wav文件(sound blaster)

    开更 大概最后做了一个能播放无损音乐(无压缩.不需解码)的播放器 原理是基于dosbox的模拟声卡,通过硬件之间的相互通讯做到的 关于详细内容接下来再讲. 一.从dosbox入手 我们知道cpu可以直 ...

  10. thymeleaf支持java8的日期实例

    一.实体 @Entity public class Customer { @Id @GenericGenerator(name="generator",strategy = &qu ...