The problem and Hdu 5093 the same as the portal. That is, the maximum matching number of the binary graph = minimum point set coverage. The problem requires a minimal number of robotic detention centers with important points, and obstructions that block the robot's Guard range.
We might as well separate the rows and columns by the maximum number of guard ranges for the row and column, so that the maximum number of matches is obtained. Since the detention center has important points, so it can be effective to heavy, after the maximum match can also be guaranteed to guard all the points. Set the Dinic template on the map after the build is OK.
The question is a bit counter-clockwise, please taste it carefully.
See the code for details:
#include <bits/stdc++.h>using namespace Std;typedef long long ll;const int INF = 1000000000;const int MAXN = 20000 + 100;int T,cnt,n,m,u,pp,a,b,v,w,t,id1[100][100],id2[100][100];char s[maxn][maxn];struct Edge {int from, to, cap, flow;}; BOOL operator < (const edge& A, const edge& b) {return A.from < B.from | | (A.from = = B.from && a.to < b.to);} struct Dinic {int n, m, S, t; Vector<edge> old; Vector<edge> edges; Twice times the number of sides vector<int> G[MAXN]; adjacency table, G[i][j] indicates the ordinal of the J-side of the node I in the E-array of bool VIS[MAXN]; BFS using int D[MAXN]; Distance from starting point to I int CUR[MAXN]; Current arc pointer void init (int n) {for (int i = 0; i < n; i++) g[i].clear (); Edges.clear ();} void Addedge (int from, int to, int caps) {Edges.push_back (Edge) {from, to, Cap, 0}); Edges.push_back (Edge) {To, from, 0, 0}); m = Edges.size (); G[from].push_back (m-2); G[to].push_back (m-1);} BOOL BFS () {memset (Vis, 0, sizeof (VIS)); Queue<int> Q; Q. push (s); Vis[s] = 1; D[s] = 0; while (! Q.empty ()) {int x = Q.front (); Q.pop (); for (int i = 0; i < g[x].size (); i++) {edge& e = edges[g[x][i]]; if (!vis[e.to] && e.cap > E.flow) {vis[e.to] = 1; D[e.to] = d[x] + 1; Q.push (e.to); }}} return vis[t];} int DFS (int x, int a) {if (x = = T | | a = = 0) return A; int flow = 0, F; for (int& i = cur[x]; i < g[x].size (); i++) {edge& e = edges[g[x][i]]; if (d[x] + 1 = = D[e.to] && (f = DFS (e.to, Min (A, e.cap-e.flow))) > 0) {e.flow + = f; Edges[g[x][i]^1].flow-= f; Flow + + F; A-= f; if (a = = 0) break; }} return flow;} int Maxflow (int s, int t) {this->s = s; this->t = t; int flow = 0; while (BFS ()) {memset (cur, 0, sizeof (cur)); Flow + = DFS (s, INF); } return flow; }}g;char ss[maxn][maxn];map<int,int> P;int Main () {scanf ("%d", &t); while (t--) {scanf ("%d%d%d", &n,&m,&pp); for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) s[i][j] = '. '; G.init (N*m*2+n); for (int i=1;i<=pp;i++) {scanf ("%d%d", &a,&b); S[A][B] = ' * '; } scanf ("%d", &w); for (int i=1;i<=w;i++) {scanf ("%d%d", &a,&b); S[a][b] = ' # '; } int cnt = 1; for (int i=1;i<=n;i++) {for (int j=1;j<=m;j++) {if (s[i][j] = = ' # ') cnt++;//by line number if (s[i][j] = = ' * ') id1[i][j] = cnt; } cnt++; } for (int j=1;j<=m;j++) {for (int i=1;i<=n;i++) {if (s[i][j] = = ' # ') cnt++;//by column number if (s[i][j] = = ' * ') id2[i][j] = cnt; } cnt++; } p.clear (); for (int i=1;i<=n;i++) {for (int j=1;j<=m;j++) {if (s[i][j] = = ' * ') { G.addedge (id1[i][j],id2[i][j],1);//Edge if (!p.count (Id1[i][j])) {//each number can only be connected to the source point once P[ID1[I][J]] = 1; G.addedge (0,id1[i][j],1); } if (!p.count (Id2[i][j])) {//Ibid. p[id2[i][j]] = 1; G.addedge (id2[i][j],cnt+1,1); }}}} int ans = G.maxflow (0,cnt+1); printf ("%d\n", ans); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
12549-sentry Robots (binary image matching)