Computer factory POJ3436-ACM

Source: Internet
Author: User

 

Reprinted please indicate the source: Thank you

Http://user.qzone.qq.com/289065406/blog/1299340266

Tip: Maximum Flow Problems

I have been suffering from questions for three days... Previous online users recommended splitting points, but I didn't use splitting points (I feel it is very troublesome)

I used three methods to solve this problem, but the results were unsatisfactory ....

[BFS + label method + No split point] Successful AC

[BFS + press the duplicate mark method + do not split points] (WA, do not know where the error is, cannot find the counterexample)

[BFS + press-in method + simulate split point] (WA, I don't know where the error is, but I cannot find the counterexample)

I paste the AC program below, and the last two wa codes below the AC code. I hope someone can help me find out what went wrong... Infinite gratitude

 

Question:

Honestly, I don't understand what the question is saying =... Orz

However, the following is a brief summary:

There are n machines, and each machine has P parts. Each part has its own input and output specifications. Therefore, one machine has P Input specifications and P output specifications. Each machine has 2 * p + 1 parameters to describe: the first parameter q: capacity of the machine; the next parameter S: Input specifications of each part of the machine; next, p parameter D: The output specifications of each part of the machine.

There are three Input specifications: 0, 1, 2

0: This part cannot exist

1: This part must be retained

2: This part is optional

There are two output types: 0, 1

0: This part does not exist.

1: This part exists

 

As for what I want to do with this question, I do not understand it at all =

However, by analyzing the input and output of the sample, coupled with some unclear insights that I have previously summarized, I can thoroughly analyze the Model O (attention _ attention) O Haha ~

 

Note: Only one input, one output, and sample I/O are required.

Sample input:

P n (n machines, each machine has P parts)

Then input n rows. In fact, each row is a node.

The format of each row is q p s p d

Q indicates the capacity of the current node, s indicates the input specification of the current node, and D indicates the output specification.

Sample output:

The two numbers in the first line indicate the maximum value of the stream, and the number of edges with traffic changes M (the edges associated with S and T are not in the column, and those do not belong to the original edges, is an appended edge)

Next there are m rows, each row has three numbers, a B W

A B is the endpoint of the edge with traffic changes, W is the traffic change value (the initial traffic of each edge is 0, and the final traffic is the traffic when the maximum flow is found)

 

If the graph is not connected, 0 0 is output.

 

Solution:

First, the structure diagram:

Add two super sources, super sink t

If the input part of a node (I) does not include 1, add a s-> I path with the capacity of qi;

If all the output of a node (j) is 1, add a J-> T path with the capacity of Qj;

If the output of node I does not conflict with the input of node J (the sum of output and input cannot be 1), add an I-> J path with the capacity of min (QI, qj ).

PS: The sum of the output and input locations cannot be 1, that is, the combination can be, 21, or 20, but not 01.

Solution:

Is the biggest stream Problem

 

/* BFS + do not split point-> Successful AC * // memory time // 292 K 0 Ms # include <iostream> using namespace STD; const int INF = 10001; int s; // super source int t; // super sink int N; // sum up the number of points (including super source and super sink) int P; // number of parts of each machine int CAP [52] [52]; // edge capacity int min (int A, int B) {return a <B? A: B;}/* use BFs to find the maximum network stream using the augmented chain */INT maxflow (void) {int queue [52]; int head, tail; int pre [52]; // The precursor int minflow of node I; int flow = 0; int X, Y; while (true) {memset (PRE,-1, sizeof (pre); For (queue [head = tail = 0] = s; head <= tail; head ++) {x = queue [head]; for (INT I = 0; (I <n) & (pre [T] =-1); I ++) // If (Cap [x] [I]> 0 & Pre [I] =-1) when the vertex is not marked) // when node u points to the edge of I and I has not marked the precursor, {pre [I] = X; // The frontend of record node I is u queue [++ tail] = I ;}} if (pre [T] =-1) Br Eak; // After BFs, if the vertex is not marked, it jumps out of the while, And the augmented chain minflow = inf does not exist; // initialize for (x = pre [Y = T]; y! = S;) // trace back {If (Cap [x] [Y] <minflow) minflow = CAP [x] [Y]; // find the smallest edge in the current augmented chain and record its edge weight (capacity) y = x; X = pre [y];} for (x = pre [Y = T]; y! = S;) // current augmented link traffic adjustment {CAP [x] [Y]-= minflow; // Forward Arc capacity reduction CAP [y] [x] + = minflow; // y = x; X = pre [y];} flow + = minflow; // maximum stream = sum of the adjusted values of the augmented chain obtained each time} return flow; // returns the maximum stream} int main (int I, Int J, int K) {int in [52] [21]; int out [52] [3]; int backup [52] [52]; // backup map int N; // int line for summary points except super source and super sink; // number of production lines (number of sides with changed capacity) int flow; // maximum stream while (CIN> P> N) {/* Initial */memset (Cap, 0, sizeof (CAP )); // The capacity of all forward and reverse arcs is initialized to 0 s = 0; // super source t = n + 1 ;/ /Supercollect n = n + 2; // sum points + 2 line = 0; // record the number of changed edges (number of production lines) /* input */for (I = 1; I <= N; I ++) for (j = 0; j <2 * p + 1; j ++) cin> in [I] [J]; // use a series to store information of node I in [I] [0] As the capacity bool flag_s and flag_t of node I; for (I = 1; I <= N; I ++) {flag_s = flag_t = true; For (k = 0; k <p; k ++) {If (in [I] [1 + k] = 1) flag_s = false; // check the input sequence of node I, if (in [I] [p + 1 + k] = 0) flag_t = false when the input column does not contain 1; // check the output sequence information of node I, when the output column is all 1} If (flag_s) CAP [s] [I] = in [I] [0]; // when the input column does not contain 1, s-> I, the edge capacity is the capacity of I if (Flag_t) CAP [I] [T] = in [I] [0]; // when the output column is all 1, I-> T, bool flag = true; For (j = 1; j <= N; j ++) if (I! = J) {flag = true; For (k = 0; (k <p) & flag; k ++) if (in [I] [p + 1 + k] + in [J] [1 + k] = 1) // when the K output bits of the I node are not 0, flag = false; If (FLAG) cap [I] [J] = min (in [I] [0], in [J] [0]); // I-> J, the edge capacity is the capacity of I and the minimum capacity of J}/* use BFs to find the augmented chain to find the maximum network flow */memcpy (backup, Cap, sizeof (CAP )); // copy the capacity information of the graph before the augmented chain to flow = maxflow (); // return the maximum stream/* output */for (I = 1; I <= N; I ++) // note range, excluding the edge with super source and super sink (j = 1; j <= N; j ++) if (Cap [I] [J] <backup [I] [J]) // compare the edge weights before and after adjustment. If the capacity is reduced, then, the output edge information {out [Line] [0] = I; // I. J indicates the out [Line] [1] = J at both ends of the production line; out [Line] [2] = backup [I] [J]-cap [I] [J]; // changed traffic value (maximum production volume of this production line) line ++;} cout <flow <<''' <line <Endl; for (I = 0; I <line; I ++) cout <out [I] [0] <''<out [I] [1] <'' <out [I] [2] <Endl;} return 0 ;}

 

 

 

============ Gorgeous split line ==================

/* [BFS + press the duplicate mark + do not split points]-> wa */# include <iostream> using namespace STD; const int INF = 10001; int S = 0; // super source int t; // super sink int N; // number of machines int P; // number of each machine part int CAP [52] [52]; // The capacity of the arc (I, j) int flow [52] [52]; // arc (I, j) bool mark [52] [52] = {false}; int sum = 0; bool vist [52]; // indicates whether vertex I has been labeled class info // tag information of the current vertex J {public: int pre; // iint LV, the precursor of the current vertex J; // L (V) int Q; // The production (capacity) of the machine (node I) int in [10]; // input specification int out [10]; // output specification int nei [51]; // current node Direct to the neighbor node int PN; // pointer to the neighbor node} node [52]; int min (int A, int B) {return a <B? A: B;} void back (void) {int x = T; while (X! = S) {If (X! = T & node [X]. Pre! = S) {If (! Mark [node [X]. pre] [x]) sum ++; // records the number of arcs with traffic changes (except for arcs containing S and T) MARK [node [X]. pre] [x] = true; // indicates whether the traffic of an arc (I, j) has changed (except for an arc containing S and T)} flow [node [X]. pre] [x] + = node [T]. LV; // improved augmented path X = node [X]. pre;} return;} bool BFS (void) {memset (vist, false, sizeof (vist); vist [s] = true; int queue [52]; int head = 0; int tail = 0; queue [tail ++] = s; while (Head <= tail-1) // note, this is no longer the end condition of the augmented path {int x = queue [head]; int y; For (INT I = 0; I <node [X]. PN; I ++) {Y = node [X]. nei [I]; If (! Vist [y] & flow [x] [Y] <CAP [x] [Y]) // The search target must be unmarked & unsaturated arc {queue [tail ++] = y; vist [y] = true; node [Y]. pre = x; node [Y]. lv = min (node [X]. LV, Cap [x] [Y]-flow [x] [Y]);} If (vist [T]) // when the supersink is marked with break;} If (! Vist [T]) Head ++; elsereturn true; // search for an augmented path} return false;} int main (int I, Int J, int K) {/* input */CIN> P> N;/* Initial */node [s]. pre =-1; node [s]. lv = inf; t = n + 1; for (I = 0; I <t; I ++) node [I]. pn = 0;/* input & structure graphs */bool sign; for (I = 1; I <= N; I ++) {Sign = false; cin> node [I]. q; For (j = 0; j <p; j ++) {CIN> node [I]. in [J]; If (node [I]. in [J] = 1) // If the input part of a node (I) does not include 1 Sign = true;} If (! Sign) // Add a s-> I path with the capacity of Qi {node [s]. nei [node [s]. pn ++] = I; CAP [s] [I] = node [I]. q; flow [s] [I] = 0;} Sign = false; For (j = 0; j <p; j ++) {CIN> node [I]. out [J]; If (node [I]. out [J] = 0) // If the output of a node (j) is all 1 Sign = true;} If (! Sign) // Add a J-> T path with the capacity of Qj {node [I]. nei [node [I]. pn ++] = T; CAP [I] [T] = node [I]. q; flow [I] [T] = 0 ;}} for (I = 1; I <= N; I ++) for (j = 1; j <= N; j ++) {Sign = false; if (I! = J) {for (k = 0; k <p; k ++) if (node [I]. out [k] + node [J]. in [k]) = 1) // If the output of node I does not conflict with the input of node J {// The sum of the positions of the output and input is not 1 Sign = true; break;} If (! Sign) // Add an I-> J path. The capacity is Min (QI, Qj ). {node [I]. nei [node [I]. pn ++] = J; CAP [I] [J] = min (node [I]. q, node [J]. q); flow [I] [J] = 0 ;}}/* Find the augmented rail by pressing the duplicate Mark Method */while (true) {If (BFS ()) // if you can search for augmented path back (); // you can trace back from the Super channel to improve the else {int max = 0; for (I = 0; I <node [s]. PN; I ++) MAX + = flow [s] [node [s]. nei [I]; cout <max <''<sum <Endl; for (I = 1; I <= N; I ++) for (j = 1; j <= N; j ++) if (I! = J & Mark [I] [J]) cout <I <''<j <'' <flow [I] [J] <Endl; break;} return 0 ;}

 

 

============ Gorgeous split line ==================

/* [BFS + press duplicate mark + simulate split point]-> wa */# include <iostream> using namespace STD; const int INF = 10001; int S = 0; // super source int t; // super sink int N; // number of machines int P; // number of each machine part int CAP [52] [52]; // The capacity of the arc (I, j) int flow [52] [52]; // arc (I, j) bool mark [52] [52] = {false}; int sum = 0; bool vist [52]; // indicates whether vertex I has been labeled class info // tag information of the current vertex J {public: int pre; // iint LV, the precursor of the current vertex J; // L (V) int Q; // total machine (node J) production (capacity) int F; // current machine (node J) production (Traffic) int in [10]; // input specification int out [10 ]; // Output specification int nei [51]; // The Int Pn of the neighbor node directly pointed to by the current node; // pointer of the neighbor node} node [52]; int min (int A, int B) {return a <B? A: B;} void back (void) {int x = T; while (X! = S) {If (X! = T & node [X]. Pre! = S) {If (! Mark [node [X]. pre] [x]) sum ++; // records the number of arcs with traffic changes (except for arcs containing S and T) MARK [node [X]. pre] [x] = true; // indicates whether the traffic of an arc (I, j) has changed (except for an arc containing S and T)} flow [node [X]. pre] [x] + = node [T]. LV; // improved augmented path node [X]. F + = node [T]. LV; // improves the vertex x = node [x] On the augmented path. pre;} return;} bool BFS (void) {memset (vist, false, sizeof (vist); vist [s] = true; int queue [52]; int head = 0; int tail = 0; queue [tail ++] = s; while (Head <= tail-1) // note, this is no longer the end condition of the augmented path {int x = queue [head]; int y; For (int I = 0; I <node [X]. pn; I ++) {Y = node [X]. nei [I]; If (! Vist [y] & flow [x] [Y] <CAP [x] [Y] & node [Y]. F <node [Y]. q) // The search target must be unmarked & unsaturated arc & unsaturated point (simulated split point) {// when a vertex is full, this vertex cannot reproduce more machines. queue [tail ++] = y; vist [y] = true; node [Y]. pre = x; node [Y]. lv = min (node [X]. LV, Cap [x] [Y]-flow [x] [Y]);} If (vist [T]) // when the supersink is marked with break;} If (! Vist [T]) Head ++; elsereturn true; // search for an augmented path} return false;} int main (int I, Int J, int K) {freopen ("in.txt", "r", stdin);/* input */CIN> P> N;/* Initial */t = n + 1; node [s]. pre =-1; node [s]. lv = inf; node [T]. Q = inf; for (I = 0; I <= T; I ++) {node [I]. pn = 0; node [I]. f = 0;}/* input & structure graphs */bool sign; for (I = 1; I <= N; I ++) {Sign = false; cin> node [I]. q; For (j = 0; j <p; j ++) {CIN> node [I]. in [J]; If (node [I]. in [J] = 1) // If the input part of a node (I) does not contain 1 Sign = True;} If (! Sign) // Add a s-> I path with the capacity of Qi {node [s]. nei [node [s]. pn ++] = I; CAP [s] [I] = node [I]. q; flow [s] [I] = 0;} Sign = false; For (j = 0; j <p; j ++) {CIN> node [I]. out [J]; If (node [I]. out [J] = 0) // If the output of a node (j) is all 1 Sign = true;} If (! Sign) // Add a J-> T path with the capacity of Qj {node [I]. nei [node [I]. pn ++] = T; CAP [I] [T] = node [I]. q; flow [I] [T] = 0 ;}} for (I = 1; I <= N; I ++) for (j = 1; j <= N; j ++) {Sign = false; if (I! = J) {for (k = 0; k <p; k ++) if (node [I]. out [k] + node [J]. in [k]) = 1) // If the output of node I does not conflict with the input of node J {// The sum of the positions of the output and input is not 1 Sign = true; break;} If (! Sign) // Add an I-> J path. The capacity is Min (QI, Qj ). {node [I]. nei [node [I]. pn ++] = J; CAP [I] [J] = min (node [I]. q, node [J]. q); flow [I] [J] = 0 ;}}/* Find the augmented rail by pressing the duplicate Mark Method */while (true) {If (BFS ()) // if you can search for augmented path back (); // you can trace back from the Super channel to improve the else {int max = 0; for (I = 0; I <node [s]. PN; I ++) MAX + = flow [s] [node [s]. nei [I]; cout <max <''<sum <Endl; for (I = 1; I <= N; I ++) for (j = 1; j <= N; j ++) if (I! = J & Mark [I] [J]) cout <I <''<j <'' <flow [I] [J] <Endl; break;} return 0 ;}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.