HDU 2686 Matrix (maximum fee + split point)
Same as POJ3422
Delete K and change the size of the sink point and source point to 2 (because there are two options ).
# Include
# Include
# Include
# Include
# Include
# Include const int maxn = 20000; const int maxm = 800000; const int inf = 1e8; const int INF = 0x3f3f3f; # define MIN INT_MIN # define MAX 1e6 # define LL long # define init (a) memset (a, 0, sizeof (a) # define FOR (I, a, B) for (int I = a; I
B )? (A) :( B) # define min (a, B) (a> B )? (B) :( a) using namespace std; struct node {int u, v, w, cap, next;} edge [maxm]; int pre [maxn], dis [maxn], head [maxn], cnt; bool vis [maxn]; int n; void add (int u, int v, int c, int cap) {edge [cnt]. u = u; edge [cnt]. v = v; edge [cnt]. w = c; edge [cnt]. cap = cap; edge [cnt]. next = head [u]; head [u] = cnt ++; edge [cnt]. u = v; edge [cnt]. v = u; edge [cnt]. w =-c; edge [cnt]. cap = 0; edge [cnt]. next = head [v]; head [v] = cnt ++;} int spfa (int s, int t) {queue
Q; while (q. empty () = false) q. pop (); q. push (s); memset (vis, 0, sizeof (vis); memset (pre,-1, sizeof (pre); FOR (I, s, t + 1) dis [I] =-1; // obtain the longest path dis array and initialize it to-1 dis [s] = 0; vis [s] = 1; while (! Q. empty () {int u = q. front (); q. pop (); vis [u] = 0; for (int I = head [u]; I! =-1; I = edge [I]. next) {if (edge [I]. cap & dis [edge [I]. v] <(dis [u] + edge [I]. w) // obtain the longest path {dis [edge [I]. v] = dis [u] + edge [I]. w; pre [edge [I]. v] = I; if (! Vis [edge [I]. v]) {vis [edge [I]. v] = 1; q. push (edge [I]. v) ;}}} if (dis [t]! =-1) // ------------------ forgot to change .. Return 1; else return 0;} int MinCostMaxFlow (int s, int t) {int flow = 0, cost = 0; while (spfa (s, t )) {int df = inf; for (int I = pre [t]; I! =-1; I = pre [edge [I]. u]) {if (edge [I]. cap