POJ3436 ACM Computer Factory 【最大流】

來源:互聯網
上載者:User

標籤:poj3436

ACM Computer Factory
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5412   Accepted: 1863   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 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 13 415  0 0 0  0 1 010  0 0 0  0 1 130  0 1 2  1 1 13   0 2 1  1 1 1Sample input 23 55   0 0 0  0 1 0100 0 1 0  1 0 13   0 1 0  1 1 01   1 0 1  1 1 0300 1 1 2  1 1 1Sample input 32 2100  0 0  1 0200  0 1  1 1

Sample Output

Sample output 125 21 3 152 3 10Sample output 24 51 3 33 5 31 2 12 4 14 5 1Sample output 30 0

Hint

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

Source

Northeastern Europe 2005, Far-Eastern Subregion題意:一個電腦由n個組件組成,現在有m台機器,每台機器可以將一個組裝狀態的電腦組合成另一個狀態。如(0, 1, 2)表示第一個組件未完成,第二個組件完成,第三個組件可完成可不完成。然後給出m個機器單位時間內能完成的任務數以及具體的輸入和輸出狀態。求整個系統單位時間內的電腦成品產量以及具體的機器間的傳輸關聯。題解:這題可以轉換成最大流來做,一個機器的輸出狀態可以跟另一個機器的輸入狀態關聯,只要它們的狀態“equals”,然後再設定一個超級源點和匯點,再就可以用Dinic解題了。

#include <stdio.h>#include <string.h>#define maxn 55#define inf 0x3fffffffstruct Node {    int in[10], out[10]; // 拆點    int Q; // 容量} M[maxn];int G[maxn << 1][maxn << 1], que[maxn << 1], m, n, mp;int G0[maxn << 1][maxn << 1], deep[maxn << 1], vis[maxn << 1];bool equals(int a[], int b[]) {    for(int k = 0; k < n; ++k) {        if(a[k] != 2 && b[k] != 2 && a[k] != b[k])            return false;    }    return true;}bool countLayer() {    int i, id = 0, now, front = 0;    memset(deep, 0, sizeof(deep));    deep[0] = 1; que[id++] = 0;    while(front < id) {        now = que[front++];        for(i = 0; i <= mp; ++i)            if(G[now][i] && !deep[i]) {                deep[i] = deep[now] + 1;                if(i == mp) return true;                que[id++] = i;            }    }    return false;}int Dinic() {    int i, id = 0, maxFlow = 0, minCut, pos, u, v, now;    while(countLayer()) {        memset(vis, 0, sizeof(vis));        vis[0] = 1; que[id++] = 0;        while(id) {            now = que[id - 1];            if(now == mp) {                minCut = inf;                for(i = 1; i < id; ++i) {                    u = que[i - 1]; v = que[i];                    if(G[u][v] < minCut) {                        minCut = G[u][v]; pos = u;                    }                }                maxFlow += minCut;                for(i = 1; i < id; ++i) {                    u = que[i - 1]; v = que[i];                    G[u][v] -= minCut;                    G[v][u] += minCut;                }                while(id && que[id - 1] != pos)                    vis[que[--id]] = 0;                            } else {                for(i = 0; i <= mp; ++i) {                    if(G[now][i] && deep[now] + 1 == deep[i] && !vis[i]) {                        que[id++] = i; vis[i] = 1; break;                    }                }                if(i > mp) --id;            }        }     }    return maxFlow;}int main() {    //freopen("stdin.txt", "r", stdin);    int i, j, sum, count;    while(scanf("%d%d", &n, &m) == 2) {        memset(G, 0, sizeof(G));        for(i = 1; i <= m; ++i) {            scanf("%d", &M[i].Q);            for(j = 0; j < n; ++j) scanf("%d", &M[i].in[j]);            for(j = 0; j < n; ++j) scanf("%d", &M[i].out[j]);            G[i][i + m] = M[i].Q;        }        // 串連出口跟入口        for(i = 1; i <= m; ++i) {            for(j = i + 1; j <= m; ++j) {                if(equals(M[i].out, M[j].in))                    G[i + m][j] = inf;                if(equals(M[j].out, M[i].in))                    G[j + m][i] = inf;            }        }        // 設定超級源點和超級匯點        for(i = 1; i <= m; ++i) { // 源點            G[0][i] = inf;            for(j = 0; j < n; ++j)                if(M[i].in[j] == 1) {                    G[0][i] = 0; break;                }        }        mp = m << 1 | 1;        for(i = 1; i <= m; ++i) { // 匯點            G[i + m][mp] = inf;            for(j = 0; j < n; ++j)                if(M[i].out[j] != 1) {                    G[i + m][mp] = 0; break;                }        }        // 備份原圖        memcpy(G0, G, sizeof(G));        sum = Dinic();        count = 0;        // 判斷哪些路徑有流走過        for(i = m + 1; i < mp; ++i)            for(j = 1; j <= m; ++j)                if(G0[i][j] > G[i][j]) ++count;        printf("%d %d\n", sum, count);        // 輸出機器間的關係        if(count)            for(i = m + 1; i < mp; ++i)                for(j = 1; j <= m; ++j)                    if(G0[i][j] > G[i][j])                        printf("%d %d %d\n", i - m, j, G0[i][j] - G[i][j]);    }    return 0;}


POJ3436 ACM Computer Factory 【最大流】

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.