1 #include <iostream> 2 #include <cstdlib> 3 #include <cstring> 4 #include <cstdio> 5 #include <queue> 6 const int inf = 1<<30 , N = 100 + 11 , M = 1000 + 11 ; 7 using namespace std ; 8 int m , n ; 9 int head[N] , tot ; 10 struct id 11 { 12 int to , nxt , val ; 13 } edge[(N*N)<<1] ; 14 int dis[N*M] , cur[N*M] , num[M] , fir[M] , edg[N]; 15 bool vis[M] , use[N][N]; 16 queue< int > Q ; 17 18 inline void Init( ) 19 { 20 freopen( "NSOOJ10745.in" , "r" , stdin ) ; 21 freopen( "NSOOJ10745.out" , "w" , stdout ) ; 22 } 23 24 25 int read( ) 26 { 27 char ch = getchar( ) ; int k = 1 , ret = 0 ; 28 while( ch > '9' || ch < '0' ) { if( ch == '-' ) k = -1 ; ch = getchar( ) ; } 29 while( ch <= '9' && ch >= '0' ) ret = ret * 10 + ch - '0' , ch = getchar( ) ; 30 return ret * k ; 31 } 32 33 34 void add( int u , int v , int c ) 35 { 36 edge[++tot].to = v ; edge[tot].nxt = head[u] ; 37 edge[tot].val = c ; head[u] = tot ; 38 } 39 40 void input( ) 41 { 42 m = read( ) , n = read( ) ; tot = 1 ; 43 memset( head , -1 , sizeof(head) ) ; 44 for( int x = 1 ; x <= m ; ++x ) 45 num[x] = read( ) ; 46 int a , b ; 47 for( int x = 1 ; x <= n ; ++x ) 48 { 49 a = read( ) ; 50 for( int y = 1 ; y <= a ; ++y ) 51 { 52 b = read( ) ; 53 if( !vis[b] ) 54 { 55 if( !edg[x] ) 56 { 57 add( 0 , x , num[b] ) ; 58 edg[x] = tot ; 59 add( x , 0 , 0 ) ; 60 } 61 else edge[edg[x]].val += num[b] ; 62 fir[b] = x ; 63 vis[b] = true ; 64 } 65 else 66 { 67 if( use[fir[b]][x] ) continue ; 68 add( fir[b] , x , inf ) ; 69 add( x , fir[b] , 0 ) ; 70 use[fir[b]][x] = true ; 71 } 72 } 73 b = read( ) ; 74 add( x , n + 1 , b ) ; 75 add( n+1 , x , 0 ) ; 76 } 77 } 78 79 80 bool bfs( ) 81 { 82 memset( dis , -1 , sizeof(dis) ) ; 83 Q.push( 0 ) ; dis[0] = 0 ; 84 while( !Q.empty( ) ) 85 { 86 int u = Q.front( ) ; Q.pop( ) ; 87 for( int i = head[u] ; ~i ; i = edge[i].nxt ) 88 { 89 int v = edge[i].to ; 90 if( dis[v] < 0 && edge[i].val > 0 ) 91 { 92 dis[v] = dis[u] + 1 ; 93 Q.push( v ) ; 94 } 95 96 } 97 } 98 if( dis[n+1] > 0 ) return true ; 99 return false ; 100 } 101 102 103 int dfs( int u , int inf ) 104 { 105 if( u == n + 1 ) return inf ; 106 int ans = 0 , cost = 0 ; 107 for( int i = cur[u] ; ~i ; i = edge[i].nxt ) 108 { 109 int v = edge[i].to ; 110 if( dis[v] != dis[u] + 1 ) continue ; 111 ans = dfs( v , min( inf - cost , edge[i].val ) ) ; 112 edge[i].val -= ans ; edge[i^1].val += ans ; 113 if( edge[i].val ) cur[u] = i ; 114 cost += ans ; 115 if( cost == inf ) return inf ; 116 } 117 if( !cost ) dis[u] = -1 ; 118 return cost ; 119 } 120 121 122 123 void sov( ) 124 { 125 int ans = 0; 126 while( bfs( ) ) 127 { 128 for( int x = 0 ; x <= n + 1 ; ++x ) cur[x] = head[x] ; 129 ans += dfs( 0 , inf ) ; 130 } 131 printf( "%d " , ans ) ; 132 133 } 134 135 136 int main( ) 137 { 138 // Init( ) ; 139 input( ) ; 140 sov( ) ; 141 // fclose( stdin ) ; 142 // fclose( stdout ) ; 143 return 0 ; 144 145 }
Description
Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pighouse because he doesn't have the keys. Customers come to the farm one after another. Each of them has keys to some pig-houses and wants to buy a certain number of pigs. All data concerning customers planning to visit the farm on that particular day are available to Mirko early in the morning so that he can make a sales-plan in order to maximize the number of pigs sold. More precisely, the procedure is as following: the customer arrives, opens all pig-houses to which he has the key, Mirko sells a certain number of pigs from all the unlocked pig-houses to him, and, if Mirko wants, he can redistribute the remaining pigs across the unlocked pig-houses.
An unlimited number of pigs can be placed in every pig-house. Write a program that will find the maximum number of pigs that he can sell on that day.
An unlimited number of pigs can be placed in every pig-house. Write a program that will find the maximum number of pigs that he can sell on that day.
Input
The first line of input contains two integers M and N, 1 <= M <= 1000, 1 <= N <= 100, number of pighouses and number of customers. Pig houses are numbered from 1 to M and customers are numbered from 1 to N. The next line contains M integeres, for each pig-house initial number of pigs. The number of pigs in each pig-house is greater or equal to 0 and less or equal to 1000. The next N lines contains records about the customers in the following form ( record about the i-th customer is written in the (i+2)-th line): A K1 K2 ... KA B It means that this customer has key to the pig-houses marked with the numbers K1, K2, ..., KA (sorted nondecreasingly ) and that he wants to buy B pigs. Numbers A and B can be equal to 0.
Output
The first and only line of the output should contain the number of sold pigs.
Sample Input
3 3 3 1 10 2 1 2 2 2 1 3 3 1 2 6
Sample Output
7
一道网络流的题 ,