Farmer John’s farm has $N$ barns, and there are some cows that live in each barn. The cows like to drop around, so John wants to build some roads to connect these barns. If he builds roads for every pair of different barns, then he must build $N * (N - 1) / 2$ roads, which is so costly that cheapskate John will never do that, though that’s the best choice for the cows.
Clever John just had another good idea. He first builds two transferring point $S1$ and $S2$, and then builds a road connecting $S1$ and $S2$ and $N$ roads connecting each barn with $S1$ or $S2$, namely every barn will connect with $S1$ or $S2$, but not both. So that every pair of barns will be connected by the roads. To make the cows don’t spend too much time while dropping around, John wants to minimize the maximum of distances between every pair of barns.
That’s not the whole story because there is another troublesome problem. The cows of some barns hate each other, and John can’t connect their barns to the same transferring point. The cows of some barns are friends with each other, and John must connect their barns to the same transferring point. What a headache! Now John turns to you for help. Your task is to find a feasible optimal road-building scheme to make the maximum of distances between every pair of barns as short as possible, which means that you must decide which transferring point each barn should connect to.
We have known the coordinates of $S1$, $S2$ and the N barns, the pairs of barns in which the cows hate each other, and the pairs of barns in which the cows are friends with each other.
Note that John always builds roads vertically and horizontally, so the length of road between two places is their Manhattan distance. For example, saying two points with coordinates $(x1, y1)$ and $(x2, y2)$, the Manhattan distance between them is $|x1 - x2| + |y1 - y2|$.
链接
题解
相互讨厌是 $!((a$ and $b)$ or $(!a$ and $!b)$ 化简得 $(!a$ or $!b)$ and $(a$ or $b)$ 相互喜欢是 $!(a$ and $!b)$ or $(!a$ and $b)$ 化简得 $(!a$ or $b)$ and $(a$ or $!b)$,二分答案就好。
代码
1 |
|