Description:

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 QiSi,1 Si,2…Si,P Di,1 Di,2…Di,P, where Qi specifies performance, Si,j — input specification for part j, Di,k — output specification for part k.

Constraints

1 ≤ P ≤ 10, 1 ≤ N ≤ 50, 1 ≤ Qi ≤ 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.

Sample Input:

Sample input 1
3 4
15 0 0 0 0 1 0
10 0 0 0 0 1 1
30 0 1 2 1 1 1
3 0 2 1 1 1 1
Sample input 2
3 5
5 0 0 0 0 1 0
100 0 1 0 1 0 1
3 0 1 0 1 1 0
1 1 0 1 1 1 0
300 1 1 2 1 1 1
Sample input 3
2 2
100 0 0 1 0
200 0 1 1 1

Sample Output:

Sample output 1
25 2
1 3 15
2 3 10
Sample output 2
4 5
1 3 3
3 5 3
1 2 1
2 4 1
4 5 1
Sample output 3
0 0

题目链接

若一个机器输入零件规格没有1,则将它与源点相连,流量为其效率,若一个机器输出零件规格没有0,则将它与汇点相连,流量为其效率,若一个机器的零件输出规格和另一个机器的零件输入规格相符,将两个机器相连,流量设置为无穷大,跑最大流即可。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
#define lowbit(x) (x&(-x))
#define XDebug(x) cout<<#x<<"="<<x<<endl;
#define ArrayDebug(x,i) cout<<#x<<"["<<i<<"]="<<x[i]<<endl;
#define print(x) out(x);putchar('\n')
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
typedef pair<ll,ll> PLL;
const int INF = 0x3f3f3f3f;
const int maxn = 5e1 + 5;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = asin(1.0) * 2;
const double e = 2.718281828459;
template <class T>
inline bool read(T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) {
        return false;
    }
    while (c != '-' && (c < '0' || c > '9')) {
        c = getchar();
    }
    sgn = (c == '-') ? -1 : 1;
    ret = (c == '-') ? 0 : (c - '0');
    while (c = getchar(), c >= '0' && c <= '9') {
        ret = ret * 10 + (c - '0');
    }
    ret *= sgn;
    return true;
}
template <class T>
inline void out(T x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) {
        out(x / 10);
    }
    putchar(x % 10 + '0');
}

int P, N;
int Ans;
int Q[maxn];
int Depth[maxn];
int Basic[maxn][maxn];
int Adj[maxn][maxn];
vector<int> S[maxn], D[maxn];
vector<pair<int, pair<int, int> > > Res;

bool Bfs(int Start, int End) {
    std::queue<int> Que;
    memset(Depth, -1, sizeof(Depth));
    Depth[Start] = 0;
    Que.push(Start);
    while (!Que.empty()) {
        int Vertex = Que.front();
        Que.pop();
        for (int i = 0; i <= N + 1; ++i) {
            if (Depth[i] == -1 && Adj[Vertex][i]) {
                Depth[i] = Depth[Vertex] + 1;
                Que.push(i);
            }
        }
    }
    return Depth[End] > 0;
}

int Dfs(int Vertex, int End, int NowFlow) {
    if (Vertex == End) {
        return NowFlow;
    }
    int FindFlow = 0;
    for (int i = 0; i <= N + 1; ++i) {
        if (Adj[Vertex][i] && Depth[i] == Depth[Vertex] + 1) {
            FindFlow = Dfs(i, End, std::min(NowFlow, Adj[Vertex][i]));
            if (FindFlow) {
                Adj[Vertex][i] -= FindFlow;
                Adj[i][Vertex] += FindFlow;
                return FindFlow;
            }
        }
    }
    if (!FindFlow) {
        Depth[Vertex] = -2;
    }
    return false;
}

int Dinic(int Start, int End) {
    int MaxFlow = 0;
    while (Bfs(Start, End)) {
        MaxFlow += Dfs(Start, End, INF);
    }
    return MaxFlow;
}

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    read(P); read(N);
    mem(Adj, 0);
    mem(Basic, 0);
    for (int i = 1, flag; i <= N; ++i) {
        read(Q[i]);
        S[i].clear(); D[i].clear();
        flag = 1;
        for (int j = 1, X; j <= P; ++j) {
            read(X);
            S[i].pb(X);
            if (X == 1) {
                flag = 0;
            }
        }
        if (flag) {
            Adj[0][i] = Q[i];
            Basic[0][i] = Q[i];
        }
        flag = 1;
        for (int j = 1, X; j <= P; ++j) {
            read(X);
            D[i].pb(X);
            if (!X) {
                flag = 0;
            }
        }
        if (flag) {
            Adj[i][N + 1] = Q[i];
            Basic[i][N + 1] = Q[i];
        }
    }
    for (int i = 1; i <= N; ++i) {
        for (int j = 1; j <= N; ++j) {
            if (i != j) {
                bool flag = 1;
                for (int k = 0; k < P; ++k) {
                    if (D[i][k] == S[j][k] || S[j][k] == 2) {
                        continue;
                    }
                    else {
                        flag = 0;
                        break;
                    }
                }
                if (flag) {
                    Adj[i][j] = min(Q[i], Q[j]);
                    Basic[i][j] = min(Q[i], Q[j]);
                }
            }
        }
    }
    Ans = Dinic(0, N + 1);
    Res.clear();
    for (int i = 1; i <= N; ++i) {
        for (int j = 1; j <= N; ++j) {
            if (Basic[i][j] > Adj[i][j]) {
                Res.pb(mp(Basic[i][j] - Adj[i][j], mp(i, j)));
            }
        }
    }
    printf("%d %d\n", Ans, int(Res.size()));
    for (int i = 0; i < int(Res.size()); ++i) {
        printf("%d %d %d\n", Res[i].second.first, Res[i].second.second, Res[i].first);
    }
#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
    system("gedit out.txt");
#endif
    return 0;
}