POJ 2195 going Home (km algorithm)

Source: Internet
Author: User

Topic Links:

http://poj.org/problem?id=2195

Problem Solving Ideas:

The man and home are extracted, and then figure out the distance of each man and home, and then set up a matching diagram, the template for the KM algorithm, to find the minimum weight matching, km template is generally the maximum right to match, the smallest words, generally take negative, of course, if you feel negative forcing lattice too low, You can also use the following methods to improve the template.

Code:

1 //Write code too vegetables, do not understand the establishment of the matching diagram when the table from zero will be hard WA, but changed to 1 will be AC, have seen the small partners please speak out, kneeling XIE!!!!! 2 //The minimum weight matching of the KM algorithm for complete matching3#include <cstdio>4#include <cstring>5#include <iostream>6#include <algorithm>7#include <cmath>8 using namespacestd;9 Ten #defineMAXN 110 One #defineINF 0x3f3f3f3f A intMAP[MAXN][MAXN]/*Match Graph*/; - intUSED[MAXN]/*matching values for x and y*/; - intS[MAXN]/*Modification Amount*/; the intN, M; - intLX[MAXN], LY[MAXN];/*Top Label*/ - intVISX[MAXN], VISY[MAXN]; -  + structnode - { +     intx, y; A     voidInitintXxintyy) at     { -x =xx; -y =yy; -     } - }; -  in intFas (intXinty) - { to     if(X >y) +         returnX-y; -     returnY-x; the } * intFind (intx) ${//Hungarian algorithm, augmented road warp, enlarged equal sub-graphPanax NotoginsengVISX[X] =1; -      for(intI=1; i<n; i++) the     { +         if(!visy[i] && lx[x]+ly[i] = =Map[x][i]) A         { theVisy[i] =1; +             if(!used[i] | |find (Used[i])) -             { $Used[i] =x; $                 return 1; -             } -         } the         Else -             //S[i] = min (S[i], lx[x] + ly[i]-map[x][i]);Wuyi             //Maximum weight matching theS[i] = min (S[i], Map[x][i]-(lx[x] + ly[i]));//Update the modified value, ensure the minimum, so that the results obtained are optimal -     } Wu     return 0; - } About intKM () $ { -memset (Used,0,sizeof(used)); -memset (Ly,0,sizeof(ly)); -      for(intI=1; i<n; i++)//Initialize the top label ALx[i] = INF;//Lx[i] = 0; maximum weight matching +  the      for(intI=1; i<n; i++) -          for(intj=0; j<n; J + +) $Lx[i] =min (lx[i], map[i][j]); the  the      for(intI=1; i<n; i++) the     { the          for(intj=1; j<n; J + +) -S[J] =INF; in          while(1) the         { thememset (VISX,0,sizeof(VISX)); Aboutmemset (Visy,0,sizeof(Visy)); the  the             if(Find (i)) the                  Break; +  -             intnum =INF; the              for(intj=1; j<n; J + +)Bayi                 if(!visy[j])// thenum =min (num, s[j]); the  -              for(intj=1; j<n; J + +) -             { the                 if(Visx[j]) theLX[J] + = num;//lx[j]-= num; maximum weight matching the                 if(Visy[j]) theLY[J]-= num;//ly[j] + = num; Maximum weight matching -                 Else theS[J]-=num; the             } the         }94     } the     intres =0; the      for(intI=1; i<n; i++) theRes + =Map[used[i]][i];98     returnRes; About  - }101 intMain ()102 {103     CharSTR[MAXN];104     intA, B; the node HOME[MAXN], MAN[MAXN];106      while(SCANF ("%d%d", &a, &b), A +b)107     {108n = m =1;109memset (Map,0,sizeof(map)); the 111          for(intI=0; i<a; i++) the         {113scanf ("%s", str); the              for(intj=0; STR[J]; J + +) the             { the                 if(Str[j] = ='H')117home[n++].init (i, j);118                 if(Str[j] = ='m')119man[m++].init (i, j); -             }121         }122          for(intI=1; i<n; i++)123              for(intj=1; j<m; J + +)124MAP[I][J] = Fas (home[i].x, man[j].x) +Fas (HOME[I].Y, man[j].y); theprintf ("%d\n", KM ());126     }127     return 0; -}

POJ 2195 going Home (km 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.