hdu 1227(动态规划)
Fast Food
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2647 Accepted Submission(s): 1124
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 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 a blank line after each test case.
5
6
12
19
20
27
0 0
Total distance sum = 8
假设第 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(动态规划)的更多相关文章
- HDU 1227 Fast Food
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1227 题意:一维坐标上有n个点,位置已知,选出k(k <= n)个点,使得所有n个点与选定的点中 ...
- hdu 1087 动态规划之最长上升子序列
http://acm.hdu.edu.cn/showproblem.php?pid=1087 Online Judge Online Exercise Online Teaching Online C ...
- HDU 1003 动态规划
http://acm.hdu.edu.cn/showproblem.php?pid=1003 这几天开始刷动归题目,先来一道签到题 然而做的并不轻松, 没有注意到边界问题, WA了几发才发现 #inc ...
- hdu 4055 && hdu 4489 动态规划
hdu 4055: 一开始我想的递推方向想得很复杂,看了别人的博客后才醍醐灌顶: 参照他的思路和代码: #include<cstdio> #include<cstring> # ...
- hdu 4745 动态规划
思路:特水的一个最长回文子序列动态规划.比赛时硬卡第一题,49WA后终于AC,可惜没时间做这题,结果成绩也就可想而知了.兔子跳一样权值的石头,并且一个正跳,一个反跳,这不就是个回文子序列吗?????! ...
- hdu 4711 动态规划
思路:其实这题是个挺水的动态规划,一开始就能AC,可是不知道错哪了,瞎改瞎交,WA了数十次.AC之后怎么改都是AC,也不知道改了什么地方,郁闷死了~~~难道开始时的测试数据有问题??? dp[i][j ...
- HDU 6076 (动态规划)
HDU 6076 Security Check Problem : 有两个长度为n的队列过安检,每个人有一个特征值.如果两个队列中的第一个人的特征值之差小于等于k,那么一次只能检查其中一个人,否则一次 ...
- 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 ...
- 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]为新 ...
随机推荐
- sqlite sql语句关键字GROUP BY的理解
第一遍看GROUP BY的介绍时,没看懂. SQLite 的 GROUP BY 子句用于与 SELECT 语句一起使用,来对相同的数据进行分组.在 SELECT 语句中,GROUP BY 子句放在 W ...
- PAT 1080 MOOC期终成绩
https://pintia.cn/problem-sets/994805260223102976/problems/994805261493977088 对于在中国大学MOOC(http://www ...
- [Elasticsearch] 多字段搜索 (一) - 多个及单个查询字符串
多字段搜索(Multifield Search) 本文翻译自官方指南的Multifield Search一章. 查询很少是只拥有一个match查询子句的查询.我们经常需要对一个或者多个字段使用相同或者 ...
- Java集合整体框架
Java中的集合类有List.Set.Map Collection的实现类:List.Set List的实现类:ArrayList.LinkedList.Vector Set的实现类:HashSet. ...
- JSON使用(4)
把JSON文本转换为JavaScript对象 JSON最常见的用法之一,是从web服务器上读取JSON数据(作为文件或作为HttpRequest),将JSON数据转换为JavaScript对象,然后在 ...
- C#语法糖大汇总【转发】
首先需要声明的是“语法糖”这个词绝非贬义词,它可以给我带来方便,是一种便捷的写法,编译器会帮我们做转换:而且可以提高开发编码的效率,在性能上也不会带来损失.这让java开发人员羡慕不已,呵呵. 1. ...
- DES 加密解密
[概念] 数据加密算法(Data Encryption Algorithm,DEA)是一种对称加密算法,很可能是使用最广泛的密钥系统,特别是在保护金融数据的安全中,最初开发的DEA是嵌入硬件中的.通常 ...
- bzoj 4836 [Lydsy1704月赛]二元运算 分治FFT+生成函数
[Lydsy1704月赛]二元运算 Time Limit: 8 Sec Memory Limit: 128 MBSubmit: 577 Solved: 201[Submit][Status][Di ...
- js删除一个父元素下面的所有子元素
比如<div id="ok"><button tpye='button'>111111</button><p>22222</p ...
- 第一次做的jsp分页,详细代码。。。。
自己学jsp也有了一段时间,而且自己现在上的课是java web现在雪儿基础做了一个最简单的jsp页面,代码都放在一个页面,自己准备在改进,一步步来,这里的代码可能不是很完美,没事,下面接下来会有大概 ...