pair bike and pedestrian
Problem description:
In 2D space, m
people, n
bikes, with the following conditions
m< n
- does not exist two people that have same distance,
abs(x1-x2)+abs(y1-y2)
, between a bike. - everyone would find the bike that is the most closest to himself. If a bike is occupied, others can not use it.
1
2
3
4
5OPOBOOP
OOOOOOO
OOOOOOO
OOOOOOO
BOOBOOB
Solution guidline:
- 需要跟面试官讨论清楚他需要的最佳匹配是什么
如果是要求全局人车距离最短
- 二分图的最佳匹配问题,使用匈牙利算法,参考题目https://blog.csdn.net/u011721440/article/details/38169201
- 发现这里不能使用max flow, 因为这个bipartite is a weighted graph. 参考 https://www.topcoder.com/community/competitive-programming/tutorials/assignment-problem-and-hungarian-algorithm/
如果是要求最佳匹配只是给每个人匹配到车,可以用PQ+Map
PQ
1 |
|
time complexity: $O()$
space complexity: $O()$
reference:
http://www.cnblogs.com/jackge/archive/2013/05/03/3057028.html
https://www.careercup.com/question?id=5687196447670272