hdu 1227(经典dp)

题目链接: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): 2165    Accepted Submission(s): 926

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

Southwestern Europe 1998

题意:一条直线上有n家餐馆,现在要在这n家餐馆中选出k个来建立仓库,每家餐馆都会就近选择一个仓库,使得最小;

思路:分析一下, 用 dp[i][j] 表示前j家餐馆修建i个仓库的值最小,那么当修建第i个仓库的时候,前i-1个仓库肯定修好了,所以用dp[i-1][k]来表示前k家餐馆修建i-1个仓库的最小值,那么容易知道  i-1<=k<j;

所以得到状态转移方程:

dp[i][j]=min(dp[i-1][k]+cost[k+1][j]);

PS:cost[i][j]表示在第i家餐馆到第j家餐馆修建一个仓库的最小值,那么肯定修建在 (i+j)/2处才是最优的;所以预处理一下cost即可;

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
const int INF=99999999;
using namespace std;

int dp[33][220];
int a[220];
int cost[220][220];

int main()
{
  int n,m,test=1;
  while(cin>>n>>m)
  {
    if(n==0&&m==0)break;
    for(int i=1;i<=n;i++)cin>>a[i];
    //预处理一下
    memset(cost,0,sizeof(cost));
    for(int i=1;i<=n;i++)
     for(int j=i+1;j<=n;j++)
     {
       for(int k=i;k<=j;k++)
       cost[i][j]+=fabs(a[k]-a[(i+j)/2]);
     }
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=n;i++)dp[1][i]=cost[1][i];
    //dp
    for(int i=2;i<=m;i++)
     for(int j=i+1;j<=n;j++)
     {
      dp[i][j]=INF;
      for(int k=i-1;k<j;k++)
      dp[i][j]=min(dp[i][j],dp[i-1][k]+cost[k+1][j]);
     }
    printf("Chain %d\n",test++);
    printf("Total distance sum = %d\n\n",dp[m][n]);
  }
  return 0;
}
时间: 2024-10-05 04:43:43

hdu 1227(经典dp)的相关文章

HDU 1176 经典dp

记录最晚时间 从time为2枚举到最晚时间 每个时间段的x轴节点都等于上一个时间段的可触及的最大馅饼数 #include<stdio.h> #include<string.h> #include<algorithm> #include<map> #include<math.h> using namespace std; int dp[100050][11]; int a[100050][11]; void init() { memset(a,0,

hdu 1159 经典dp最长公共子序列

背景:上次比赛就没有做出来,回来根据实际意义半天也想不出如何dp,结果从猜转移方程入手,竟然想对了!开始想把空间优化到一维数组,没有想到要用同维度左边的值wa了. 思路: dp[i][j]=max{max[i-1][j],max[i][j-1],max[i-1][j-1]+(a[i] == b[j])} //dp[i][j]定以为,a串的前i个字符和b串的前b个字符的最大字串和,为选a串的第i个字符而不选b串的第j个字符,不选a串的第i个字符而选b串的第j个字符,既选a串的第i个字符又选b串的第

hdu 1421 经典dp

很显然,将物品按照重量排序后,如果要搬某一对物品,则这两件物品一定是相邻的. 于是排序后依次考虑第i件物品放或者不放,即得到状态转移方程: f[i][j] = min( f( i - 1, j ), f( i - 2, j - 1 ) + d[i] ); 其中,d[i]表示第i件物品和第i-1件物品这一对产生的疲劳度. 1 #include <algorithm> 2 #include <cstring> 3 #include <cstdio> 4 using name

HDU 1227 Fast Food (DP)

题目链接 题意 : 有n个饭店,要求建k个供应点,要求每个供应点一定要建造在某个饭店的位置上,然后饭店都到最近的供应点拿货,求出所有饭店到最近的供应点的最短距离. 思路 : 一开始没看出来是DP,后来想想就想通了.预处理,如果要在下标为 i 到 j 的饭店建一个供应点,那一定是在下标为(i+j)/2的位置建造的,状态转移方程:dp[i][j] = dp[i-1][k-1]+dis[k][j](i <= k <= j) ,dp[i][j]代表的是在前 j 个饭店中建了 i 个供应点的最小距离.方

hdu 1227 Fast Food

http://acm.hdu.edu.cn/showproblem.php?pid=1227 在n个商店中建m个仓库,使各个商店到仓库的路程之和最小,商店到哪个仓库是有选择的,求路程之和最小. dp[i][j]为建i个仓库前j个商店. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define maxn 1000 5 using namespace std; 6 const int

hdu 4734 数位dp

http://acm.hdu.edu.cn/showproblem.php?pid=4734 Problem Description For a decimal number x with n digits (AnAn-1An-2 ... A2A1), we define its weight as F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1. Now you are given two numbers A and B, plea

hdu 3853 概率DP 简单

http://acm.hdu.edu.cn/showproblem.php?pid=3853 题意:有R*C个格子,一个家伙要从(0,0)走到(R-1,C-1) 每次只有三次方向,分别是不动,向下,向右,告诉你这三个方向的概率,以及每走一步需要耗费两个能量,问你走到终点所需要耗费能量的数学期望: 回头再推次,思想跟以前的做过的类似 注意点:分母为0的处理 #include <cstdio> #include <cstring> #include <algorithm>

51nod 1412 AVL树的种类(经典dp)

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1412 题意: 思路: 经典dp!!!可惜我想不到!! $dp[i][k]$表示i个结点,最大深度为k的形态数. 它的转移方程就是: dp[i][k] += dp[i - 1 - j][k - 1] * dp[j][k - 1] dp[i][k] += 2 * dp[i - 1 - j][k - 2] * dp[j][k - 1] j是右子树结点个数,如果除去根结点,是不

HDU 4968 (水dp 其他?)

1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <vector> 5 #include <map> 6 using namespace std; 7 const int inf = 0x3f3f3f3f; 8 const int MAX = 200+10; 9 double GPA[10],dp1[20][30000],dp2[20][30000