标签:
http://acm.fzu.edu.cn/problem.php?pid=1005
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
Output
Output a blank line after each test case.
Sample Input
Sample Output
n个旅馆和k个补给站的问题
假设有3个旅馆坐标分别是 1, 4, 5, 和2个补给站,那么路程代价就是1了,一个补给站放在坐标为1的旅馆那,令一个放在4位置处。
也可以一个补给站放在坐标为 1 的旅馆那,令一个放在 5 位置处。
//dp[i][k]表示前i个店添加k个供应点所达到的最小值
//状态转移方程为:dp[i][k] = min(dp[j][k-1], sum[j+1][i]),
//其中k-1 <= j <= i-1, sum[i][j]表示从第i个饭店到第j个饭店添加一个供应点所达到的最小值,取i,j中间值即可
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
#define N 220
#define MOD 1000000007
#define met(a, b) memset(a, b, sizeof(a))
#define INF 0x3f3f3f3f
int dp[N][N], sum[N][N], a[N];
int main()
{
int n, m, iCase=1;
while(scanf("%d%d", &n, &m), n||m)
{
int i, j, k;
for(i=1; i<=n; i++)
scanf("%d", &a[i]);
for(i=1; i<=n; i++)
{
sum[i][i] = 0;
for(j=i+1; j<=n; j++)
{
sum[i][j] = sum[i][j-1] + a[j] - a[(i+j)/2];
}
}
for(i=0; i<=n; i++)
for(j=0; j<=m; j++)
dp[i][j] = INF;
dp[0][0] = 0;
for(i=1; i<=n; i++)
{
for(k=1; k<=m; k++)
{
for(j=k-1; j<i; j++)
{
dp[i][k] = min(dp[i][k], dp[j][k-1]+sum[j+1][i]);
}
}
}
printf("Chain %d\n", iCase++);
printf("Total distance sum = %d\n\n", dp[n][m]);
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/YY56/p/5463818.html