[AGC034D] Manhattan Max Matching
Posted frank3215的小尛博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[AGC034D] Manhattan Max Matching相关的知识,希望对你有一定的参考价值。
https://atcoder.jp/contests/agc034/tasks/agc034_d
Time Limit: 5 sec / Memory Limit: 1024 MB
Score : \\(1200\\) points
Problem Statement
Snuke is playing with red and blue balls, placing them on a two-dimensional plane.
First, he performed \\(N\\) operations to place red balls. In the \\(i\\)-th of these operations, he placed \\(RC_i\\) red balls at coordinates \\((RX_i,RY_i)\\). Then, he performed another \\(N\\) operations to place blue balls. In the \\(i\\)-th of these operations, he placed \\(BC_i\\) blue balls at coordinates \\((BX_i,BY_i)\\). The total number of red balls placed and the total number of blue balls placed are equal, that is, \\(\\sum_{i=1}^{N} RC_i = \\sum_{i=1}^{N} BC_i\\). Let this value be \\(S\\).
Snuke will now form \\(S\\) pairs of red and blue balls so that every ball belongs to exactly one pair. Let us define the score of a pair of a red ball at coordinates \\((rx, ry)\\) and a blue ball at coordinates \\((bx, by)\\) as \\(|rx-bx| + |ry-by|\\).
Snuke wants to maximize the sum of the scores of the pairs. Help him by finding the maximum possible sum of the scores of the pairs.
Constraints
- \\(1 \\leq N \\leq 1000\\)
- \\(0 \\leq RX_i,RY_i,BX_i,BY_i \\leq 10^9\\)
- \\(1 \\leq RC_i,BC_i \\leq 10\\)
- \\(\\sum_{i=1}^{N} RC_i = \\sum_{i=1}^{N} BC_i\\)
- All values in input are integers.
解答
明显用网络流……但是图太大了!而且曼哈顿距离暴难处理!!
……然后想到拆点:\\((x_1, y_1), (x_2, y_2)\\)之间的曼哈顿距离为
之后,就只有在整体上取\\(\\max\\),随便建图就可以了。
Editorial
https://img.atcoder.jp/agc034/editorial.pdf
Bonus: This problem can also be solved in \\(O(S \\log N)\\) time. 见 https://codeforces.com/blog/entry/67345?#comment-515285
Without affecting the score, we can assume that instead of getting the score of |rx − bx| + |ry − by| for a pair, we can choose one of the following scores for a pair:
- (rx − bx) + (ry − by) = (rx + ry) + (−bx − by)
- −(rx − bx) + (ry − by) = (−rx + ry) + (bx − by)
- (rx − bx) − (ry − by) = (rx − ry) + (−bx + by)
- −(rx − bx) − (ry − by) = (−rx − ry) + (bx + by)
For each ball, let us decide in advance which of these four to use.
That is, we will classify the red balls into the following four types:
- Type 0: adds (rx + ry) to the score.
- Type 1: adds (−rx + ry) to the score.
- Type 2: adds (rx − ry) to the score.
- Type 3: adds (−rx − ry) to the score.
We will also classify the blue balls into the following four types:
- Type 0: adds (−bx − by) to the score.
- Type 1: adds (bx − by) to the score.
- Type 2: adds (−bx + by) to the score.
- Type 3: adds (bx + by) to the score.
Then, we can form the pairs if the number of red balls and that of blue balls are equal for each type.
We want to find the maximum score of such a classification.
We can solve it as a minimum-cost flow problem. Let us build a graph with 1 (source) +N (operations with red balls) +4 (types) +N (operations with blue balls) +1 (sink) vertices and the following edges:
- from the source to each “operations with red balls” vertex: an edge of capacity RCi and cost 0
- from each “operations with red balls” vertex to each “type” vertex: an edge of capacity ∞ and cost −(the score above)
- from each “type” vertex to each “operations with blue balls” vertex: an edge of capacity ∞ and cost −(the score above)
- from each “operations with blue balls” vertex to the sink: an edge of capacity BCi and cost 0
Then send the flow of S (the number of balls). We can resolve negative costs by adding some offset to
each edge.
There are O(N) edges, and the amount of flow is O(S), so the time complexity is O(SN log N).
以上是关于[AGC034D] Manhattan Max Matching的主要内容,如果未能解决你的问题,请参考以下文章