HDU 2255 Ben well-off make money miles simple explanation of the algorithm

Source: Internet
Author: User
Tags benchmark

KM algorithms are generally used to find the best matching of binary graphs.

Steps:

1. Initialize the feasible benchmark

2. Use the Hungarian algorithm to judge the newly added points

3. If you are unable to add new, modify the feasible benchmark

4. Repeat the 2.3 operation until you find an exact match for the equal sub-graph.

Summary of each step:

1. Establishment of 2 feasible benchmarks according to the dichotomy chart;

LX is the feasible benchmark for X, initializing Lx[i] to be the largest edge connected to the I point

Ly is the feasible benchmark for Y, initialized to 0.

The judging condition of feasibility should be lx[x]+ly[y] >= map[x][y].

2. For newly added points, the Hungarian algorithm is used to determine whether pity Dorado can be added to the old sub-graph to form a new sub-graph.

There are two possibilities for the added points:

2.1. Find a Y point, the y point is connected, then connect to the point, and meet the conditions

2.2 Find a Y point, the y point has been connected by a X1 point, then the X1 point through the line deep search, if X1 can find a new connection point, then modify the X1 connection path, and make x points and Y points connected.

3. If it is found that no more edges are added to the existing equal sub-graphs, the proof is no longer lx[x]+ly[y]==map[x][y], then we modify the feasible benchmark for x and Y.

When an augmented path is not found, the X-vertex set on the searched path is set to S,y-T, and for all points in S and points not in T YJ, d=min{(L (xi) +l (YJ)-weight (XIYJ)} is computed, Subtracting d from the X benchmark in the S-set and adding it to the benchmark for y in the T-set, as the X-benchmark in the S-set is reduced, and the Y-benchmark in t does not change, the LX (x) +ly (y) in the two sets becomes smaller, that is, lx[x]+ly[y] becomes smaller, and there may be lx[x]+ Ly[y]==map[x][y] is established, that is, new edges may be added.

4. Repeat the 2.3 process, until you find the complete equal sub-graph.

Time complexity: approximately O (n^3) after optimization;

Code:

#include <cstdio>#include<stdio.h>#include<cstdlib>#include<cmath>#include<iostream>#include<algorithm>#include<cstring>#include<vector>#include<queue>#defineINF 0x3f3f3f3f#defineMAX 1005#defineMoD 1000000007using namespacestd;intVisx[max],visy[max],lx[max],ly[max],linker[max],slack[max],map[max][max],n;BOOLDFS (intx) {Visx[x]=1;  for(inty=1; y<=n;y++)    {        if(Visy[y])Continue; inttmp=lx[x]+ly[y]-Map[x][y]; if(tmp==0) {Visy[y]=1; if(linker[y]==-1||DFS (Linker[y])) {Linker[y]=x; return true; }        }        Else{Slack[y]=min (slack[y],tmp); }    }    return false;}intKM () {inti,j; memset (ly,0,sizeof(ly));//Initialize feasible benchmark LYmemset (linker,-1,sizeof(linker));  for(i=1; i<=n; i++)//Initialize feasible benchmark LX{Lx[i]=-INF;  for(j=1; j<=n; J + +) Lx[i]=Max (lx[i],map[i][j]); }     for(intx=1; x<=n; X + +)    {         for(i=1; i<=n; i++) Slack[i]=INF;  while(1) {memset (VISX,0,sizeof(VISX)); memset (Visy,0,sizeof(Visy)); if(DFS (x))//If you can add a direct jump                 Break; intD=INF;  for(i=1; i<=n; i++)            {                if(!Visy[i]) d=min (SLACK[I],D);//Otherwise, according to the greedy thought, find the smallest feasible benchmark modified value            }             for(i=1; i<=n; i++)//modifying the LX benchmark            {                if(Visx[i]) lx[i]-=D; }             for(i=1; i<=n; i++)//Modify the LY benchmark            {                if(Visy[i]) ly[i]+=D; ElseSlack[i]-=d;//slack by Lx[x]+ly[y]-map[x][y], the unlabeled value in ly does not change, the value marked in the LX is reduced, the slack should be reduced            }        }    }    intans=0;  for(i=1; i<=n;i++) {ans+=Map[linker[i]][i]; }    returnans;}intMain () { while(SCANF ("%d", &n)! =EOF) {         for(intI=1; i<=n; i++)        {             for(intj=1; j<=n; J + +) scanf ("%d",&Map[i][j]); }        intans=KM (); printf ("%d\n", ans); }    return 0;}

HDU 2255 Ben well-off make money miles simple explanation of the algorithm

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.