There are nheaded cattle and M cowshed, and each ox has a ranking of satisfaction with the cowshed. Each cowshed has a limit on the number of cattle.
Ask how to assign each ox to minimize the difference in the degree of satisfaction of all cows.
First, you can think of a binary answer to determine whether the difference is feasible.
I don't want to start network stream any more. I learned the solution of the Two-byte graph Hungary ..
When matching, if the range is met and there is excess capacity for each choice (Bull shed), it will match;
Otherwise, the cattle that have already matched the cowshed are extended.
# Include <iostream> # include <cstdlib> # include <cstring> # include <string> # include <cstdio> # include <cmath> # include <algorithm> # include <vector> # include <queue> # include <map> # define INF 0x3f3f3f3f # pragma comment (linker, "/Stack: 16777216") # define EPS 1e-6 # define ll _ int64const int maxn = 1005; using namespace STD; int link [25] [maxn], rk [maxn] [25], Cap [maxn], num [maxn]; bool vis [maxn]; int n, m, R, L; int path (int U) {int V; For (V = 0; v <m; V ++) {If (rk [u] [v]> = R & rk [u] [v] <= L &&! Vis [v]) {vis [v] = 1; if (Num [v] <CAP [v]) {link [v] [++ num [v] = u; // num [v] Head ox return 1;} For (INT I = 1; I <= CAP [v]; I ++) {If (path (link [v] [I]) {link [v] [I] = u; return 1 ;}}}return 0 ;}int hungry () {memset (Num, 0, sizeof num); memset (link,-1, sizeof link ); for (INT I = 0; I <n; I ++) {memset (VIS, 0, sizeof vis); If (! PATH (I) return 0;} return 1;} int main () {int mid, ANS, I, j; while (~ Scanf ("% d", & N, & M) {for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) {scanf ("% d", & ANS); rk [I] [-- ANS] = J; // What is the order of storage ans for this ox?} for (I = 0; I <m; I ++) scanf ("% d ", & Cap [I]); r = L = 0; ans = inf; while (r <= L & L <m) {mid = l-R + 1; if (hungry () {r ++; If (mid <ans) ans = mid;} else l ++;} printf ("% d \ n ", ans);} return 0 ;}
Poj3189 steady cow assignment --- multi-match and bipartite graph matching solution