- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
动态规划总结1.POJ 1160Post OfficeTime Limit: 1000MSMemory Limit: 10000KTotal Submissions: 17936Accepted: 9655DescriptionThere is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position of each village is identified with a single integer coordinate. There are no two villages in the same position. The distance between two positions is the absolute value of the difference of their integer coordinates. Post offices will be built in some, but not necessarily all of the villages. A village and the post office in it have the same position. For building the post offices, their positions should be chosen so that the total sum of all distances between each village and its nearest post office is minimum. You are to write a program which, given the positions of the villages and the number of post offices, computes the least possible sum of all distances between each village and its nearest post office. IntputYour program is to read from standard input. The first line contains two integers: the first is the number of villages V, 1 = V = 300, and the second is the number of post offices P, 1 = P = 30, P = V. The second line contains V integers in increasing order. These V integers are the positions of the villages. For each position X it holds that 1 = X = 10000.outputThe first line contains one integer S, which is the sum of all distances between each village and its nearest post office. Sample input10 51 2 3 6 7 9 11 22 44 50Sample output9题目大意:用数轴描述一条高速公路,有V个村庄,每一个村庄坐落在数轴的某个点上,需要选择P个村庄在其中建立邮局,要求每个村庄到最近邮局的距离和最小。题目分析:1、考虑在V个村庄中只建立【一个】邮局的情况,显然可以知道,将邮局建立在中间的那个村庄即可。也就是在a到b间建立一个邮局,若使消耗最小,则应该将邮局建立在(a+b)/2这个村庄上(可以通过画图知道)。2、下面考虑建立【多个】邮局的问题,可以这样将该问题拆分为若干子问题,在前i个村庄中建立j个邮局的最短距离,是在前【k】个村庄中建立【j-1】个邮局的最短距离与在【k+1】到第i个邮局建立【一个】邮局的最短距离的和。而建立一个邮局我们在上面已经求出。3、状态表示,由上面的讨论,可以开两个数组dp[i][j]:在前i个村庄中建立j个邮局的最小耗费sum[i][j]:在第i个村庄到第j个村庄中建立1个邮局的最小耗费那么就有转移方程:dp[i][j] = min(dp[i][j],dp[k][j-1]+sum[k+1][i]) ?DP的边界状态即为dp[i][1] = sum[1][i]; 所要求的结果即为dp[vil_nu
您可能关注的文档
最近下载
- 2019北京高三二模七选五汇编.docx VIP
- Unit 3 Food 知识清单(六大知识题型)总梳理(解析版).docx VIP
- 统编版道德与法治三年级上册第8课《同学相伴》教学设计.docx VIP
- 2025至2030年中国管道直饮水市场发展前景分析及行业投资规划建议报告.docx
- 单髁置换手术步骤.pptx VIP
- Unit 3 Food 知识清单(六大知识题型)总梳理(原卷版).docx VIP
- 教师评课用语缺点建议.doc VIP
- 2025年全国红十字应急救护竞赛活动考试题库300题(含答案).docx
- 互联网交往的利与弊-教学素材【课件】.ppt VIP
- 急性肾损伤的护理.pptx VIP
原创力文档


文档评论(0)