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. input设置为readonly后js设置intput的值后台仍然可以接收到

    今天发现一个奇怪现象,一个input属性readonly的值被设置为readonly,然后有前台js给input设置了新值. 虽然前台看不到效果,但是提交到后台后,仍然可以接收到新值,感觉很奇怪. 我 ...

  2. wutianqi 博客 母函数

    母函数(Generating function)详解 — Tanky Woo 在数学中,某个序列的母函数(Generating function,又称生成函数)是一种形式幂级数,其每一项的系数可以提供 ...

  3. JAVA课程设计——植物大战僵尸(团队)

    1.团队名称.团队成员介绍 团名:嗷嗷嗷嗷嗷 吴军霖(组长) 写得一手好代码也改得一手好bug 代码整洁好看源于强迫症 大概没有什么不会的东西叭 真正的王者段位 欧阳震霆(组员) 同样擅长写代码 在青 ...

  4. lintcode-104-合并k个排序链表

    104-合并k个排序链表 合并k个排序链表,并且返回合并后的排序链表.尝试分析和描述其复杂度. 样例 给出3个排序链表[2->4->null,null,-1->null],返回 -1 ...

  5. PAT 甲级 1042 Shuffling Machine

    https://pintia.cn/problem-sets/994805342720868352/problems/994805442671132672 Shuffling is a procedu ...

  6. java csv list cant not repeat

    require: /** * before: * file A1.csv {1,2,3,4,5} * file A2.csv {2,3,9,10,11} * file B1.csv {5,12,13, ...

  7. C++——OOP面向对象理解

    从Rob Pike 的 Google+上的一个推看到了一篇叫<Understanding Object Oriented Programming>的文章,我先把这篇文章简述一下,然后再说说 ...

  8. 【BZOJ 3195 】[Jxoi2012]奇怪的道路 装压dp

    受惯性思维的影响自动把二进制状态认为是连与不连......... 我们这里二进制状态表示的是奇偶,这样的话我们f[i][j][k]表示的就是前i个城市用了j个边他前k个城市的奇偶状态,然后想想怎么转移 ...

  9. 一些奇怪的JavaScript试题

    JavaScript有很多地方和我们熟知的C.Java等的编程习惯不同,这些不同会产生很多让人意想不到的事情.前段时间在知乎有人发了写Javascrtip试题,觉得挺好玩的,这里跟大家分享一下. 01 ...

  10. NOIP2016愤怒的小鸟 [状压dp]

    愤怒的小鸟 题目描述 Kiana 最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于 (0,0) 处,每次 Kiana 可以用它向第一象限发射一只红色的小鸟, ...