Some matching concepts:
Perfect match, complete match, and best match
Staggered rail
Expandable rail
Algorithm principle of Hungary: Starting from the current matching M, check each uncovered point, and then start from it to find the Extensible path, find the Extensible path, and expand along the augmented path, until such a stop is not found.
Joj 2730
Http://blog.csdn.net/jxy859/article/details/6747413:
Adjacent Arrays
# Include <cstdio> # include <cstring> const int n = 105; const int M = 105; bool map [N] [m]; // X yint match [m]; // which of the following matches X in Y? The initial value is N, indicating that int vis [N] is not matched. // indicates whether the element in X has been marked with int N during DFS, m; bool DFS (int u) {for (INT I = 0; I <n; ++ I) if (Map [u] [I] &! Vis [I]) {vis [I] = true; If (Match [I] =-1 | DFS (Match [I]) {match [I] = u; return true;} return false;} int maxmatch () {int res = 0; memset (match,-1, sizeof (MATCH )); for (INT I = 0; I <n; ++ I) {memset (VIS, false, sizeof (VIS); If (DFS (I) RES ++ ;} return res ++;} int K; int SC [105] [30]; bool valid (int x, int y) {for (INT I = 0; I <K; ++ I) if (SC [x] [I]> = SC [y] [I]) return false; return true;} void build_graph () {memset (map, false, sizeof (MAP); For (INT I = 0; I <n; ++ I) {for (Int J = 0; j <n; ++ J) {If (valid (I, j) map [I] [J] = true; // directed edge; }}} int main () {int CAS; scanf ("% d", & CAS); For (INT I = 1; I <= CAS; ++ I) {scanf ("% d", & N, & K); For (INT I = 0; I <n; ++ I) {for (Int J = 0; j <K; ++ J) scanf ("% d", * (SC + I) + J);} build_graph (); int ans = maxmatch (); printf ("case # % d: % d \ n ", I, n-ans);} return 0 ;}
Adjacent table:
Joj has small advantages in space ~~
# Include <cstdio> # include <cstring> const int n = 105; const int M = 105; // max (n, m) cannot be used as the upper limit; const int MaxE = 10000; int N, K; struct edge {int V, next;} edge [MaxE]; int head [N], CNT; void addedge (int u, int v) {edge [CNT]. V = V; edge [CNT]. next = head [u]; head [u] = CNT ++;} // bool vis [N]; int match [m]; bool DFS (int u) {for (int p = head [u]; ~ P; P = edge [p]. next) {int v = edge [p]. v; If (vis [v]) continue; vis [v] = true; If (Match [v] =-1 | DFS (Match [v]) {match [v] = u; return true;} return false;} int maxmatch () {int res = 0; memset (match,-1, sizeof (MATCH )); for (INT I = 0; I <n; ++ I) {memset (VIS, 0, sizeof (VIS); If (DFS (I) ++ res ;} return res;} int stock [m] [30]; bool valid (int A, int B) {for (INT I = 0; I <K; ++ I) if (stock [a] [I]> = stock [B] [I]) return false; return true;} void build_graph () {memset (Head,-1, sizeof (head); CNT = 0; For (INT I = 0; I <n; ++ I) {for (Int J = 0; j <N; ++ J) {If (valid (I, j) addedge (I, j) ;}} int main () {int CAS; scanf ("% d ", & CAS); For (INT I = 1; I <= CAS; ++ I) {scanf ("% d", & N, & K ); for (INT I = 0; I <n; ++ I) {for (Int J = 0; j <K; ++ J) {scanf ("% d ", * (stock + I) + J) ;}} build_graph (); printf ("case # % d: % d \ n", I, n-maxmatch ()); /// pay special attention to n} return 0 when finding the minimum path overwrite ;}
Hdoj 3991 Harry Potter and the present II
An undirected graph is given, and P requirements are given. Ask how many partners are required to complete all requirements.
Obviously, the minimum path overwrites.
The time of this question is very tight, and the construction of the network stream will time out, so now I have to learn about Hungary, 1200 + MS, thanks.
# Include <cstdio> # include <cstring> # define min (a, B) (a <B? A: B) using namespace STD; const int n = 1005; const int M = 1005; // max (n, m) cannot be used as the upper limit; const int MaxE = 30000000; const int maxn = 2100; const int INF = 0x3fffffff; const long INF = 20000000000000ll; int N, K; struct edge {int V, next ;} edge [MaxE]; int head [N], CNT; void addedge (int u, int v) {edge [CNT]. V = V; edge [CNT]. next = head [u]; head [u] = CNT ++;} // bool vis [N]; int match [m]; bool DFS (int u) {for (int p = head [u]; ~ P; P = edge [p]. next) {int v = edge [p]. v; If (vis [v]) continue; vis [v] = true; If (Match [v] =-1 | DFS (Match [v]) {match [v] = u; return true ;}} return false ;}int maxmatch (INT nodes) {int res = 0; memset (match,-1, sizeof (MATCH); For (INT I = 0; I <nodes; ++ I) {memset (VIS, 0, sizeof (VIS )); if (DFS (I) + + Res;} return res;} // long map [N] [N]; int Q, M; long long QT [1005]; int QP [1005]; void build_graph ()/ /O (N * n * k) {memset (Head,-1, sizeof (head); CNT = 0; For (int K = 0; k <N; ++ K) {for (INT I = 0; I <n; ++ I) {for (Int J = 0; j <n; ++ J) {If (Map [I] [k]! = Inf & map [k] [J]! = Inf & map [I] [J]> map [I] [k] + map [k] [J]) map [I] [J] = map [I] [k] + map [k] [J] ;}}/ * For (INT I = 0; I <N; ++ I) {for (Int J = 0; j <n; ++ J) printf ("% d-", map [I] [J]); printf ("\ n");} * // process the shortest Short Circuit for (INT I = 0; I <q; ++ I) {for (Int J = 0; j <q; ++ J) {if (I = J) continue; // If (QP [I] = QP [J] & QT [I] <= QT [J]) addedge (I, j ); if (Map [QP [J] [QP [I] <= QT [J]-QT [I]) addedge (I, j );}} /// graph} int main () {int U, V, W; int ca S; scanf ("% d", & CAS); For (INT I = 1; I <= CAS; ++ I) {scanf ("% d", & N, & M, & Q); For (INT I = 0; I <n; ++ I) for (Int J = 0; j <n; ++ J) {if (I! = J) map [I] [J] = inf; else map [I] [I] = 0;} For (INT I = 0; I <m; ++ I) {scanf ("% d", & U, & V, & W ); map [u] [v] = map [v] [u] = min (Map [u] [v], W); // What is the duplicate edge?} For (INT I = 0; I <q; ++ I) {scanf ("% d", qP + I, QT + I);} build_graph (); printf ("case % d: % d \ n", I, Q-maxmatch (q)-1);} return 0 ;}