ACM Computer Factory(dinic)

时间:2022-06-10 05:58:15
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 
 #include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
using namespace std;
const int M = , inf = 0x3f3f3f3f ;
struct edge
{
int u , v , timeu ;
int w ;
}e[M * M * ]; struct node
{
int input[] , output[] ;
int w ;
}o[M]; int p , n ;
int src , des ;
int dis[M] ;
int head[M * M * ] ;
int cnt , app ;
struct ABW
{
int a , b , w ;
}step[M * M * ]; bool bfs ()
{
queue <int> q ;
while (!q.empty ())
q.pop () ;
memset (dis , - , sizeof(dis)) ;
dis[src] = ;
q.push (src) ;
while (!q.empty ()) {
int u = q.front () ;
q.pop () ;
for (int i = head[u] ; i != - ; i = e[i].timeu) {
int v = e[i].v ;
if (dis[v] == - && e[i].w > ) {
dis[v] = dis[u] + ;
q.push (v) ;
}
}
}
if (dis[des] > )
return true ;
return false ;
} int dfs (int u , int low)
{
int a = ;
if (u == des)
return low ;
for (int i = head[u] ; i != - ; i = e[i].timeu) {
int v = e[i].v ;
if (e[i].w > && dis[v] == dis[u] + && (a = dfs (v , min (low , e[i].w)))) {
e[i].w -= a ;
if (e[i].u != src && e[i].v != des) {
step[app].a = e[i].u ; step[app].b = e[i].v ; step[app].w = a ;
app++ ;
}
e[i^].w += a ;
return a ;
}
}
dis[u] = - ;
return ;
} void dinic ()
{
int ans = , res = ;
app = ;
while (bfs ()) {
while () {
if (ans = dfs (src , inf))
res += ans ;
else
break ;
}
}
printf ("%d %d\n" , res , app) ;
for (int i = app - ; i >= ; i--)
printf ("%d %d %d\n" , step[i].a + , step[i].b + , step[i].w) ;
} void addedge (int u , int v)
{
e[cnt].u = u ; e[cnt].v = v ; e[cnt].w = o[u].w == - ? o[v].w : o[u].w ; e[cnt].timeu = head[u] ;
head[u] = cnt++ ;
e[cnt].u = v ; e[cnt].v = u ; e[cnt].w = ; e[cnt].timeu = head[v] ;
head[v] = cnt++ ;
} void binary (int s , int l , int r)
{
if (l == r) {
if (s != l && l != n) {
int i ;
for (i = ; i < p ; i++) {
if (o[l].input[i] != && o[s].output[i] != o[l].input[i])
break ;
}
if (i == p) {
addedge (s , l) ;
}
}
return ;
}
int mid = l + r >> ;
binary (s , l , mid) ;
binary (s , mid + , r) ;
} int main ()
{
// freopen ("a.txt" , "r" , stdin) ;
while (~ scanf ("%d%d" , &p , &n)) {
for (int i = ; i < n ; i++) {
scanf ("%d" , &o[i].w) ;
for (int j = ; j < p ; j++) {
scanf ("%d" , &o[i].input[j]) ;
}
for (int j = ; j < p ; j++) {
scanf ("%d" , &o[i].output[j]) ;
}
}
for (int i = ; i < p ; i++) {
o[n].input[i] = o[n].output[i] = ;//源点
o[n + ].input[i] = o[n + ].output[i] = ;//汇点
}
o[n].w = - , o[n + ].w = - ;
src = n , des = n + ; n += ;
cnt = ;
memset (head , - , sizeof(head)) ;
for (int i = ; i < n - ; i++) {
binary(i , , n) ;
}
/* for (int i = 0 ; i < cnt ; i++) {
if (i % 2 == 0)
printf ("%d-->%d === %d , time: %d\n" , e[i].u , e[i].v , e[i].w , e[i].timeu) ;
}
puts ("") ; */
dinic () ;
}
return ;
}