SYSU-4, UVA 12711, general figure Max (small) right match

Source: Internet
Author: User
Tags in degrees

The main idea: to give you a 100-point graph, divided into two points set, requires a point set of all points of the degree is odd, the point of the B-point set is even, to find a minimum edge right edge set to meet this constraint.

Solution: We need to abstract some properties before we can get the general diagram of the minimum matching practice

First of all, even if the points are not considered at all, or they do not select, or they as the point on the path (out of degrees = in degrees), so directly to do the FLYOD, you can turn the graph into only a point set of graphs.

2nd, for a legitimate answer, a side cannot be selected two times or more, because this can be removed two this edge, still can get a legitimate diagram, and the answer is smaller, which is used to prove the above abstract image of the correctness of the practice.

Then we abstracted the diagram, we can know that we do a minimum match, because this can meet the constraints of the odd points, but also can prove that the edge of the least choice (larger graph can be deleted by the end of the matching form)

The general figure of the minimum weight matching practice many, another great God's blog introduced a way to eliminate the ring, but I was xzj Amway a random algorithm to get, feel good, the code is short and good understanding.

#include <cstdio>#include<string>#include<iostream>#include<algorithm>#include<cmath>#include<cstring>#include<complex>#include<Set>#include<vector>#include<map>#include<queue>#include<deque>#include<ctime>using namespacestd;Const DoubleEPS = 1e-8;#defineABS (x) ((x) <0? ( -(x)):(x))#defineSQR (x) ((x) * (x))#defineMIN (a) < (b) ( A):(B))#defineMAX (a) (a) > (b)? ( A):(B))#defineLson (x) ((x) <<1)#defineRson (x) (((x) <<1) +1)#defineLowbit (x) ((x) & (-(x)))#defineMAXN 111#defineLL Long Long#defineOO 214748364intW[MAXN][MAXN], G[MAXN][MAXN];intMATCH[MAXN], PATH[MAXN], D[MAXN], P[MAXN], Len;BOOLV[MAXN];intN, M, K;voidinit () {scanf ("%d%d%d", &n, &m, &k);  for(inti =0; I < n; ++i) for(intj =0; J < N; ++J) G[i][j] =OO;  for(inti =0; I < m; ++i) {intx, y, Z; scanf ("%d%d%d", &x, &y, &z); --x; --y; if(x = = y)Continue; if(Z <G[x][y]) {G[x][y]= G[y][x] =Z; }    }     for(intK =0; K < n; ++k) for(inti =0; I < n; ++i) for(intj =0; J < N; ++j) G[i][j]= Min (G[i][j], g[i][k] +G[k][j]);  for(inti =0; I < K; ++i) { for(intj =0; J < K; ++j) {W[i][j]= OO-G[i][j]; }    }}BOOLDfsinti) {Path[len++] =i; if(V[i])return true; V[i]=true;  for(intj =0; J < K; ++j) {if(I! = J && Match[i]! = J &&!V[j]) {            intKok =Match[j]; if(D[kok] < D[i] + w[i][j]-W[j][kok]) {D[kok]= D[i] + w[i][j]-W[j][kok]; if(Dfs (Kok))return true; }        }    }    --Len; V[i]=false; return false;}voidsolve () {if(k&1) {puts ("Impossible"); return ; }     for(inti =0; I < K; ++i) P[i] = i, match[i] = i ^1; intCNT =0;  for (;;) {Len=0; BOOLFlag =false; memset (d,0,sizeof(d)); memset (V,0,sizeof(v));  for(inti =0; I < K; ++i) {if(Dfs (P[i])) {flag=true; intt = Match[path[len-1]], j = len-2;  while(Path[j]! = Path[len-1]) {match[t]=Path[j];                    Swap (T, Match[path[j]]); --J; } Match[t]=Path[j]; MATCH[PATH[J]]=T;  Break; }        }        if(!flag) {            if(++cnt >=3) Break; Random_shuffle (p, p+k); }    }    intAns =0;  for(inti =0; I < K; ++i) {intt =W[i][match[i]]; //cout << t << Endl;        if(T = =0) {puts ("Impossible"); return ; } ans+ = OO-T; } printf ("%d\n", ans/2);}intMain () {Freopen ("Test.txt","R", stdin); Srand (Time (0)); intCAs scanf"%d", &CAs);  for(inttt =1; TT <= CAS; ++TT) {printf ("Case %d:", TT);        Init ();    Solve (); }    return 0;}
UVA12711

SYSU-4, UVA 12711, general figure Max (small) right match

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.