Portal
Test instructions: To a n*n matrix, some lattice have obstacles, ask us to eliminate these obstacles, asked each time to eliminate a row or a column of obstacles, at least a few times. Analytical:
Take each row and each column as a point on either side of the two-part graph.
If a lattice has a barrier, it corresponds to the row and column edge.
Select the fewest points so that all edges are overwritten.
Minimum point overlay.
--code
1#include <cstdio>2#include <cstring>3 #defineM (x, a) memset (a, X, sizeof (a))4 5 using namespacestd;6 7 Const intMAXN =501;8 intN, K, CNT, ans;9 intHEAD[MAXN], NEXT[MAXN * MAXN], TO[MAXN *MAXN], BELONG[MAXN];Ten BOOLVIS[MAXN]; One AInlinevoidAddintXinty) - { -TO[CNT] =y; theNEXT[CNT] =Head[x]; -HEAD[X] = cnt++; - } - +InlineBOOLFindintu) - { + intI, V; A for(i = head[u]; I! =-1; i =Next[i]) at { -v =To[i]; - if(!Vis[v]) - { -VIS[V] =1; - if(!belong[v] | |find (Belong[v])) in { -BELONG[V] =u; to return 1; + } - } the } * return 0; $ }Panax Notoginseng - intMain () the { + intI, x, y; Ascanf"%d%d", &n, &k); theM (-1, head); + for(i =1; I <= K; i++) - { $scanf"%d%d", &x, &y); $ Add (x, y); - } - for(i =1; I <= N; i++) the { -M0, Vis);Wuyi if(Find (i)) ans++; the } -printf"%d", ans); Wu return 0; -}
View Code
[POJ3041] Asteroids (minimum point overlay-Hungarian algorithm)