POJ 3189 Steady Cow Assignment
Question link
Some cows, each of which has a ranking in a cowshed, are given the capacity of each Cowshed, which requires allocation of these cattle to the cowshed, so that the ranking gap between all the cattle and the Cowshed is as small as possible.
Idea: The standard solution for this type of question is to divide the difference value into two parts, enumerate the lower bound to determine the upper bound, and then create a graph to judge. This question uses the maximum stream for judgment, it is worth mentioning whether the efficiency of dinic is reduced or the time is too late. In theory, it is better to use sap or bipartite graph for multiple matching.
Code:
# Include <cstdio> # include <cstring> # include <queue> # include <algorithm> using namespace std; const int MAXNODE = 1025; const int MAXEDGE = 200005; typedef int Type; const Type INF = 0x3f3f3f; struct Edge {int u, v; Type cap, flow; Edge () {} Edge (int u, int v, Type cap, Type flow) {this-> u = u; this-> v = v; this-> cap = cap; this-> flow = flow ;}}; struct Dinic {int n, m, s, t; Edge edges [MAXEDGE]; int first [MAXNOD E]; int next [MAXEDGE]; bool vis [MAXNODE]; Type d [MAXNODE]; int cur [MAXNODE]; vector <int> cut; void init (int n) {this-> n = n; memset (first,-1, sizeof (first); m = 0;} void add_Edge (int u, int v, Type cap) {edges [m] = Edge (u, v, cap, 0); next [m] = first [u]; first [u] = m ++; edges [m] = Edge (v, u, 0, 0); next [m] = first [v]; first [v] = m ++;} bool bfs () {memset (vis, false, sizeof (vis); queue <int> Q; Q. push (s); d [s] = 0; Vis [s] = true; while (! Q. empty () {int u = Q. front (); Q. pop (); for (int I = first [u]; I! =-1; I = next [I]) {Edge & e = edges [I]; if (! Vis [e. v] & e. cap> e. flow) {vis [e. v] = true; d [e. v] = d [u] + 1; Q. push (e. v) ;}}return vis [t];} Type dfs (int u, Type a) {if (u = t | a = 0) return; type flow = 0, f; for (int & I = cur [u]; I! =-1; I = next [I]) {Edge & e = edges [I]; if (d [u] + 1 = d [e. v] & (f = dfs (e. v, min (a, e. cap-e. flow)> 0) {e. flow + = f; edges [I ^ 1]. flow-= f; flow + = f; a-= f; if (a = 0) break;} return flow;} Type Maxflow (int s, int t) {this-> s = s; this-> t = t; Type flow = 0; while (bfs () {for (int I = 0; I <n; I ++) cur [I] = first [I]; flow + = dfs (s, INF);} return flow;} void MinCut () {cut. clear (); for (int I = 0; I < M; I + = 2) {if (vis [edges [I]. u] &! Vis [edges [I]. v]) cut. push_back (I) ;}} gao; const int N = 1005; const int M = 25; int n, B, g [N] [M], s [M]; bool build (int x, int y) {gao. init (n + B + 2); for (int I = 1; I <= n; I ++) {gao. add_Edge (0, I, 1); for (int j = x; j <= y; j ++) gao. add_Edge (I, g [I] [j] + n, 1) ;}for (int I = 1; I <= B; I ++) gao. add_Edge (I + n, n + B + 1, s [I]); return gao. maxflow (0, n + B + 1) = n;} int main () {while (~ Scanf ("% d", & n, & B) {for (int I = 1; I <= n; I ++) for (int j = 1; j <= B; j ++) scanf ("% d", & g [I] [j]); for (int I = 1; I <= B; I ++) scanf ("% d", & s [I]); int ans = 100; for (int I = 1; I <= B; I ++) {for (int j = B; j> = I; j --) {if (ans <= j-I + 1) continue; if (build (I, j )) ans = min (ans, j-I + 1) ;}} printf ("% d \ n", ans) ;}return 0 ;}
POJ 3189 Steady Cow Assignment (Max stream)