标题:[求助]ACM的题!!一个我没有读懂的题!!
取消只看楼主
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
 问题点数:0 回复次数:8 
[求助]ACM的题!!一个我没有读懂的题!!

看了好几遍,愣是没懂是什么意思!大家看看,到底是什么意思!知道了,告诉我一声,呵呵,然后咱们一起做!
Fast Food

--------------------------------------------------------------------------------

Time limit: 3sec. Submitted: 477
Memory limit: 32M Accepted: 102

Source : Unknown

--------------------------------------------------------------------------------

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


[此贴子已经被作者于2006-11-11 18:13:33编辑过]

搜索更多相关主题的帖子: ACM limit The highway 
2006-11-11 14:46
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
得分:0 


Sample Output

Chain 1
Total distance sum = 8



该学习了。。。
2006-11-11 15:00
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
得分:0 

没有人看一下吗?


该学习了。。。
2006-11-11 15:57
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
得分:0 

这是我找到的代码,但是还是看不懂啊!希望大家看看!谢谢了!
#include <stdio.h>
#include <stdlib.h>

int line[202];
int dis[202][202];
int dpbuf[32][202];
int chain[202];
int sum[202];

int myabs(int a,int b)
{
if(a < b)
return b-a;
return a-b;
}

void distance(int rnum)
{
int depth = 0;
int i =0;

for(depth = 0; depth < rnum;depth++)
{
for(i = 1; i <= rnum;i++)
{
if(depth <= 1)
{
dis[i][i+depth] = myabs(line[i],line[i+depth]);
}
else
{
dis[i][i+depth] = dis[i][i+depth-1] + dis[i+1][i+depth];
}
}
}
}
/*
dpbuf[1][rnum] = dis[1][rnum]
dpbuf[knum][rnum] = min(dpbuf[knum-1][m] + dis[m+1][rnum])
*/
int dp(int rnum,int knum)
{
int i;
for(i = 1; i <= rnum; i++)
{
dpbuf[1][i] = dis[1][i];
}
for(i = 2;i <= knum;i++)
{
int m;
for(m = i;m <= rnum;m++)
{
int min = 65535;
int l = 0;
for(l = i-1; l < m; l++)
{
int tmp = 0;
tmp = dpbuf[i-1][l] + dis[l+1][m];
if(tmp < min)
min = tmp;
}
dpbuf[i][m] = min;
// printf("dpbuf[%d][%d] = %d\n",i,m,dpbuf[i][m]);
}
}
return dpbuf[knum][rnum];
}

int main()
{
int rnum,knum;
int counter = 0;
int ct = 0;
int i;
int ret = 0;
rnum = 0;
knum = 0;

while(scanf("%d %d",&rnum,&knum) && rnum && knum)
{
int i = 0;
if(rnum < 0 || rnum > 200)
continue;
if(knum < 0 || knum > 30 || knum > rnum)
continue;
for(i = 1; i <= rnum;i++)
{
int tmp = 0;
scanf("%d",&tmp);
line[i] = tmp;
}
distance(rnum);
ret += dp(rnum,knum);
sum[ct] = ret;
chain[ct] = ++counter;
ct++;
printf("Chain %d\n",counter);
printf("Total distance sum = %d\n\n",ret);
}
/*
for(i = 0; i < ct; i++)
{
printf("Chain %d\n",chain[i]);
printf("Total distance sum = %d\n\n",sum[i]);
}
*/
return 0;
}


该学习了。。。
2006-11-11 16:24
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
得分:0 

n
∑ =| di – (position of depot serving restaurant i)| 前两个i 都是下标。n 是上面的
i=1

这是求和的。复制不下来,只好自己弄了!


该学习了。。。
2006-11-11 16:35
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
得分:0 

这是什么问题!请说明一下!呵呵!我还真的不懂啊!


该学习了。。。
2006-11-11 20:44
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
得分:0 
不好意思版主,我要下机了!先谢谢你!明天我会看你的答复的。

该学习了。。。
2006-11-11 20:54
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
得分:0 
呵呵!通过了!

该学习了。。。
2006-11-12 10:06
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
得分:0 
麻烦版主说一下这个函数是做什么 用的?p[i][j]这个不用定义吗?(*p)[N]这个是什么意思啊?呵呵!我今天刚看到 指针这里。
void dis(int (*p)[N],int *pos,int n)
{
int i,j,k,s;
for(i=1;i<=n;i++)
for(j=i;j<=n;j++)
{
s=0;
for(k=i;k<=j;k++)
s+=abs(pos[k]-pos[(i+j)/2]);
p[i][j]=s;
}
}

该学习了。。。
2006-11-12 11:45



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-102245-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.494489 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved