HDU_1227_Fast Food_动态规划
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1227
Fast Food
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2695 Accepted Submission(s): 1142
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.
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
#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<cstring>
using namespace std;
#define LL long long int dis[];
int dist[][];
int dp[][]; int main()
{
int n,k,cases=;
while(scanf("%d%d",&n,&k)!=EOF&&n&&k)
{
memset(dist,,sizeof(dist)); for(int i=; i<=k; i++)
for(int j=; j<=n; j++)
dp[i][j]=;
//cout<<dp[0][0];
dp[][]=;
dp[][]=;
for(int i=; i<=n; i++)
scanf("%d",&dis[i]);
for(int i=; i<=n-; i++)
for(int j=i; j<=n; j++)
{
int mid=(i+j)/;
for(int k=i; k<=j; k++)
dist[i][j]+=abs(dis[mid]-dis[k]);
}
for(int i=;i<=n;i++)
dp[][i]=dist[][i];
for(int j=; j<=n; j++)
for(int i=; i<=j,i<=k; i++)
for(int m=i-; m<=j-; m++)
dp[i][j]=min(dp[i][j],dp[i-][m]+dist[m+][j]); printf("Chain %d\n",cases++);
printf("Total distance sum = %d\n\n",dp[k][n]);
}
return ;
}
HDU_1227_Fast Food_动态规划的更多相关文章
- 增强学习(三)----- MDP的动态规划解法
上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...
- 简单动态规划-LeetCode198
题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...
- 动态规划 Dynamic Programming
March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...
- 动态规划之最长公共子序列(LCS)
转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
- C#动态规划查找两个字符串最大子串
//动态规划查找两个字符串最大子串 public static string lcs(string word1, string word2) { ...
- C#递归、动态规划计算斐波那契数列
//递归 public static long recurFib(int num) { if (num < 2) ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划
[BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...
- POJ 1163 The Triangle(简单动态规划)
http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
随机推荐
- mysql 经常使用命令整理总结
#改动字段类型 alter table `table_name` modify column ip varchar(50); #添加字段 alter table `table_name` add ip ...
- 5 shell命令之tr
这是一个奇妙的命令. tr的全拼就是translate,即翻译.有趣的是我们能够制定规则进行翻译.使用方法例如以下: tr [option] set1 [set2] tr从标准输入接受输入.然后将结 ...
- 关于MacBook怎么更新Android SDK
昨天公司的人给了我一个VPN,可是还是无法更新SDK,后来发现将下图: 通过VPN发送全部流量勾选以后就能够连接更新了,哎.处处皆学问,特此分享一下此经验. 喜欢的朋友关注我哦! 多谢支持
- Java语言中extend和implement的区别
Java语言并不支持多重继承,而只能继承一个类,不过我们可以使用implements来实现多个接口. extends继承的父类:不能声明为final或者定义为abstract: implements实 ...
- Canny算法源码,欢迎交流
http://blog.csdn.net/jianxiong8814/article/details/1563109 http://blog.csdn.net/assuper/article/deta ...
- ps图片怎样实现渐变
首先我先将一副图片展示给大家: 我所做的效果是想把图片做成渐变.而不改变中间图片的颜色效果. 接下来我来说明一下我的所做思路. 首先将图片中间部分选择出来.然后复制成一个图层. 详细操作是用魔棒进行反 ...
- ACdream区域赛指导赛之手速赛系列(5) 题解
A - Problem A Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submi ...
- HDU4403 A very hard Aoshu problem DFS
A very hard Aoshu problem Time Limit: 2000/1000 MS (Java/Others) Memory ...
- 60分钟Python快速学习
之前和同事谈到Python,每次下班后跑步都是在听他说,例如Python属于“胶水语言啦”,属于“解释型语言啦!”,是“面向对象的语言啦!”,另外没有数据类型,逻辑全靠空格缩进表示等. 今天自己用了6 ...
- YTU 2631: B1 能存各种类型数据的Store类
2631: B1 能存各种类型数据的Store类 时间限制: 1 Sec 内存限制: 128 MB 提交: 245 解决: 177 题目描述 有一种类,海纳百川,可以对任意类型的数据进行存取,造就 ...