The problem is a classic binary graph matching topic. Now finally a little understanding of what is a binary map matching, in fact, it is dependent on the maximum flow algorithm on the solution of a specific problem of the algorithm. The so-called dichotomy is that we assume that there are two sets a and B, each set has several elements (points), where the source point is connected to a, the meeting point is connected to B, and their total capacity determines the upper limit of the final answer, so be sure to maintain it well. Then a point in a to the point in the B line, they also have a certain capacity constraints (specific to the topic of the Benquan value limit). This allows you to find the maximum flow matching.
Sometimes we ask for a perfect match, that is, all incoming quantities are equal to the amount of outflow.
The idea is very ingenious, because we know the sum of each row and each column, then we abstract this practical problem is: each row as a node connected to the source point, each column as a junction point connected to the node, then it is like a water flow, we imagine a pipe from the cross, there is water outflow from each column, Flow into each column. Since the problem itself is guaranteed to be solvable, it is a perfect match, and the water flowing out of the line will eventually be fully remitted to the into row.
Then the intersection traffic of each row and column is the value of that point, because the range of the required values is 1~20, so we let the capacity of the row and column nodes be up to 19, and so on! Why is it 19? Because in the maximum flow algorithm, we use the augmented path algorithm to increase the flow from 0, which means that some sides of the traffic is 0, so we might as well as the value of all intersections minus 1, then the capacity range is 0~19, then the corresponding source point to each point of capacity and the capacity of the meeting point to each point will change accordingly.
In addition to the solution of the printing, I initially want to update the augmented path algorithm, the last discovery is not possible, because there is still a reverse flow.
Details to participate in the code:
#include <bits/stdc++.h>using namespace Std;const int INF = 1000000000;const int MAXN = 305;int T,n,r,kase=0,c,a[max n],b[maxn],t,aa[maxn],bb[maxn],m,a[maxn],p[maxn],ans[maxn][maxn];struct edge{int from,to,cap,flow; Edge (int u,int v,int c,int f): From (U), to (v), Cap (c), Flow (f) {}};vector<edge> edges;vector<int> G[MAXN]; void Init () {for (int i=0;i<maxn;i++) g[i].clear (); Edges.clear (); memset (ans,0,sizeof (ANS));} void Addedge (int from,int to,int cap) {//increases the edge and saves the corresponding edge of each node in G Edges.push_back (Edge (from,to,cap,0)); Edges.push_back (Edge (to,from,0,0)); t = edges.size (); G[from].push_back (t-2); G[to].push_back (t-1);} int maxflow (int s,int t) {int flow = 0;//maximum flow is initialized to 0 for (;;) {///core algorithm, it should be noted that we initially add in the edge of the traffic is 0, by seeking the minimum residue gradually augmented, update the maximum Flow memset (A,0,sizeof (a)); Queue<int> Q; Q.push (s); A[s] = INF; 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 (!a[e.to]&&e.cap > E.flow) {p[e.to] = G[x][i]; Record the path of each increment of traffic a[e.to] = min (a[x],e.cap-e.flow); Find out the minimum value of Q.push (e.to) of all residuals in the road; }} if (A[t]) break; Reach the end, exit} if (!a[t]) break; End residue is 0, can not be augmented, break; for (int u = t;u = s; u = edges[p[u]].from) {Edges[p[u]].flow + = a[t];//Adds the remainder to the path edges[p[u]^1]. Flow-= A[t]; Subtract the reverse path by ' flow + = a[t]; Update the total maximum flow} return flow;} void print () {for (int i=0;i<edges.size (); i+=2) {ans[edges[i].from][edges[i].to] = Edges[i].flow; } for (int i=1;i<=r;i++) {printf ("%d", ans[i][101]+1); for (int j=2;j<=c;j++) printf ("%d", ans[i][j+100]+1); printf ("\ n"); }}int Main () {scanf ("%d", &t); while (t--) {scanf ("%d%d", &r,&c); Init (); for (int i=1;i<=r;i++) SCANF ("%d", &a[i]); for (int i=1;i<=c;i++) scanf ("%d", &b[i]); for (int i=1;i<=r;i++) aa[i] = A[i]-a[i-1]; for (int i=1;i<=c;i++) bb[i] = B[i]-b[i-1]; for (int i=1;i<=r;i++) Addedge (0,I,AA[I]-C); for (int i=1;i<=c;i++) Addedge (i+100,300,bb[i]-r); for (int i=1;i<=r;i++)//Plus 100, and 0 and 300 are all meant to differentiate the node value from the for (int j=1;j<=c;j++) Addedge (i,j+100,19); printf ("Matrix%d\n", ++kase); int cnt = Maxflow (0,300); Print (); if (T) printf ("\ n"); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
11082-matrix decompressing (Network Flow Modeling | binary graph matching)