# 描述

As you know, all the computers used for ACM contests must be identical, so the participants compete on equal terms. That is why all these computers are historically produced at the same factory.

Every ACM computer consists of $P$ parts. When all these parts are present, the computer is ready and can be shipped to one of the numerous ACM contests.

Computer manufacturing is fully automated by using N various machines. Each machine removes some parts from a half-finished computer and adds some new parts (removing of parts is sometimes necessary as the parts cannot be added to a computer in arbitrary order). Each machine is described by its performance (measured in computers per hour), input and output specification.

Input specification describes which parts must be present in a half-finished computer for the machine to be able to operate on it. The specification is a set of $P$ numbers $0$, $1$ or $2$ (one number for each part), where $0$ means that corresponding part must not be present, $1$ — the part is required, $2$ — presence of the part doesn’t matter.

Output specification describes the result of the operation, and is a set of $P$ numbers $0$ or $1$, where $0$ means that the part is absent, $1$ — the part is present.

The machines are connected by very fast production lines so that delivery time is negligibly small compared to production time.

After many years of operation the overall performance of the ACM Computer Factory became insufficient for satisfying the growing contest needs. That is why ACM directorate decided to upgrade the factory.

As different machines were installed in different time periods, they were often not optimally connected to the existing factory machines. It was noted that the easiest way to upgrade the factory is to rearrange production lines. ACM directorate decided to entrust you with solving this problem.

## Input

Input file contains integers $P \ N$, then $N$ descriptions of the machines. The description of ith machine is represented as by $2 P + 1$ integers $Q_i\ S_{i,1}\ S_{i,2}…S_{i,P}\ D_{i,1}\ D_{i,2}…D_{i,P}$, where $Q_i$ specifies performance, $S_{i,j}$ — input specification for part $j$, $D_{i,k}$ — output specification for part $k$.

• Constraints
$1 ≤ P ≤ 10, 1 ≤ N ≤ 50, 1 ≤ Q_i ≤ 10000$

## Output

Output the maximum possible overall performance, then $M$ — number of connections that must be made, then $M$ descriptions of the connections. Each connection between machines $A$ and $B$ must be described by three positive numbers $A\ B\ W$, where $W$ is the number of computers delivered from $A$ to $B$ per hour.

If several solutions exist, output any of them.

# 思路

• 网络流都是作文题……题目看懂建出图就是跑算法0.0
• 题意：每台机器有工作的零件输入要求和输出情况，以及最大产量，问如何调度才能使得总产量最高。
• 由于机器有产量限制，所以我们要把机器拆成两个点（很经典的套路了），具体建模方式：
• 超级源点：$0$；
• 机器输入端：$1\sim n$；
• 机器输出端：$n+1\sim 2n$:
• 超级汇点：$2n+1$。
• 每个机器的输入端与输出端相连，容量为机器的产量限制；
• 如果该机器的输入端没有必须的零件（没有$1$），则超级源点与它相连，容量为$+\infty$；
• 如果该机器能生产所有零件，则让它与超级汇点相连，容量为$+\infty$；
• 输出零件与输入零件相匹配的机器之间建边，输出→输入，容量为$+\infty$。
• 这个题目难点在于建图的实现和输出方案，建图的实现我开了结构体各种成员函数去判断。输出方案的话，只要在网络中跑完最大流后找到流量为正的边即可。（注意只要输出有实际意义的边，拆点而建的边和与源汇相连的边是不用输出的）

0%