您的位置:首页 > 其它

uva 662 Fast Food

2017-02-03 09:23 267 查看
原题:

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 restaurent 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 d 1 < d 2 < ··· < d n (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 n ∑ i=1 | d i − (position of depot serving restaurant i) | 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 d i 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 an optimal placement of the depots as follows: for each depot output a line containing its position and the range of restaurants it serves. If there is more than one optimal solution, output any of them. After the depot descriptions output a line containing the total distance sum, as defined in the problem text. Output a blank line after each test case.

Sample Input

6 3

5

6

12

19

20

27

0 0

Sample Output

Chain 1

Depot 1 at restaurant 2 serves restaurants 1 to 3

Depot 2 at restaurant 4 serves restaurants 4 to 5

Depot 3 at restaurant 6 serves restaurant 6

Total distance sum = 8

中文:

给你两个数n和k,分别表示有n家店,要在k家店存货物。然后给你n个数,表示这n个店在一维数轴上的坐标。现在问你在这n家店当中选出哪k家店当仓库可以使得这k家店能够给周围的店运送货物的距离最短。距离用绝对值表示。

#include <bits/stdc++.h>
using namespace std;
int mark[201][201];
int dp[50][210];
int pos[210];
int x[201][201],y[201][201];
int n,k;

int get_mid_dis(int a[],int s,int e)
{
if(s==e)
return 0;
int mid=(e+s+1)/2;
int tot=0;
for(int i=s;i<=e;i++)
tot+=abs(a[mid]-a[i]);
return tot;
}

void ini()
{
memset(dp,0,sizeof(dp));
memset(mark,0,sizeof(mark));
memset(x,0,sizeof(x));
memset(y,0,sizeof(y));
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
mark[i][j]=get_mid_dis(pos,i,j);
}
}

int main()
{
ios::sync_with_stdio(false);
int T=1;
while(cin>>n>>k,n+k)
{
for(int i=1;i<=n;i++)
cin>>pos[i];

ini();
for(int i=2;i<=n;i++)
dp[1][i]=mark[1][i];

for(int j=2;j<=k;j++)
{
for(int i=j;i<=n;i++)
{
int tmp=INT_MAX;
for(int t=j-1;t<i;t++)
{
if(dp[j-1][t]+mark[t+1][i]<tmp)
{
tmp=dp[j-1][t]+mark[t+1][i];
x[j][i]=t+1;
y[j][i]=i;
}
}
dp[j][i]=tmp;
}
}

int i=n,j=k;
vector<int> rx,ry;
while(true)
{
if(j==1)
{
rx.push_back(j);
ry.push_back(i);
break;
}
rx.push_back(x[j][i]);
ry.push_back(y[j][i]);
i=x[j][i]-1;
j--;
}
cout<<"Chain "<<T++<<endl;
i=1;
for(auto ritx=rx.rbegin(),rity=ry.rbegin();ritx!=rx.rend();ritx++,rity++,i++)
if(*ritx!=*rity)
cout<<"Depot "<<i<<" at restaurant "<<(*ritx+*rity)/2<<" serves restaurants "<<*ritx<<" to "<<*rity<<endl;
else
cout<<"Depot "<<i<<" at restaurant "<<(*ritx+*rity)/2<<" serves restaurant "<<*ritx<<endl;
cout<<"Total distance sum = "<<dp[k]
<<endl<<endl;
}
return 0;
}


思路:

中规中矩的一道动态规划题目,比较好想。状态转移方程为

dp[j][i]=min(dp[j-1][k]+dis(k+1,j))

其中dis(i,j)表示第i个店到第j个店之间设置一个仓库供应的距离,仓库的位置就是中位数,然后累加i到j之间店的坐标到这个中位数的和。

dp[j][i]表示在前i个店当中设置j个仓库的最小距离

那么转移方程的意义就是前i家店当中设置j个仓库的最距离等于前k家店当中设置j-1个仓库,然后加上新加的第j个仓库后的最短距离即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: