1. 程式人生 > >POJ 3436 拆點網路流

POJ 3436 拆點網路流

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

Constraints

1 ≤ P ≤ 10, 1 ≤ ≤ 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

這裡採用了記錄fow 而不是直接用cap減的方法,方便後來列印路徑。code:

#include <stdio.h>
#include <cstring>
#include <stdlib.h>
#include <queue>
#include <math.h>
#include <vector>
#include <climits>
#define rep(i,j,k) for(int i=j;i<k;++i)
#define mst(a,b) memset((a),(b),sizeof(a))
using namespace std;
const int MAXN = 1e4+7;
const int INF = INT_MAX;
typedef long long LL;
const long long int LLINF = 0x3f3f3f3f3f3f3f;
int p, n, s, t,cnt,dep[MAXN], iter[MAXN],path[MAXN][5];
int w[55],in[55][15],out[55][15];
struct Edge {
    int to, cap, rev, flow;
    Edge(int To, int Cap, int Rev, int Flow):
            to(To), cap(Cap), rev(Rev), flow(Flow){}
};
vector <Edge> G[MAXN];
void addEdge(int s,int t, int cap) {//建邊
    G[s].push_back(Edge(t, cap, G[t].size(), 0));
    G[t].push_back(Edge(s, 0, G[s].size()-1, 0));
}

bool bfs() {
    mst(dep,-1);
    dep[s] = 0;
    queue <int> q;
    q.push(s);
    while(!q.empty()) {
        int now = q.front(); q.pop();
        for(int i=0; i<G[now].size(); i++) {
            Edge &e = G[now][i];
            if(e.cap > e.flow && dep[e.to] < 0) {
                dep[e.to] = dep[now] + 1;
                q.push(e.to);
                if(e.to == t)
                    return true;
            }
        }
    }
    return  dep[t] > 0;
}

int dfs(int now, int f) {
    if(now == t || f == 0) return f;
    for(int &i=iter[now]; i<G[now].size(); i++) {
        Edge &e = G[now][i];
        if(e.cap > e.flow && dep[e.to] > dep[now]) {
            int d = dfs(e.to, min(f, e.cap-e.flow));
            if(d > 0) {
                e.flow += d;
                G[e.to][e.rev].flow -= d;
                return d;
            }
        }
    }
    dep[now] = -1;
    return 0;
}

int max_flow() {//Dinic跑最大流
    int ans = 0;
    while(bfs()) {
        int f = dfs(s, INF);
        memset(iter, 0, sizeof(iter));
        if(f > 0)
            ans += f;
        else
            break;
    }
    return ans;
}

// 列印路徑
void print_path(int ans) {//把路找出來
    cnt = 0;
    for(int i=n+1;i<=2*n;i++) {
        for(int j=0;j < G[i].size();j++) {
            Edge &e = G[i][j];
            if(e.flow > 0 && e.to <= n) {
                path[cnt][0] = i - n;// 表示機器序號
                path[cnt][1] = e.to;
                path[cnt][2] = e.flow;
                cnt++;
            }
        }
    }
    printf("%d %d\n",ans, cnt);
    for(int i=0;i<cnt;i++)
        printf("%d %d %d\n",path[i][0], path[i][1], path[i][2]);
}

bool isStart(int x[]){
    rep(i,1,p+1){
        if(x[i] == 1)
            return false;
    }
    return true;
}

bool isMatch(int out[], int in[]){
    rep(i,1,p+1){
        if(out[i] != in[i] && in[i] != 2)
            return false;
    }
    return true;
}

bool isEnd(int x[]){
    rep(i,1,p+1){
        if(x[i] == 0)
            return false;
    }
    return true;
}

int main()
    {
        while(scanf("%d%d",&p,&n) != EOF){
            rep(i,0,2*n+2) G[i].clear(); //一定不要忘記清空
            mst(w,0); mst(in,0);mst(out,0);
            s = 0,t = 2*n+1;
            rep(i,1,n+1){
                scanf("%d",&w[i]);
                rep(j,1,p+1)
                    scanf("%d",&in[i][j]);
                rep(j,1,p+1)
                    scanf("%d",&out[i][j]);
                if(isStart(in[i]))
                    addEdge(s,i,INF);
                if(isEnd(out[i]))
                    addEdge(i+n,t,INF);
            }

            rep(i,1,n+1){
                addEdge(i,i+n,w[i]);
                rep(j,1,n+1){
                    if(i == j) continue;
                    if(isMatch(out[i],in[j])){
                        addEdge(i+n,j,INF);
                    }
                }
            }
            LL flow = max_flow();
            print_path(flow);
        }

        return 0;
    }