Problem 1005 Fast Food
2014-04-10 23:40
197 查看
题目:
Problem 1005 Fast Food
Accept: 714 Submit: 1980
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.
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.
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.
6 356121920270 0
Chain 1Total distance sum = 8//题目摘自http://acm.fzu.edu.cn/problem.php?pid=1005
Problem 1005 Fast Food
Accept: 714 Submit: 1980
Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem 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
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 containingone 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 356121920270 0
Sample Output
Chain 1Total distance sum = 8//题目摘自http://acm.fzu.edu.cn/problem.php?pid=1005内容是学习http://blog.csdn.net/e6894853/article/details/9791249 然后个别优化计算,中间出现depot开得不过大,原来[31][222]出现-》runtime error 错误,后面艰难地发现了,结果,竟然输出格式错误,少了换行
#include<stdio.h> int main() { int depot[222][222],min[31][222],n,k,front,now,putin,j,add=0; while((scanf("%d%d",&n,&k)),n!=0) { add++; for(putin=1;putin<=n;putin++) {scanf("%d",&depot[0][putin]);} for(front=1;front<=n;front++) { depot[front][front]=0; j=n-k+1+front; for(now=1+front;now<=j;now++) { depot[front][now]=depot[front][now-1] +depot[0][now]-depot[0][(now+front)/2]; } } for(putin=1;putin<=n-k+1;putin++)/*n-k+1 中的+1*/ { min[1][putin]=depot[1][putin]; } for(putin=2;putin<=k;putin++) { min[putin][putin]=0; j=n-k+1+putin; for(now=putin+1;now<=j;now++) { min[putin][now]=100000000; for(front=putin-1;front<now;front++) { depot[0][0]=min[putin-1][front]+depot[front+1][now]; if(depot[0][0]<min[putin][now])min[putin][now]=depot[0][0]; } } } printf("Chain %d\nTotal distance sum = %d\n\n",add,min[k] ); } return 0; }
相关文章推荐
- Number Sequence(http://acm.hdu.edu.cn/showproblem.php?pid=1005)
- 三分 Problem 1005 Turn the corner 车转向问题
- DP 动态规划 Problem E 1005 最大递增子序列
- 多校7 1005 The shortest problem
- problem 1005
- Problem - 1005 Euler theoremt(杭电多校签到题)
- hdu4975 A simple Gaussian elimination problem.(多校第十场1005)
- 集训队专题(2)1005 A Simple Math Problem
- ZOJ Problem Set - 1005
- ZOJ Problem Set - 1005 Jugs
- problem 1005
- 2013年4月_武科大程序设计大赛(复赛-非专业组)_解题报告(problem 1005)
- FOJ-1005 Fast Food
- 【ACM】hdu_zs2_1005_Problem E _201308030747
- Problem3-1005
- http://acm.fafu.edu.cn/showproblem?problem_id=1005食物链 (第一次用类写,居然过了
- 题解西电OJ (Problem 1005 -跳舞毯)--动态规划
- HDU 4975 (杭电多校 #10 1005题)A simple Gaussian elimination problem.(网络流之最大流)
- ZOJ Problem Set - 1005
- 搜索—Problem_1005-Turn the corner