Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 5501 | Accepted: 1887 | 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 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 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
#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;#define INF 0x3f3f3f3f#define LL __int64struct node{ int u , v , w ; int next , k ;} p[1000000];struct node1{ int u , v , w ;} q[1000000] ;queue <int> que ;LL in[110] , out[110] , num[110] ;int head[110] , cnt ;int arr[110] , vis[110] ;void init(){ cnt = 0 ; memset(in,0,sizeof(in)) ; memset(out,0,sizeof(out)) ; memset(head,-1,sizeof(head)) ;}void add(int u,int v,int w){ p[cnt].u = u ; p[cnt].v = v ; p[cnt].w = w ; p[cnt].k = w ; p[cnt].next = head[u] ; head[u] = cnt++ ; p[cnt].u = v ; p[cnt].v = u ; p[cnt].w = 0 ; p[cnt].k = 0 ; p[cnt].next = head[v] ; head[v] = cnt++ ;}bool judge(LL x,LL y,int m){ int i , k1 , k2 ; for(i = 1 ; i <= m ; i++) { k1 = x % 10 ; k2 = y % 10 ; x /= 10 ; y /= 10 ; if(k1 == 2 || k2 == 2) continue ; if( k1 != k2 ) return false ; } return true ;}bool bfs(int s,int e){ int i , u , v ; while( !que.empty() ) que.pop(); memset(arr,-1,sizeof(arr)) ; vis[s] = 1 ; arr[s] = 0 ; que.push(s) ; while( !que.empty() ) { u = que.front() ; que.pop() ; for(i = head[u] ; i != -1 ; i = p[i].next ) { v = p[i].v ; if( arr[v] == -1 && p[i].w ) { arr[v] = arr[u] + 1 ; que.push(v) ; } } } if( arr[e] > 0 ) return true ; return false ;}int dfs(int u,int t,int min1){ if( u == t ) return min1 ; int i , v , a , ans = 0 ; for(i = head[u] ; i != -1 ; i = p[i].next) { v = p[i].v ; if( arr[v] == arr[u] + 1 && p[i].w && ( a = dfs(v,t,min(min1,p[i].w) ) ) ) { p[i].w -= a ; p[i^1].w += a ; ans += a ; min1 -= a ; if( !min1 ) break ; } } if( ans ) return ans ; arr[u] = -1 ; return 0 ;}int main(){ int n , m , i , j , u , v , w , k , max_flow , sum ; while( scanf("%d %d", &m, &n) != EOF ) { init() ; max_flow = sum = 0 ; for(i = 1 ; i <= n ; i++) { k = 0 ; scanf("%I64d", &num[i]) ; for(j = 0 ; j < m ; j++) { scanf("%d", &w) ; in[i] = in[i]*10 + w ; k = k*10 + 1 ; } for(j = 0 ; j < m ; j++) { scanf("%d", &w) ; out[i] = out[i]*10 + w ; } } for(i = 1 ; i <= n ; i++) { add(i,i+50,num[i]) ; if( judge(0,in[i],m) ) add(0,i,INF) ; if( judge(k,out[i],m) ) add(i+50,101,INF) ; for(j = 1 ; j <= n ; j++) { if( i == j ) continue ; if( judge(out[i],in[j],m) ) add(i+50,j,INF) ; } } while( bfs(0,101) ) { while( k = dfs(0,101,INF) ) max_flow += k ; } for(i = 51 ; i <= n+50 ; i++) { for(j = head[i] ; j != -1 ; j = p[j].next) { if( p[j].k == INF && p[j].u != 0 && p[j].v != 101 && p[j].w < p[j].k ) { q[sum].u = i-50 ; q[sum].v = p[j].v ; q[sum].w = p[j].k - p[j].w ; sum++ ; } } } if( max_flow == 0 ) printf("0 0\n") ; else { printf("%d %d\n", max_flow, sum) ; for(i = 0 ; i < sum ; i++) printf("%d %d %d\n", q[i].u, q[i].v, q[i].w) ; } } return 0;}