poj3436 ACM Computer Factory(网络流最大流 + 拆点 + 建图 + 输出路径)

ACM Computer Factory

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions:12151   Accepted:4563   Special Judge

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 Qi Si,1 Si,2...Si,P Di,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 Bmust 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

Hint

Bold texts appearing in the sample sections are informative and do not form part of the actual data.

题意:

有 n 台机器,每个机器有 p 个属性,给出每台机器的效率、输入属性和输出属性。输入属性用0、1、2表示,0表示没有某个元件,1表示有某个元件,2表示可有可无。输出属性仅由0、1表示。问整个系统最大生产效率,即从初始产品(0,0,……,0)到成品(1,1,……,1)

https://www.cnblogs.com/GarrettWale/p/11436026.html

拆点:将每个机器的输入和输出当作两个点。入口为 i 的机器,出口为 i + n。

建图:

(1)建立一个超级源点 s = 0,和所有输入属性可以是初始产品的机器的入口连一条边,流量为inf

(2)建立一个超级汇点 t = 2 * n + 1。输出成品的机器的出口和 t 连一条边,流量为inf

(3)同一机器的入口和出口连一条边,流量为该机器的效率

(4)暴力找出机器之间的关系,若机器A的输出属性可以输入机器B中,把A的出口到B的入口连一条边

路径输出:

遍历所有边,如果起点不是 s ,终点不是 t ,且起点为某一机器的出口,终点为某一机器的入口,则打印这条边,流量即为edge[i].flow

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1005;
const int M = 2e6 + 10;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;

int head[N], tot, n, m, s, t;
int in[N][15], out[N][15];
struct node {
    int u, v, w;
};

struct Edge {
    int from, to, next, cap, flow;
}edge[M];

void init() {
    tot = 2;
    memset(head, -1, sizeof(head));
}

void addedge(int u, int v, int w, int rw = 0) {
    edge[tot].from = u;
    edge[tot].to = v;
    edge[tot].cap = w;
    edge[tot].flow = 0;
    edge[tot].next = head[u];
    head[u] = tot++;

    edge[tot].from = v;
    edge[tot].to = u;
    edge[tot].cap = rw;
    edge[tot].flow = 0;
    edge[tot].next = head[v];
    head[v] = tot++;
}

int Q[N];
int dep[N], cur[N], sta[N]; ///数组cur记录点u之前循环到了哪一条边
bool bfs(int s, int t, int n) {
    int fron = 0, tail = 0;
    memset(dep, -1, sizeof(dep[0]) * (n + 1));
    dep[s] = 0;
    Q[tail++] = s;
    while(fron < tail) {
        int u = Q[fron++];
        for(int i = head[u]; i != -1; i = edge[i].next) {
            int v = edge[i].to;
            if(edge[i].cap > edge[i].flow && dep[v] == -1) {
                dep[v] = dep[u] + 1;
                if(v == t) return true;
                Q[tail++] = v;
            }
        }
    }
    return false;
}

int dinic(int s, int t, int n) {
    int maxflow = 0;
    while(bfs(s, t, n)) {
        for(int i = 0; i <= n; ++i) cur[i] = head[i];
        int u = s, tail = 0;
        while(cur[s] != -1) {
            if(u == t) {
                int tp = inf;
                for(int i = tail - 1; i >= 0; --i)
                    tp = min(tp, edge[sta[i]].cap - edge[sta[i]].flow);
                maxflow += tp;
                for(int i = tail - 1; i >= 0; --i) {
                    edge[sta[i]].flow += tp;
                    edge[sta[i] ^ 1].flow -= tp;
                    if(edge[sta[i]].cap - edge[sta[i]].flow == 0)
                        tail = i;
                }
                u = edge[sta[tail] ^ 1].to;
            }
            else if(cur[u] != -1 && edge[cur[u]].cap > edge[cur[u]].flow && dep[u] + 1 == dep[edge[cur[u]].to]) {
                sta[tail++] = cur[u];
                u = edge[cur[u]].to;
            }
            else {
                while(u != s && cur[u] == -1)
                    u = edge[sta[--tail] ^ 1].to;
                cur[u] = edge[cur[u]].next;
            }
        }
    }
    return maxflow;
}

int main() {
    int p, n, q;
    while(~scanf("%d%d", &p, &n)) {
        init();
        s = 0;
        t = 2 * n + 1;
        for(int i = 1; i <= n; ++i) {
            scanf("%d", &q);
            addedge(i, i + n, q);
            bool flag = 1;
            for(int j = 1; j <= p; ++j) {
                scanf("%d", &in[i][j]);
                if(flag && in[i][j] == 1) flag = 0;
            }
            if(flag)
                addedge(s, i, inf);
            flag = 1;
            for(int j = 1; j <= p; ++j) {
                scanf("%d", &out[i][j]);
                if(flag && out[i][j] == 0) flag = 0;
            }
            if(flag)
                addedge(i + n, t, inf);
        }
        for(int i = 1; i <= n; ++i) {
            for(int j = i + 1; j <= n; ++j) {
                bool flag = 1;
                for(int k = 1; k <= p; ++k) {
                    if(in[i][k] != out[j][k] && in[i][k] != 2) {
                        flag = 0;
                        break;
                    }
                }
                if(flag)
                    addedge(j + n, i, inf);
                flag = 1;
                for(int k = 1; k <= p; ++k) {
                    if(out[i][k] != in[j][k] && in[j][k] != 2) {
                        flag = 0;
                        break;
                    }
                }
                if(flag)
                    addedge(i + n, j, inf);
            }
        }
        printf("%d ", dinic(s, t, t + 1));
        vector<node>vec;
//      两种输出方式。
//        for(int i = 1; i <= n; ++i) {
//            for(int j = head[i + n]; j != -1; j = edge[j].next) {
//                if(edge[j].to != i && edge[j].to != s && edge[j].to != t && edge[j].flow) {
//                     vec.push_back(node{i, edge[j].to, edge[j].flow});
//                }
//            }
//        }
        for(int i = 0; i < tot; i += 2){
            if(edge[i].from <= n || edge[i].to >= 1 + n || edge[i].to == t || edge[i].from == 0)
                continue;
            if(edge[i].flow){
                vec.push_back(node{edge[i].from - n, edge[i].to, edge[i].flow});
            }
        }
        int siz = vec.size();
        printf("%d\n", siz);
        for(int i = 0; i < siz; ++i)
            printf("%d %d %d\n", vec[i].u, vec[i].v, vec[i].w);
    }
    return 0;
}

 

全部评论

相关推荐

双非坐过牢:非佬,可以啊10.28笔试,11.06评估11.11,11.12两面,11.19oc➕offer
点赞 评论 收藏
分享
威猛的小饼干正在背八股:挂到根本不想整理
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务