设施位置的动态规划算法

时间:2015-10-16 18:49:51

标签: algorithm dynamic-programming

沿着一条线a_1,a_2,...,a_n的位置有n个房屋。我们想在同一条线上设置porta potties,这样每个房子都在至少一个porta potty的距离R内。这些porta potties仅限于指定位置b_1,b_2,...,b_m。设c_i是在位置b_i设置porta便盆的成本。

找到一种动态编程算法,可以最大限度地降低设置porta potties的总成本。该算法应该能够检测解决方案是否不存在。假设所有a和b值都是不同的。

输入:

  • A [1,2,... n]占有房屋位置

  • B [1,2,... m]拥有潜在的porta便盆位置

  • C [1,2,... m]承担在每个设置porta便盆的费用 位置

输出:在每个房屋必须在一些便盆的距离R内的约束条件下放置porta便盆的最低成本

我无法找出一个递归表达式来解决问题。任何帮助,将不胜感激!

4 个答案:

答案 0 :(得分:1)

您的问题让我有机会为类似的问题编写一些代码,这些代码通常会显示为手机信号灯放置问题手机基座覆盖问题

伪代码如下:

1) Sort houses in ascending order
2) Sort facilities positions and their costs in ascending order by facilities positions
3) Let dp(i) be the minimum cost to cover i houses and lastBase(j) the last base used to cover j houses
4) Set the base case dp(0) = 0 and lastBase(0) = -1
5) For each house i:
6)   Check if previous solution is either valid or in range of this new house
7)   if it is -> grab it as solution
8)   else
9)     find a new base starting from lastBase(i) + 1 which can cover this house
10)    let it be the minimum-cost one
11)  if a base could be found -> add it to the previous solution
12)  else -> Problem cannot be solved

我建议先自己尝试一下。

为了完整性'缘故:解释,图像和C ++代码are available here

欢迎提供反馈或错误。

答案 1 :(得分:0)

我将向您介绍如何进行操作,您的编码方式取决于您 给定BCA(也假设B-> Sort A in ascending order. -> Sort B and C together(as they are dependent) based on B's values in ascending order. -> Maintain a temp array(size n) which keeps track of which "porta potty" an A element belongs to,by mapping to the "porta potty" index. -> Now take each element from B and move both forward and backward R steps from that point on the number line. -> If any A element is found in those R steps(on the number line) AND if(and only if) it does not presently belong to any "porta potty" OR the cost of setting up the current "porta potty" element is more than the "porta potty" it(A element) already belongs to, then only shall you set the value in temp array for that A element to the current "porta potty" index number. -> Now once we are done with all B points, do the following -> Traverse the temp array and push the "porta potty" index numbers we have into a set -> You now have a list of all the "porta potty" indices which are the cheapest yet crucial to be placed. 中的所有元素都在数字行上) -

{{1}}

想一想,如果您不清楚某些事情,请告诉我。排序部分也只是为了提高性能。

答案 2 :(得分:0)

这是针对手机塔放置问题。我想你的应该是相似的。

cellphone tower placing problem

答案 3 :(得分:0)

应该有一个递归。这是Python中的一个注释示例,它假设已排序的输入:

a = [1, 7,11,13,15]
b = [1,8,9,12,13]
c = [1,3,2, 2, 5]
r = 3

na = len(a)
nb = len(b)

def f (ia,ib,prev_ib,s):
  # base case no suitable potties
  if ib == nb:
    return 1000  # a number larger than sum of all costs

  # base case end of row of houses
  if ia == na:
    return s

  # house is in range of last potty
  if prev_ib >= 0 and abs(a[ia] - b[prev_ib]) < r:
    return f(ia + 1,ib,prev_ib,s)

  # house is too far
  if abs(a[ia] - b[ib]) >= r:
    # house is west of potty
    if a[ia] < b[ib]:
      return 1000
    # house is east of potty
    else:
      return f(ia,ib + 1,prev_ib,s)

  # house is in range of current potty
  else: 
    # choose or skip
    return min(f(ia + 1,ib + 1 if ib < nb - 1 else ib,ib,s + c[ib]),f(ia,ib + 1,prev_ib,s))

输出:

print f(0,0,-1,0) # 8