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)
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)
相关文章推荐
- CareerCup Fermat point of a traingle
- careercup1.3: remove duplicate without extra space.
- Recursion 八皇后问题 @CareerCup
- Sorting&Searching 二分法找有空字符串的数组 @CareerCup
- Moderate 猜测序列hit或是pseudo-hit @CareerCup
- Hard 在字符串S中查找T中的字符串 @CareerCup
- CareerCup Arrange the 2 x 1 boards on the 2 x n board
- LinkLists 删除链表中的一个给定指针的结点 @CareerCup
- Tree_Graph 判断是否平衡二叉树 @CareerCup
- CareerCup Find at-least one triplet such that A[n-1] >= A[n] <= A[n+1].
- CareerCup Insert 0s to maximize A*B
- CareerCup chapter 5 Bit Manipulation
- CareerCup chapter 8 Recursion
- CareerCup Chapter 4 Trees and Graphs
- Careercup面试题
- careercup-C和C++ 13.8
- careercup1.3: remove duplicate without extra space.
- careercup top 150判断字符串中字符是否唯一(字符不重复)
- CareerCup-1.6
- Recursion 叠箱子最高问题 @CareerCup