CSU 1319 CX's dreams maximum number of vertices obtained from the right closure Graph
Question link: Click the open link
Ideas:
Obviously, this is to ask the maximum right closure graph and the number of vertices that can obtain the most.
1. First of all, the payment for the positive value is taken directly, and the payment for the positive value is ignored for the dream. In this way, the payment is converted into a bare maximum weight closure diagram.
2. Calculate the number of vertices right at this time: Set the total vertices right * to a large number of C, and then add the vertices right value to 1. The flow/C is the traffic, the maximum number of vertices is the vertices Point Set-flow % C.
# Include
# Include
# Include
Using namespace std; // point mark [0, n] typedef long ll; const int N = 3010; const int M = 500010; const ll INF = 1e18; template
Struct Max_Flow {int n; int Q [N], sign; int head [N], level [N], cur [N], pre [N]; int nxt [M], pnt [M], E; T cap [M]; void Init (int n) {this-> n = n + 1; E = 0; std: fill (head, head + this-> n,-1);} // returns to rw = 0 void add (int from, int to, T c, T rw) {pnt [E] = to; cap [E] = c; nxt [E] = head [from]; head [from] = E ++; pnt [E] = from; cap [E] = rw; nxt [E] = head [to]; head [to] = E ++;} bool Bfs (int s, int t) {si Gn = t; std: fill (level, level + n,-1); int * front = Q, * tail = Q; * tail ++ = t; level [t] = 0; while (front <tail & level [s] =-1) {int u = * front ++; for (int e = head [u]; e! =-1; e = nxt [e]) {if (cap [e ^ 1]> 0 & level [pnt [e] <0) {level [pnt [e] = level [u] + 1; * tail ++ = pnt [e] ;}} return level [s]! =-1;} void Push (int t, T & flow) {T mi = INF; int p = pre [t]; for (int p = pre [t]; p! =-1; p = pre [pnt [p ^ 1]) {mi = std: min (mi, cap [p]);} for (int p = pre [t]; p! =-1; p = pre [pnt [p ^ 1]) {cap [p]-= mi; if (! Cap [p]) {sign = pnt [p ^ 1];} cap [p ^ 1] + = mi;} flow + = mi;} void Dfs (int u, int t, T & flow) {if (u = t) {Push (t, flow); return;} for (int & e = cur [u]; e! =-1; e = nxt [e]) {if (cap [e]> 0 & level [u]-1 = level [pnt [e]) {pre [pnt [e] = e; Dfs (pnt [e], t, flow); if (level [sign]> level [u]) {return ;} sign = t ;}}t Dinic (int s, int T) {pre [s] =-1; t flow = 0; while (Bfs (s, T )) {std: copy (head, head + n, cur); Dfs (s, t, flow);} return flow ;}}; Max_Flow
F; ll dream [N], work [N], ans; int from, to; int n, m; const ll C = 1e6; void input () {ans = 0; from = 0; to = n + m + 1; F. init (to); for (int I = 1; I <= n; I ++) {scanf ("% lld", & dream [I]); F. add (from, I, dream [I] * C + 1LL, 0); ans + = dream [I] ;}for (int I = 1; I <= m; I ++) {scanf ("% lld", & work [I]); if (work [I]> = 0) ans + = work [I]; elseF. add (n + I, to,-work [I] * C, 0) ;}for (int I = 1, siz, u; I <= n; I ++) {scanf ("% d", & siz); whi Le (siz --) {scanf ("% d", & u); if (work [u]> = 0) continue; F. add (I, n + u, INF, 0) ;}} int main () {while (~ Scanf ("% d", & n, & m) {input (); ll flow = F. dinic (from, to); // cout <"FLOW:" <