UVA1349
Test instructions: Given some weighted edges, it is found that these edges are constructed into rings and the total weights are minimized.
Solution:
The matching problem of weighted binary graphs can be solved by using km algorithm.
The requirement of maximum right matching is initialization g[i][j] is 0, can run directly;
The minimum right to match is the initialization g[i][j] is-inf, the edge is negative, and the inverse number of the final output answer.
Because each point is required to belong to a circle, it means that each point has a unique successor. Conversely, as long as each point has a unique successor, each point must belong to a certain circle.
The only thing we think about is the concept of a two-point graph, where we create a binary graph from u to V for each of the points, and then the problem is converted to the least-weight perfect match problem on the two-minute graph.
1#include <bits/stdc++.h>2 #defineREP (I, A, b) for (int i = (a); I < (b); i++)3 #defineMEM (a,x) memset (A,x,sizeof (a))4 #defineINF 0x3f3f3f3f5 #defineMAXN 100+106 using namespacestd;7 8 structKM {9 intN;Ten intG[MAXN][MAXN]; One intLX[MAXN], LY[MAXN]; A intSLACK[MAXN];//How much weight is required to record the match from X to Y? - intMATCH[MAXN];//records the points in the Y-set that each X-point matches to - BOOLS[MAXN], T[MAXN]; the - voidInitintN) { - This->n =N; - for(inti =0; I < n; i++) + for(intj =0; J < N; J + +) -G[I][J] =-INF; + //Note that if this is the maximum weight value match and the assignment is 0 A //minimum weights matching and is-inf at } - - voidAdd_edge (intUintVintval) { -G[U][V] =Max (G[u][v], Val); - } - in BOOLDfsinti) { -S[i] =true; to for(intj =0; J < N; J + +) { + if(T[j])Continue; - intTMP = Lx[i] + ly[j]-G[i][j]; the if(!tmp) { *T[J] =true; $ if(Match[j] = =-1||DFS (Match[j])) {Panax NotoginsengMATCH[J] =i; - return true; the } + } A ElseSLACK[J] =min (slack[j], TMP); the } + return false; - } $ $ voidUpdate () { - intA =INF; - for(inti =0; I < n; i++) the if(! T[i]) A =min (A, slack[i]); - for(inti =0; I < n; i++) {Wuyi if(S[i]) Lx[i]-=A; the if(T[i]) Ly[i] + =A; - } Wu } - About voidkm () { $ for(inti =0; I < n; i++) { -Match[i] =-1; -Lx[i] =-inf; Ly[i] =0; - for(intj =0; J < N; J + +) ALx[i] =Max (Lx[i], g[i][j]); + } the for(inti =0; I < n; i++) { - for(intj =0; J < N; J + +) Slack[j] =INF; $ while(1) { the for(intj =0; J < N; J + +) S[j] = t[j] =false; the if(Dfs (i)) Break; the Elseupdate (); the } - } in } the }men; the About intMain () { the intN; the while(SCANF ("%d", &n) = =1&&N) { the men.init (n); +REP (U,0, N) { - intv; the while(SCANF ("%d", &v) &&v) {Bayi intW scanf"%d", &W); thev--; theMen.add_edge (U, V,-W); - } - } the the men.km (); the intAns =0, flag =1; theREP (I,0, N) { - if(Men.g[men.match[i]][i] = =-INF) { the //There is no match to, is not successful, because the title of the request is a perfect match theFlag =0; the Break;94 } theAns + = men.g[men.match[i]][i];//Cumulative Weight value the } the if(!flag) printf ("n\n");98 Elseprintf"%d\n",-ans);//Finally, the output is negative. About } - return 0;101}
UVA1349 (with weighted binary graph maximum match-km algorithm template)