| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1676 人关注过本帖
标题:[求助]ACM的题!!一个我没有读懂的题!!
只看楼主 加入收藏
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
收藏
 问题点数:0 回复次数:13 
[求助]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
我不是郭靖
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:494
专家分:6
注 册:2006-10-4
收藏
得分:0 
动态规划问题.貌似有点难度

2006-11-11 19:44
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
我不是郭靖
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:494
专家分:6
注 册:2006-10-4
收藏
得分:0 

大概意思是:McBurger快餐公司拥沿着高速公路有几家快餐店.最近,他们决定沿着公路建几间仓库.每个

仓库都建在快餐店.这些仓库为快餐店提供补给.
要求:所有仓库到所供给快餐店的总路程和最短.
输入:第1行:n,k
n表示快餐店总数,k表示仓库总数
第2行到第n+1行输入快餐店在高速公路上的位置(坐标)
输入0 0 表示结束
Sample Input

6 3
5
6
12
19
20
27
0 0

Sample Output

Chain 1
Total distance sum = 8

表示3个仓库分别建在6,20,27位置.路程和最小
5 6 12 19 20 27

6为5,6,12补给,路程和为1+0+6=7;
20为19,20补给,路程和为1+0=1;
27为27仓库补给,路程和为0
总路程为7+1+0=8


2006-11-11 21:56
我不是郭靖
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:494
专家分:6
注 册:2006-10-4
收藏
得分:0 



#include<stdio.h>
#include<math.h>
#define N 201
#define M 31
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;
}
}
int mindis(int (*p)[N],int (*dp)[M],int n,int k)
{
int i,j,t,min,temp;
for(i=1;i<=n;i++)
for(j=1;j<=k;j++)
{
min=1<<30;
if (j==1)
dp[i][j]=p[1][i];
else
{
for(t=j-1;t<i;t++)
{
temp=dp[t][j-1]+p[t+1][i];
if(temp<min)
min=temp;
}
dp[i][j]=min;
}
}
return dp[n][k];
}


int main()
{
int v[N][N],pos[M],dp[N][M];
int n,k,i,count=0;
while(EOF!=scanf("%d%d",&n,&k))
{
if(n==0 && k==0)
break;
for(i=1;i<=n;i++)
scanf("%d",&pos[i]);
dis(v,pos,n);
count++;
printf("Chain %d\n",count);
printf("Total distance sum = %d\n\n",mindis(v,dp,n,k));
}
return 0;

}


2006-11-11 22:31
快速回复:[求助]ACM的题!!一个我没有读懂的题!!
数据加载中...
 
   



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

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