#P3967. [TJOI2014] 匹配

    ID: 2899 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2014各省省选枚举,暴力二分图费用流天津

[TJOI2014] 匹配

Description

There are NN single boys and NN single girls. The happiness of pairing boy ii with girl jj is Hi,jH_{i,j}.

A matching is an arrangement of these NN boys and girls such that each boy has exactly one girlfriend and each girl has exactly one boyfriend.

The happiness of a matching is the sum of the happiness values of these NN pairs.

The classic task is to compute the maximum possible happiness, i.e., a maximum-weight perfect matching (also called an "assignment"). However, the maximum-weight perfect matching is not always unique. You need to compute the intersection of all maximum-weight perfect matchings.

Input Format

The first line contains a positive integer NN.

Then follows an N×NN \times N matrix HH, where Hi,jH_{i,j} denotes the happiness of pairing boy ii with girl jj.

Output Format

On the first line, output the happiness of the maximum-weight perfect matching. Then output several lines, each containing a pair of integers ii and jj, indicating that boy ii and girl jj appear in the intersection of all maximum-weight perfect matchings, in increasing order of ii.

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

Hint

  • For 30% of the testdata, N30N \leq 30.
  • For 100% of the testdata, 1N801 \leq N \leq 80, 0Hi,j5×1030 \leq H_{i,j} \leq 5 \times 10^3.

Translated by ChatGPT 5