链接: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

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
 
一来没思路,看了题解,目前动态规划的题几乎都看了题解才有思路。。。。。。
思路:[i,j]之间加一个仓库,为了使要增加的路程最短,只能加在第(int)(i+j)/2个餐厅。
case[i][j]为在[i,j]之间加一个仓库所要增加的路程。
dp[i][j]表示在前j个餐厅之中加i个仓库所要走的路程。
状态转移方程:dp[i][j]=min(dp[i][j],dp[i-1][m]+case[m+1][j]);  (i-1<=m<=j-1)
 
#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_动态规划的更多相关文章

  1. 增强学习(三)----- MDP的动态规划解法

    上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...

  2. 简单动态规划-LeetCode198

    题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...

  3. 动态规划 Dynamic Programming

    March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...

  4. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  5. C#动态规划查找两个字符串最大子串

     //动态规划查找两个字符串最大子串         public static string lcs(string word1, string word2)         {            ...

  6. C#递归、动态规划计算斐波那契数列

    //递归         public static long recurFib(int num)         {             if (num < 2)              ...

  7. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  8. 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划

    [BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...

  9. POJ 1163 The Triangle(简单动态规划)

    http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

随机推荐

  1. iOS8使用TouchID

    iOS8新增了LocalAuthentication框架,用于TouchID的授权使用.亲測,眼下须要用户的设备支持指纹识别并已设置锁屏,并且实际測试过程中反馈比較慢.不能直接跟第三方账号passwo ...

  2. mongodb的备忘录

    https://www.cnblogs.com/best/p/6212807.html

  3. redis 事务 及发布于订阅功能

    事务: Redis事务可以一次执行多个命令,事务具有以下特征: 1.隔离操作:事务中的所有命令都会序列化.按顺序地执行,不会被其他命令打扰. 2.原子操作:事务中的命令要么全部被执行,要么全部都不执行 ...

  4. C#编译器优化那点事 c# 如果一个对象的值为null,那么它调用扩展方法时为甚么不报错 webAPI 控制器(Controller)太多怎么办? .NET MVC项目设置包含Areas中的页面为默认启动页 (五)Net Core使用静态文件 学习ASP.NET Core Razor 编程系列八——并发处理

    C#编译器优化那点事   使用C#编写程序,给最终用户的程序,是需要使用release配置的,而release配置和debug配置,有一个关键区别,就是release的编译器优化默认是启用的.优化代码 ...

  5. RELU 激活函数及其他相关的函数

    RELU 激活函数及其他相关的函数 转载 2016年07月21日 20:51:17 45778 本博客仅为作者记录笔记之用,不免有很多细节不对之处. 还望各位看官能够见谅,欢迎批评指正. 更多相关博客 ...

  6. 几种查看CentOS系统版本号和位数的方法

    查看系统版本号: cat /etc/redhat-release cat /proc/version uname -a cat /etc/issue 查看64位还是32位: getconf LONG_ ...

  7. io口的作用

    I/O接口的作用     主机与外界交换信息称为输入/输出(I/O).主机与外界的信息交换是通过输入/输出设备进行的.一般的输入/输出设备都是机械的或机电相结合的产物,比方常规的外设有键盘.显示器.打 ...

  8. 图像处理之基础---滤波器之高斯低通滤波器的高斯模板生成c实现

    ()代码实现 对原图进行高斯平滑,去除图像中的计算噪声void Bmp::MakeGauss(double sigma,double **pdKernel,int *pnWindowSize){ // ...

  9. c# IP从192.168.1.1转成int类型

    找了一些资料,总结如下: 方法1 .net提供的方法转换IP地址 //字符串转换为数字 System.Net.IPAddress ipaddress = System.Net.IPAddress.Pa ...

  10. 使用NDK r10构建Cocosd-x v3.2时编译和链接错误的解决办法

    如果你使用NDK r10构建Cocos2d-x v3.2,将会遇到所有测试用例编译错误以及Lua测试用例链接错误. 1. 编译错误 错误信息是: 1 2 3 4 5 6 7 8 /Users/ming ...