Hdu 4971 multi-school 10 max weight closure Diagram
/* Obviously, the biggest right closed graph question */# include
# Include
# Include
Using namespace std; # define N 2100 # define inf 0x3fffffffstruct node {int u, v, w, next;} bian [N * 20]; int head [N], yong, dis [N], work [N]; void init () {yong = 0; memset (head,-1, sizeof (head ));} void addbian (int u, int v, int w) {bian [yong]. u = u; bian [yong]. v = v; bian [yong]. w = w; bian [yong]. next = head [u]; head [u] = yong ++;} void add (int u, int v, int w) {addbian (u, v, w ); addbian (v, u, 0);} int min (int a, int B) {return
Q; q. push (s); dis [s] = 0; while (! Q. empty () {int u = q. front (); q. pop (); for (int I = head [u]; I! =-1; I = bian [I]. next) {int v = bian [I]. v; if (bian [I]. w & dis [v] =-1) {dis [v] = dis [u] + 1; q. push (v); if (v = t) return 1 ;}} return 0 ;}int dfs (int s, int limit, int t) {if (s = t) return limit; for (int & I = work [s]; I! =-1; I = bian [I]. next) {int v = bian [I]. v; if (bian [I]. w & dis [v] = dis [s] + 1) {int tt = dfs (v, min (limit, bian [I]. w), t); if (tt) {bian [I]. w-= tt; bian [I ^ 1]. w + = tt; return tt ;}}return 0 ;}int dinic (int s, int t) {int ans = 0; while (bfs (s, t )) {memcpy (work, head, sizeof (head); while (int tt = dfs (s, inf, t) ans + = tt;} return ans ;} int main () {int t, n, m, I, j, k, S, T, e, cost [N], total, cou = 0; scanf ("% d", & t); while (t --) {init (); scanf ("% d", & n, & m ); T = n + m + 1; S = 0; total = 0; for (I = 1; I <= n; I ++) {scanf ("% d ", & j); total + = j; add (S, I, j) ;}for (I = 1; I <= m; I ++) scanf ("% d", & cost [I]); for (I = 1; I <= n; I ++) {scanf ("% d ", & k); while (k --) {scanf ("% d", & j); j ++; add (I, n + j, inf );}} for (I = 1; I <= m; I ++) for (j = 1; j <= m; j ++) {scanf ("% d ", & e); if (e) add (I + n, j + n, inf) ;}for (I = n + 1; I <= n + m; I ++) add (I, T, cost [I-n]); printf ("Case # % d:", ++ cou ); printf ("% d \ n", total-dinic (S, T);} return 0 ;}