CareerCup Balanced Fermat Point

There is a matrix, m x n. Several groups of people locate at some certain spots. In the following example, there are three groups and the number 4 indicates there are four people in this group. Now we want to find a meeting point in the matrix so that the cost of all groups moving to that point is the minimum. As for how to compute the cost of moving one group to another point, please see the following example. 

Group1: (0, 1), 4 

Group2: (1, 3), 3 

Group3: (2, 0), 5 

. 4 . . 

. . . 3 

5 . . . 

If all of these three groups moving to (1, 1), the cost is: 4*((1-0)+(1-1)) + 5*((2-1)+(1-0))+3*((1-1)+(3-1)) 

My idea is : 

Firstly, this two dimensional problem can be reduced to two one dimensional problem. In the one dimensional problem, I can prove that the best spot must be one of these groups. In this way, I can give a O(G^2) algorithm.(G is the number of group). 

Use iterator's example for illustration: 

{(-100,0,100),(100,0,100),(0,1,1)},(x,y,population) 

for x, {(-100,100),(100,100),(0,1)}, 0 is the best. 

for y, {(0,100),(0,100),(1,1)}, 0 is the best. 

So it's (0, 0) 

Is there any better solution for this problem. 

Thanks, 


------------------------------------------------

It could be treated as a problem of Fermat Problem with weights. If the coordinates of nodes aren't float, look at the perspective that the cost of moving a group of people is a (distanceX+distanceY)*amount. So we need to calculate the minimal cost moving by X and then by Y axis. The complexity is O(m + n)






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值