Problem Solving report POJ3463 ACM computer Factory

Source: Internet
Author: User

Problem Solving report POJ3463 ACM computer Factory


Description

As you know, all the computers used for ACM contests must is identical, so the participants compete on equal terms. That's why all these computers was historically produced at the same factory.

Every ACM computer consists of P parts. When all these parts was 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. Removes some parts from a half-finished computer and adds some new parts (removing of parts are sometimes nece Ssary as the parts cannot is added to a computer in arbitrary order). Each machine was 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 being able to operat E on it. The specification is a set of P numbers 0, 1 or 2 (one number to each part), where 0 means that corresponding PA RT must not being present, 1-the part was 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 was absent, 1-the part is present.

The machines connected by very fast production lines so, 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 G Rowing contest needs. That's 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 FA Ctory machines. It was noted that the easiest-to upgrade the factory are to rearrange production lines. ACM Directorate decided to entrust and solving this problem.

Input

Input file contains integers PN, then N descriptions of the machines. The description of ith machine are represented as by 2 P + 1 integers qisi, 1Si, 2 ... Si,Pdi, 1Di, 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 is made, then M descriptions of the connections.  Each connection between machines A and B must is described by three positive numbers ABW, where W is the number of computers delivered from A to B per hour.

If Several solutions exist, output any of them.

Sample Input

3 415  0 0 0  0 1 010  0 0 0 0 1 0 1 2 1 1 0 2 1  1 1 1
3-   0 0 0  0 1 0100 0 1 0 1 0-0 1 0 1 1 1   0 1  1 1 0300 1 1 2  1 1 1
2 2100  0 0  1 0200  0 1  1 1

Sample Output

25 21 3 15
2 3 104 51 3 33 5 31 2 12 4 14 5 1
0 0


now you want to assemble a computer, a computer composed of P components, now has n assembly machine. Each assembly machine receives a certain format of semi-finished products (input will give the assembly machine I input needs to meet the format, for the component j,1 indicates that the component to have, 0 means that the component must not have, 2 means that the component is optional ), output after the assembly of another format of semi-finished (or finished, that is, all 1). Each assembly machine has an assembly capacity cap. Q. How many computers can you assemble? And each assembly machine between each other is how many semi-finished products passed.


Analysis: By virtue of my excellent reading comprehension and strong network skills (why I do not believe it?!) )。 A look is obviously the biggest flow, and the ideas are very clear ah, split, super Source point, and residual network traffic determination. Actually in a detail was the pit for three hours.


Well, here's the point: how to construct the maximum flow? First, all assembly machines are removed and the load is the cap of the assembly machine to limit the flow. Then if the set of installed requirements for the input format is full 0 (that is, to accept the computer has not yet begun to assemble), then the super-source point and the assembly machine is connected to the load of INF, if the assembly machine output format of 1, then the group installed with the Super meeting point, the load is INF. Then traverse any two assembly machines I, J, if Out[i] meet IN[J]---> that is, the output format of the I-assembly machine satisfies the input format of the J-assembly machine, then even one edge, the load is INF. So run the maximum flow to see how much can be. For the residual network, you scan all the edges that are connected from the assembly machine in point (the point after the break), and filter out the edges that are neither connected des nor the out point of the split, output.


If you think it's okay to see this: then you've made the same mistake as me. So where's the mistake? The conditions for connecting the source point to the Assembly machine do not require that the assembly machine input format is full 0 and can also have 2! (Think about it carefully). So when judging, you should use the Match function (see Code) instead of using in[i]==string (p, ' 0 ') as a technique to die (p indicates the number of parts). BTW, there seems to be no way to break, but the amount of data is very small so disassembly is actually almost.


On the code:

#include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include < Cstring> #include <string>using namespace std;const int maxm = 251000;const int MAXN = 510;const int INF = 0x3f3f3f 3f;struct Edge{int from, to, cap, Next, oricap;}; Edge edge[maxm];int output[maxm];int level[maxn];int head[maxn];int cap[maxn];string in[MAXN];string out[MAXN];int src  , Des, Cnt;bool match (string out, string of, int p) {for (int i = 0; i < P; i++) if (Out[i] + in[i] = = ' 0 ' + ' 1 ') return False;return true;} void Addedge (int from, int to, int cap) {Edge[cnt].from = From;edge[cnt].to = To;edge[cnt].cap = Edge[cnt].oricap = Cap;e Dge[cnt].next = Head[from];head[from] = Cnt++;swap (from, to); Edge[cnt].from = from;edge[cnt].to = To;edge[cnt].cap = EDG E[cnt].oricap = 0;edge[cnt].next = Head[from];head[from] = cnt++;} int BFs () {memset (level,-1, sizeof level), Queue<int>q;while (!q.empty ()) Q.pop (); LEVEL[SRC] = 0;q.push (SRC); wh Ile (!q.empty ()) {int u = Q.front(); Q.pop (); for (int i = head[u]; I! = 1; i = edge[i].next) {int v = edge[i].to;if (Edge[i].cap > 0 && level[v ] = =-1) {Level[v] = Level[u] + 1;q.push (v);}}} return Level[des]! =-1;} int dfs (int u, int f) {if (U = = des) return f;int tem;for (int i = head[u]; I! =-1; i = edge[i].next) {int v = edge[i].to if (edge[i].cap>0 && level[v] = = Level[u] + 1) {tem = DFS (V, min (f, edge[i].cap)), if (Tem > 0) {edge[i].c AP-= tem;edge[i ^ 1].cap + = Tem;return tem;}} Level[u] = -1;return 0;} int dinic () {int ans = 0, Tem;while (BFS ()) {while (tem = DFS (src)) > 0) {ans + = tem;}} return ans;} int main () {int p, n;src = 0;des = 505;while (cin >> p >> N) {memset (head,-1, sizeof head); cnt = 0;for (int i = 1; I <= N; i++) {cin >> cap[i];in[i] = out[i] = ""; char ch;for (int j = 1; J <= P; J + +) {cin >> ch;in[i] + = ch;} for (int j = 1; j <= P; j + +) {cin >> ch;out[i] + = ch;}} for (int i = 1; I <= n; i++) {Addedge (I, i +, cap[i]); if(Match (In[i],string (p, ' 0 '), p)) Addedge (SRC, I, INF), if (Match (out[i],string (p, ' 1 '), p)) Addedge (i + +, DES, INF); T j = 1; J <= N; J + +) if (Match (Out[i], in[j], p)) Addedge (i + A, J, INF);} int ans = dinic (), int num = 0;for (int i = 1 +, I <= n + i++) {for (int j = head[i]; J! =-1; j = edge[j].next) { if (Edge[j].from = = edge[j].to + | | edge[j].to = = des) continue;if (Edge[j].oricap > Edge[j].cap) output[num++] = j; }}printf ("%d%d\n", ans, num); for (int i = 0; i < num; i++) {printf ("%d%d%d\n", edge[output[i]].from-50, Edge[out Put[i]].to, Edge[output[i]].oricap-edge[output[i]].cap);}} return 0;}

OK, I'm going to catch up with my matlab homework. I'll be in the details for a lifetime. It proves again that the composition is the hardest part of the biggest flow!!

Problem Solving report POJ3463 ACM computer Factory

Related Article

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.