ACM Computer Factory(dinic)

ACM Computer Factory

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5596   Accepted: 1922   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 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.

Source

Northeastern Europe 2005, Far-Eastern Subregion

跟poj1459差不多,就是net要自己连上线

dinic 0ms

  1 #include<stdio.h>
  2 #include<algorithm>
  3 #include<queue>
  4 #include<string.h>
  5 using namespace std;
  6 const int M = 60 , inf = 0x3f3f3f3f ;
  7 struct edge
  8 {
  9     int u , v , timeu ;
 10     int w ;
 11 }e[M * M * 2];
 12
 13 struct node
 14 {
 15     int input[20] , output[20] ;
 16     int w ;
 17 }o[M];
 18
 19 int p , n ;
 20 int src , des ;
 21 int dis[M] ;
 22 int head[M * M * 2] ;
 23 int cnt , app ;
 24 struct ABW
 25 {
 26     int a , b , w ;
 27 }step[M * M * 2];
 28
 29 bool bfs ()
 30 {
 31     queue <int> q ;
 32     while (!q.empty ())
 33         q.pop () ;
 34     memset (dis , -1 , sizeof(dis)) ;
 35     dis[src] = 0 ;
 36     q.push (src) ;
 37     while (!q.empty ()) {
 38         int u = q.front () ;
 39         q.pop () ;
 40         for (int i = head[u] ; i != -1 ; i = e[i].timeu) {
 41             int v = e[i].v ;
 42             if (dis[v] == -1 && e[i].w > 0) {
 43                 dis[v] = dis[u] + 1 ;
 44                 q.push (v) ;
 45             }
 46         }
 47     }
 48     if (dis[des] > 0)
 49         return true ;
 50     return false ;
 51 }
 52
 53 int dfs (int u , int low)
 54 {
 55     int a = 0 ;
 56     if (u == des)
 57         return low ;
 58     for (int i = head[u] ; i != -1 ; i = e[i].timeu) {
 59         int v = e[i].v ;
 60         if (e[i].w > 0 && dis[v] == dis[u] + 1 && (a = dfs (v , min (low , e[i].w)))) {
 61             e[i].w -= a ;
 62             if (e[i].u != src && e[i].v != des) {
 63                 step[app].a = e[i].u ; step[app].b = e[i].v ; step[app].w = a ;
 64                 app++ ;
 65             }
 66             e[i^1].w += a ;
 67             return a ;
 68         }
 69     }
 70     dis[u] = -1 ;
 71     return 0 ;
 72 }
 73
 74 void dinic ()
 75 {
 76     int ans = 0 , res = 0 ;
 77     app = 0 ;
 78     while (bfs ()) {
 79         while (1) {
 80             if (ans = dfs (src , inf))
 81                 res += ans ;
 82             else
 83                 break ;
 84         }
 85     }
 86     printf ("%d %d\n" , res , app) ;
 87     for (int i = app - 1 ; i >= 0 ; i--)
 88         printf ("%d %d %d\n" , step[i].a + 1 , step[i].b + 1 , step[i].w) ;
 89 }
 90
 91 void addedge (int u , int v)
 92 {
 93     e[cnt].u = u ; e[cnt].v = v ; e[cnt].w = o[u].w == -1 ? o[v].w : o[u].w ; e[cnt].timeu = head[u] ;
 94     head[u] = cnt++ ;
 95     e[cnt].u = v ; e[cnt].v = u ; e[cnt].w = 0 ; e[cnt].timeu = head[v] ;
 96     head[v] = cnt++ ;
 97 }
 98
 99 void binary (int s , int l , int r)
100 {
101     if (l == r) {
102         if (s != l && l != n) {
103             int i ;
104             for (i = 0 ; i < p ; i++) {
105                 if (o[l].input[i] != 2 && o[s].output[i] != o[l].input[i])
106                     break ;
107             }
108             if (i == p) {
109                 addedge (s , l) ;
110             }
111         }
112         return ;
113     }
114     int mid = l + r >> 1 ;
115     binary (s , l , mid) ;
116     binary (s , mid + 1 , r) ;
117 }
118
119
120
121 int main ()
122 {
123    // freopen ("a.txt" , "r" , stdin) ;
124     while (~ scanf ("%d%d" , &p , &n)) {
125         for (int i = 0 ; i < n ; i++) {
126             scanf ("%d" , &o[i].w) ;
127             for (int j = 0 ; j < p ; j++) {
128                 scanf ("%d" , &o[i].input[j]) ;
129             }
130             for (int j = 0 ; j < p ; j++) {
131                 scanf ("%d" , &o[i].output[j]) ;
132             }
133         }
134         for (int i = 0 ; i < p ; i++) {
135             o[n].input[i] = o[n].output[i] = 0 ;//源点
136             o[n + 1].input[i] = o[n + 1].output[i] = 1 ;//汇点
137         }
138         o[n].w = -1 , o[n + 1].w = -1 ;
139         src = n , des = n + 1 ;
140
141         n += 2 ;
142         cnt = 0 ;
143         memset (head , -1 , sizeof(head)) ;
144         for (int i = 0 ; i < n - 1; i++) {
145             binary(i , 0 , n) ;
146         }
147        /* for (int i = 0 ; i < cnt ; i++) {
148             if (i % 2 == 0)
149             printf ("%d-->%d === %d , time: %d\n" , e[i].u , e[i].v , e[i].w , e[i].timeu) ;
150         }
151         puts ("") ; */
152         dinic () ;
153     }
154     return 0 ;
155 }

时间: 2024-08-29 05:00:25

ACM Computer Factory(dinic)的相关文章

18.11.23 POJ 3436 ACM Computer Factory(dinic)

描述 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 the

POJ-3436 ACM Computer Factory (最大流[Ford-Fulkerson])

ACM Computer Factory http://poj.org/problem?id=3436 Time Limit: 1000MS   Memory Limit: 65536K         Special Judge Description As you know, all the computers used for ACM contests must be identical, so the participants compete on equal terms. That i

POJ3436:ACM Computer Factory(最大流)

ACM Computer Factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9963   Accepted: 3738   Special Judge 题目链接:http://poj.org/problem?id=3436 Description: As you know, all the computers used for ACM contests must be identical, so the pa

POJ 3436 ACM Computer Factory (最大流 + 输出路径)

POJ 3436 ACM Computer Factory 链接:http://poj.org/problem?id=3436 题意:每台电脑有P部分,可以通过不同的机器来进行加工.有N台机器,每台机器用2 P +1 个整数来描述:Qi  Si,1  Si,2 ...  Si,p  Di,1  Di,2. ..  Di,p,其中Qi 指定了机器的性能,表示每小时加工的电脑数量.Si,j 为第j 部分的输入规格,0表示该部分不能被加工过,1表示该部分必须被加工过,2表示都可以.Di,k 为第k 部

POJ-3436 ACM Computer Factory(网络流EK)

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

POJ - 3436 ACM Computer Factory (ISAP EK Dinic)

题目大意:有N台机器,每台机器能处理相应型态的电脑,处理完后,电脑将变成另一种形态. 每台机器有相应的工作限度,每次至多处理K台 现在问,在一次流水线生产中,最多可以产生多少台完整的电脑(流水线指的是在每一台机器的工作限度下) 解题思路:题目比较难理解,理解题目的话,就比较好做了 首先,将每台机器的点拆成两个点,权值为工作限度 如果机器能处理的电脑的状态全是0的话,就将其和超级源点连接,表示该机器进行第一步加工 如果机器处理完后的形态与另一台机器能处理的最初形态相同,就将其连线,表示下一台机器可

POJ 3436 ACM Computer Factory(最大流+路径输出)

http://poj.org/problem?id=3436 题意: 每台计算机包含P个部件,当所有这些部件都准备齐全后,计算机就组装完成了.计算机的生产过程通过N台不同的机器来完成,每台机器用它的性能(每小时组装多少台电脑).输入/输出规格来描述. 输入规格描述了机器在组装计算机时哪些部件必须准备好了.输入规格是由P个整数组成,每个整数代表一个部件,这些整数取值为0.1或2,其中0表示该部件不应该已经准备好了,1表示该部件必须已经准备好了,2表示该部件是否已经准备好了无关紧要. 输出规格描述了

解题报告 之 POJ3463 ACM Computer Factory

解题报告 之 POJ3463 ACM Computer Factory 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 A

POJ 3436 ACM Computer Factory(网络最大流)

http://poj.org/problem?id=3436 ACM Computer Factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5286   Accepted: 1813   Special Judge Description As you know, all the computers used for ACM contests must be identical, so the particip