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]为新 ...
随机推荐
- 找jar包的网址
http://search.maven.org/#search%7Cga%7C1%7Cmybatis http://mvnrepository.com/
- Documentation & Markdown
Documentation & Markdown markdown to document & document website generator https://github.co ...
- 【bzoj3379】[Usaco2004 Open]Turning in Homework 交作业 区间dp
题目描述 数轴上有C个点,每个点有一个坐标和一个访问时间,必须在这个时间后到达这个点才算访问完成.可以在某个位置停留.每在数轴上走一个单位长度消耗一个单位的时间,问:访问所有点并最终到B花费的最小时间 ...
- MSSQL事务在C#程序端的使用
拼接成一条SQL执行 优点:简单,容易看懂: 缺点:某些场合,涉及的业务较多,在同一SQL处理显得太冗长,复杂,不利于解耦. 使用细节 在方法之间传递参数,确保多个方法中的SQL都是使用同一个事务的( ...
- git查看和操作commit命令
git reflog 显示所有branch的commit,包括commit和reset,以及已删除的commit.而git log只显示当前branch的commit,不包括已删除的commit gi ...
- [poj 3693]后缀数组+出现次数最多的重复子串
题目链接:http://poj.org/problem?id=3693 枚举长度L,看长度为L的子串最多能重复出现几次,首先,能出现1次是肯定的,然后看是否能出现两次及以上.由抽屉原理,这个子串出现次 ...
- HDU3081:Marriage Match II (Floyd/并查集+二分图匹配/最大流(+二分))
Marriage Match II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- Codeforces Round #525 (Div. 2)E. Ehab and a component choosing problem
E. Ehab and a component choosing problem 题目链接:https://codeforces.com/contest/1088/problem/E 题意: 给出一个 ...
- centos关闭ipv6
1.使用lsmod查看ipv6的模块是否被加载. lsmod | grep ipv6 [root@dmhadoop011 ~]# lsmod | grep ipv6 ipv6 ...
- 浏览器 连不上网 (3):DNS 服务器问题
解决:设置一下DNS服务器的地址 步骤: 打开网络和共享中心(网络和 Internet设置)-> 更改适配器 -> 双击我们连接的 无线网(WiFi式) 或 以太网(网线式): 从出现的窗 ...