您的位置:首页 > Web前端

CareerCup Balanced Fermat Point

2014-03-12 12:38 134 查看
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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: