# HDU1227 Fast Food

## 题目大意

n个饭店，要建m个仓库，现在给所有饭店之间的距离，问m个仓库怎么建能建的最小？输出最小值

## AC代码

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 210;
const int INF = ~0u >> 1;
int w[maxn][maxn], dp[maxn][35], a[maxn];
int n, m;
int main(){
int kase = 1;
while(scanf("%d%d", &n, &m) == 2 && n && m){
memset(dp, 0, sizeof(dp));
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = n - 1; i >= 0; i--)
a[i] = a[i] - a[0] + 1;
for (int i = 0; i < n; i++)
for (int j = i; j < n; j++){
w[i][j] = 0;
for (int k = i; k <= j; k++)
w[i][j] += min(a[k] - a[i], a[j] - a[k]);
}
for (int i = 0; i < n; i++)
for (int j = 0; j <= i; j++)
dp[i][1] += a[i] - a[j];
for (int j = 2; j <= m; j++)
for (int i = j - 1; i < n; i++){
dp[i][j] = INF;
for (int k = j - 2; k < i; k++)
dp[i][j] = min(dp[i][j], dp[k][j - 1] + w[k][i]);
}
printf("Chain %d\n", kase++);
printf("Total distance sum = ");
int ans = INF;
for (int i = 0; i < n; i++){
int s = 0;
for (int j = i + 1; j < n; j++)
s += a[j] - a[i];
ans = min(ans, s + dp[i][m]);
}
printf("%d\n\n", ans);
}
return 0;
}

• 本文已收录于以下专栏：

## HDU 1227 Fast Food DP

http://acm.hdu.edu.cn/showproblem.php?pid=1227 题目大意： n个饭店在一条直线上，要在这条直线上建立k个仓库，使各个仓库到商店距离和最小。（仓...

## hdu1227 Fast Food

Fast Food Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota...

## hdu1227 Fast Food--DP

• LaoJiu_
• 2016年03月28日 11:24
• 300

## [ACM] HDU 1227 Fast Food （经典Dp）

Fast Food Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

## HDU 1227 Fast Food

Fast Food Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota...
• WEYuLi
• 2013年03月27日 21:14
• 659

## (HDU1227)Fast Food(DP)

どこでもドア：http://acm.hdu.edu.cn/showproblem.php?pid=1227 一共有n个商店，有k个仓库，设计仓库位置，使每个商店到，离当前商店最近，的一个仓库的距离和...