Happens to belong to a circle, that equivalence with each point has a unique precursor and successor, then take a point apart, the precursor as the S set and successor as the T set, and then the edge, run the minimum right to match the two-minute graph.
The Hungarian algorithm for the maximum matching of binary graphs can also be understood, but the most powerful matching km algorithm is what a ghost ah ... Only the fee stream will be written.
#include <bits/stdc++.h>using namespacestd;Const intMAXN = $+6;structedge{intv,cap,cost,nxt;}; Vector<Edge>edges;#definePB push_backintHEAD[MAXN];voidAddedge (intUintVintCintc) {edges. PB (Edge{v,c,c,head[u]}); Head[u]= Edges.size ()-1;}voidAddedge (intUintVintCintc) {Addedge (u,v,c,c); Addedge (V,u,0,-c);} typedefintll;intS =0, T =1, vcnt;intD[MAXN],P[MAXN],A[MAXN];BOOLVIS[MAXN];Const intINF =0x3f3f3f3f; ll MCMF (ll&Cost ) { Cost=0; ll Flow=0; while(true) {memset (Vis,0,sizeof(BOOL)*vcnt); memset (d,0x3f,sizeof(int)*vcnt); Queue<int>Q; Q.push (S); D[s]=0; A[s] =INF; while(Q.size ()) {intU = Q.front (); Q.pop (); Vis[u] =false; for(inti = Head[u]; ~i; i =edges[i].nxt) {Edge&e =Edges[i]; if(E.cap && d[e.v] > d[u]+e.cost) {D[E.V]= D[u] +E.cost; P[E.V]=i; A[E.V]=min (e.cap,a[u]); if(!VIS[E.V]) {VIS[E.V]=true; Q.push (E.V); } } } } if(D[t] = = INF)returnflow; Flow+=A[t]; Cost+ = a[t]*D[t]; for(inti = T; I! = S; i = edges[p[i]^1].v) {Edges[p[i]].cap-=A[t]; Edges[p[i]^1].cap + =A[t]; } } return-1;}voidinit () {memset (head,-1,sizeof(int)*vcnt); Edges.clear ();}intMain () {//freopen ("In.txt", "R", stdin); intN; S=0; while(SCANF ("%d",&N), N) {vcnt=2*n+2; Init (); T=2*n+1; for(inti =1; I <= N; i++) {Addedge (s,i,1,0); Addedge (i+n,t,1,0); } for(inti =1; I <= N; i++){ intV,c; while(SCANF ("%d",&v) {scanf ("%d",&c); Addedge (I,v+n,1, c); } } intCost ; intFlow =MCMF (cost); if(Flow<n) puts ("N"); Elseprintf"%d\n", cost); } return 0;}
UVA 1349 Optimal Bus Route Design