博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(记忆化搜索)Jury Compromise (poj 1015)
阅读量:6312 次
发布时间:2019-06-22

本文共 4040 字,大约阅读时间需要 13 分钟。

 

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 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

Sample Output

Chain 1 Total distance sum = 8
 
题目大意:
 
给你 n k ,n 代表有 n  个点, 让你从中选出 k 个点, 求这 n 个点到达这 k 个点中的任何一个点的距离总和最小 
 
 
n个旅馆和k个补给站的问题
假设有3个旅馆坐标分别是 1, 4, 5, 和2个补给站,那么路程代价就是1了,一个补给站放在坐标为1的旅馆那,令一个放在4位置处。
也可以一个补给站放在坐标为 1 的旅馆那,令一个放在 5 位置处。
 
//dp[i][k]表示前i个店添加k个供应点所达到的最小值
//状态转移方程为:dp[i][k] = min(dp[j][k-1], sum[j+1][i]),
//其中k-1 <= j <= i-1, sum[i][j]表示从第i个饭店到第j个饭店添加一个供应点所达到的最小值,取i,j中间值即可
 
 
 
#include 
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define N 220#define MOD 1000000007#define met(a, b) memset(a, b, sizeof(a))#define INF 0x3f3f3f3fint dp[N][N], sum[N][N], a[N];int main(){ int n, m, iCase=1; while(scanf("%d%d", &n, &m), n||m) { int i, j, k; for(i=1; i<=n; i++) scanf("%d", &a[i]); for(i=1; i<=n; i++) { sum[i][i] = 0; for(j=i+1; j<=n; j++) { sum[i][j] = sum[i][j-1] + a[j] - a[(i+j)/2]; } } for(i=0; i<=n; i++) for(j=0; j<=m; j++) dp[i][j] = INF; dp[0][0] = 0; for(i=1; i<=n; i++) { for(k=1; k<=m; k++) { for(j=k-1; j

 记忆化搜索:

#include 
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define N 220#define met(a, b) memset(a, b, sizeof(a))#define INF 0xffffffconst long long Max = 2000000000;typedef long long LL;int a[N], sum[N][N], dp[N][N];int n, m;int DFS(int x, int y){ int j; if(x<0 || y<0) return INF; if(dp[x][y]!=INF) return dp[x][y]; if(y>=x) ///这点我还是想不到 { dp[x][y] = 0; return 0; } for(j=1; j<=x; j++) ///在 [1,x] 中选择一个点作为补给站 dp[x][y] = min(dp[x][y], DFS(j-1, y-1) + sum[j][x]); return dp[x][y];}int main(){ int iCase=1; while(scanf("%d%d", &n, &m), n||m) { int i, j; met(sum, 0); met(a, 0); for(i=1; i<=n; i++) scanf("%d", &a[i]); for(i=1; i<=n; i++) for(j=i+1; j<=n; j++) sum[i][j] = sum[i][j-1] + a[j]-a[(i+j)/2]; for(i=0; i<=n; i++) for(j=0; j<=m; j++) dp[i][j] = INF; dp[n][m] = DFS(n, m); printf("Chain %d\n", iCase++); printf("Total distance sum = %d\n\n", dp[n][m]); } return 0;}

 

转载于:https://www.cnblogs.com/YY56/p/5463818.html

你可能感兴趣的文章
大厂前端高频面试问题与答案精选
查看>>
我们用5分钟写了一个跨多端项目
查看>>
Visual Studio 15.4发布,新增多平台支持
查看>>
有赞透明多级缓存解决方案(TMC)设计思路
查看>>
如何设计高扩展的在线网页制作平台
查看>>
Git 2.5增加了工作树、改进了三角工作流、性能等诸多方面
查看>>
Swift 5将强制执行内存独占访问
查看>>
中台之上(二):为什么业务架构存在20多年,技术人员还觉得它有点虚?
查看>>
深度揭秘腾讯云低功耗广域物联网LPWAN 技术及应用
查看>>
与Jeff Sutherland谈敏捷领导力
查看>>
More than React(四)HTML也可以静态编译?
查看>>
React Native最佳学习模版- F8 App开源了
查看>>
云服务正在吞噬世界!
查看>>
阅读Android源码的一些姿势
查看>>
Web语义化标准解读
查看>>
一份代码构建移动、桌面、Web全平台应用
查看>>
高性能 Lua 技巧(译)
查看>>
区分指针、变量名、指针所指向的内存
查看>>
异步编程的世界
查看>>
最近话题火爆的四件事你知道不?
查看>>