P20315 [CodeChef] MATCHIT
提高+
题目描述

Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.

These days, Chef has been playing a game.

The game is played on a grid with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$); let's denote the cell in row $r$ and column $c$ by $(r, c)$. $2M$ cells $(X_{1}, Y_{1}), (X_{2}, Y_{2}), \ldots, (X_{2M}, Y_{2M})$ in the grid are marked.

Chef wants to add exactly $M$ paths between pairs of distinct marked cells in such a way that no two paths intersect. A path from cell $(X_{i}, Y_{i})$ to cell $(X_{j}, Y_{j})$ is a sequence of distinct cells $(U_{1}, V_{1}), (U_{2}, V_{2}), \ldots, (U_{L}, V_{L})$ such that $(X_{i}, Y_{i})$ is the first cell and $(X_{j}, Y_{j})$ the last cell in this sequence and for each valid $i$, cells $(U_{i}, V_{i})$ and $(U_{i+1}, V_{i+1})$ share a side. Two paths intersect if there is some cell which is in both paths.

However, this seems too easy to Chef, so he added weights to all cells. Let's denote the weight of cell $(r, c)$ by $V_{r, c}$. You should find $M$ paths as described above such that the sum of weights of all cells in all paths is as large as possible.

------ Input ------
The first line of the input contains two space-separated integers $N$ and $M$.
$2M$ lines follow. For each $i$ ($1 ≤ i ≤ 2M$), the $i$-th of these lines contains two space-separated integers $X_{i}$ and $Y_{i}$.
$N$ more lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $N$ space-separated integers $V_{i, 1}, V_{i, 2}, \ldots, V_{i, N}$.

------ Output ------
Print $M$ lines. Each of these lines should contain an integer $L$, followed by a space and $2L$ space-separated integers $U_{1}, V_{1}, U_{2}, V_{2}, \ldots, U_{L}, V_{L}$ describing one of your chosen paths. The paths may not intersect and each path must begin and end at distinct marked cells.

------ Constraints ------
$500 ≤ N ≤ 1,000$
$500 ≤ M ≤ 1,000$
$1 ≤ X_{i}, Y_{i} ≤ N$ for each valid $i$
$|V_{i, j}| ≤ 10^{9}$ for each valid $i, j$
$(X_{1}, Y_{1}), (X_{2}, Y_{2}), \ldots, (X_{2M}, Y_{2M})$ are pairwise distinct

------ Example Input ------

4 2
1 1
2 2
3 3
4 4
1 -1 -1 -1
1 1 1 1
1 1 1 1
-1 -1 -1 1

------ Example Output ------

5 1 1 2 1 3 1 3 2 3 3
5 4 4 3 4 2 4 2 3 2 2

------ Scoring ------
The score for each test case (and therefore each test file) is $(S / (10^{9} \cdot N^{2}) + 1)^6$, where $S$ is the sum of weights of cells in all paths in your solution. The score of a submission is the sum of its scores for all test files. For the example output, the score would be $(10 / (16 \cdot 10^{9}) + 1)^6 \doteq 1$.

If your output is invalid, the verdict of your submission will be Wrong Answer.

Test Generation Process
There are twenty test cases. During the contest, the displayed score will account for exactly four test files, i.e. your score reflects your submission's performance on 20% (4/20) of the test files. However, if your program gets a non-AC verdict on any test file, your submission's verdict will be non-AC. In other words, an AC verdict denotes that your program runs successfully on all the test files. After the end of the contest, your score will be changed to include the sum of your program's scores over the other sixteen test files.

The pseudocode used for generating all tests is given below. Assume that the function rand(l, r) generates a uniformly random integer between $l$ and $r$ (both inclusive).

N := rand(500, 1000)
M := rand(500, 1000)
for i in 1..2M:
X[i] := rand(1, N)
Y[i] := rand(1, N)
while (X[j], Y[j]) == (X[i], Y[i]) for some j smaller than i:
X[i] := rand(1, N)
Y[i] := rand(1, N)
for x in 1..N:
for y in 1..N:
V[x][y] := rand(-1000000000, 1000000000)

输入格式

见题面

输出格式

见题面

样例输入
4 2 1 1 2 2 3 3 4 4 1 -1 -1 -1 1 1 1 1 1 1 1 1 -1 -1 -1 1
样例输出
5 1 1 2 1 3 1 3 2 3 3 5 4 4 3 4 2 4 2 3 2 2