Purple book on the network flow part of the first example, just learned the maximum flow, still do not understand the binary map matching, here is only to say what I use the maximum flow is how to do it.
We can imagine a source point, a meeting point, and then for each device plug, a line from the source point, for each outlet, a line to the meeting point, and the capacity is 1. Then for each converter, from the original plug to the transformed plug with an edge, because the number of converters infinitely large, so the capacity is infinite.
In this way, we abstract the original problem into the maximum flow problem, cleverly assigning the capacity of the access path to 1, so that each device can match to a unique container. Purple Book said that this is a two-figure matching, perhaps also implies that its principle of it.
The maximum flow problem seems to be suitable for solving that kind of matching problem, because of its disorder, it is difficult to define the stage, and the state may go back to the previous state, so we can't use dynamic planning to solve.
In addition, it can be seen that in order to apply network flow, we should first abstract the actual problem, and abstract the nature or quantity of the concrete meaning into the traffic or capacity in the network flow model.
See the code for details:
#include <bits/stdc++.h>using namespace Std;const int INF = 100000000;const int maxn = 505;int T,CNT,N,M,T,K,A[MAXN] , P[maxn];char s[maxn],buf[maxn];map<string,int> pp;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 ();} 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];//Add the remainder to the path EDGES[P[U]^1].F Low-= a[t]; Subtract the reverse path by ' flow + = a[t]; Update the total maximum flow} return flow;} int main () {scanf ("%d", &t); while (t--) {scanf ("%d", &n); Init (); Pp.clear (); CNT = 1; for (int i=0;i<n;i++) {scanf ("%s", s); if (!pp.count (s)) pp[s] = cnt++; Addedge (pp[s],500,1); Because the plug type is maximum 400, the meeting point is set to scanf ("%d", &m); for (int i=0;i<m;i++) {scanf ("%s%s", buf,s); if (!pp.count (s)) pp[s] = cnt++; Addedge (0,pp[s],1); Set the source point to 0} scanf ("%d", &k); for (int i=0;i<k;i++) {scanf ("%s%s", buf,s); if (!pp.count (BUF)) pp[buf] = cnt++; if (!pp.count (s)) pp[s] = cnt++; Addedge (Pp[buf],pp[s],inf); } printf ("%d\n", M-maxflow (0,500)); if (T) printf ("\ n"); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
753-a Plug for UNIX (maximum stream or binary graph matching)