There are m (1 <= m <= 1000) pigsty, n (1 <= n <= 100) guests, and I pigsty has ci pig. the I-th person buys pig on the I-th day, and has ki keys to buy up to bi pig. on the day I, the pigsty corresponding to the key will open. At this time, the number of pig can be allocated to the pigsty (and the sum of the number of pig that will not exceed the number of opened pig .) ask how many pigs can be sold at most. question: (1) create a super source and a super sink. (2) The source point builds edges on all pig circles and the capacity is the initial number of pig circles. This ensures that the network traffic does not exceed the total number of pig. (3) When all the guests build edges to the sink, the capacity is the demand, which ensures that the traffic to the sink will not exceed the total demand. (3) The Pigsty builds edges for the person with the key, and the capacity is infinite. If the pigsty can flow past .www.2cto.com (4), the customer who bought the pigsty builds edges for the customer who later owns the same key, the capacity is infinite, so that the information exchanged in the pigsty can be passed down to achieve the optimal value. code: [cpp] # include <stdlib. h> # include <string. h> # Include <stdio. h> # include <ctype. h> # include <math. h> # include <stack> # include <queue> # include <map> # include <set> # include <vector> # include <string> # include <iostream> # include <algorithm> using namespace std; # define ll long # define ls rt <1 # define rs ls | 1 # define lson l, mid, ls # define rson mid + 1, r, rs # define middle (l + r)> 1 # define eps (1e-8) # define clr_all (x, c) memset (x, c, sizeo F (x) # define clr (x, c, n) memset (x, c, sizeof (x [0]) * (n + 1 )) # define MOD 1000000009 # define INF 0x3f3f3f # define PI acos (-1.0) # define _ max (x, y) (x)> (y ))? (X) :( y) # define _ min (x, y) (x) <(y ))? (X) :( y) # define _ abs (x) <0? (-(X) :( x) # define getmin (x, y) (x = (x) <0 | (y) <(x ))? (Y) :( x) # define getmax (x, y) (x = (y)> (x ))? (Y) :( x) template <class T> void _ swap (T & x, T & y) {T = x; x = y; y = t ;} int TS, cas = 1; const int M = 1100 + 5; int n, m; struct sap {typedef int type; struct edge {int to, next; type flow ;} edg [999999]; int n, m; int g [M], cur [M], pre [M]; int dis [M], gap [M]; int q [M], head, tail; void init (int _ n) {n = _ n, m = 0, clr_all (g,-1 );} void insert (int u, int v, type f, type c = 0) {edg [m]. to = v, edg [m]. flow = f, edg [m]. next = g [u], g [u] = m ++; Edg [m]. to = u, edg [m]. flow = 0, edg [m]. next = g [v], g [v] = m ++;} bool bfs (int s, int t) {clr (gap, 0, n), clr (dis, -1, n); gap [dis [t] = 0] ++, dis [s] =-1; for (q [head = tail = 0] = t; head <= tail;) {int u = q [head ++]; for (int I = g [u]; I! =-1; I = edg [I]. next) {edge & e = edg [I], ee = edg [I ^ 1]; if (dis [e. to] =-1 & ee. flow> 0) {gap [dis [e. to] = dis [u] + 1] ++; q [++ tail] = e. to; }}} return dis [s]! =-1;} type maxFlow (int s, int t) {if (! Bfs (s, t) return 0; type res = 0, a; int I; for (I = 0; I <n; I ++) cur [I] = g [I]; pre [s] = s, cur [s] = g [s], cur [t] = g [t]; for (int u = s; dis [s] <n;) {if (u = t) {for (a =-1; u! = S; u = pre [u]) getmin (a, edg [cur [pre [u]. flow); for (u = t; u! = S; u = pre [u]) {edg [cur [pre [u]. flow-= a; edg [cur [pre [u] ^ 1]. flow + = a;} res + = a;} bool OK = 0; for (I = cur [u]; I! =-1; I = edg [I]. next) {edge & e = edg [I]; if (dis [u] = dis [e. to] + 1 & e. flow> 0) {cur [u] = I, pre [e. to] = u, u = e. to; OK = 1; break;} if (OK) continue; int mindis = n-1; for (I = g [u]; I! =-1; I = edg [I]. next) {edge & e = edg [I]; if (mindis> dis [e. to] & e. flow> 0) mindis = dis [e. to], cur [u] = I;} if (-- gap [dis [u] = 0) break; gap [dis [u] = mindis + 1] ++, u = pre [u];} return res ;}} p; int tmp [M] [111], sz [M]; bool vis [111] [111]; void run () {int I, j; int k, a, B; p. init (n + m + 2); for (I = 1; I <= m; I ++) {scanf ("% d", & B); p. insert (0, n + I, B);} clr_all (sz, 0); for (I = 1; I <= n; I ++) {scanf ("% d", & k); while (k --) {scanf ("% d", &); P. insert (n + a, I, INF); tmp [a] [sz [a] ++] = I;} scanf ("% d", & B); p. insert (I, n + m + 1, B);} clr_all (vis, 0); for (I = 1; I <= m; I ++) {for (j = 1; j <sz [I]; j ++) {int u = tmp [I] [J-1], v = tmp [I] [j]; if (! Vis [u] [v]) vis [u] [v] = 1, p. insert (u, v, INF) ;}} printf ("% d \ n", p. maxFlow (0, n + m + 1);} void preSof () {} int main () {// freopen ("input.txt", "r", stdin ); // freopen ("output.txt", "w", stdout); preSof (); // run (); while (~ Scanf ("% d", & m, & n) run (); // for (scanf ("% d", & TS); cas <= TS; cas ++) run (); return 0 ;}