POJ 21,952 Sub-graph with weighted matching

Source: Internet
Author: User

Test instructions: There is a matrix, some lattice someone, some lattice has a house, everyone can move up and down, ask everyone into a house, everyone needs to walk the distance and the minimum is how much.

Seems to have seen a lot of such a similar problem, will not, now know is to use the KM algorithm to do

KM algorithm is not yet understood, the first set of templates to do

Sample Input

2 2.mh.5 5HH.. m...............mm. H7 8...H ..... H....... H....mmmhmmmm ... H....... H....... H.... 0 0

Sample Output

21028

1#include <stdio.h>2#include <iostream>3#include <string.h>4#include <algorithm>5#include <math.h>6 using namespacestd;7 Const intmaxn= -;8 Const intinf=0x3fffffff;9 intG[MAXN][MAXN],MATCH1[MAXN],MATCH2[MAXN];Ten intKM (intMintN) One { A     inti,j,k,p,q; -     intL1[MAXN],L2[MAXN]; -     intS[MAXN],T[MAXN]; the     intret=0; -      for(i=0; i<m;i++) -     { -l1[i]=-INF; +          for(j=0; j<n;j++) -           if(g[i][j]>L1[i]) +l1[i]=G[i][j]; A         if(L1[i]==-inf)return-1;//cannot match at     } -      for(i=0; i<n;i++) l2[i]=0; -  -memset (match1,-1,sizeof(MATCH1)); -memset (match2,-1,sizeof(MATCH2)); -      for(i=0; i<m;i++) in     { -memset (t,-1,sizeof(t)); to          for(s[p=q=0]=i;p<=q&&match1[i]<0;p + +) +              for(k=s[p],j=0;j<n&&match1[i]<0; j + +) -                 if(l1[k]+l2[j]==g[k][j]&&t[j]<0) the                 { *s[++q]=match2[j],t[j]=K; $                     if(s[q]<0)Panax Notoginseng                        for(p=j;p>=0; j=p) -match2[j]=k=t[j],p=match1[k],match1[k]=J; the                 } +  A         if(match1[i]<0) the         { +              for(I--, p=inf,k=0; k<=q;k++) -                 for(j=0; j<n;j++) $                  if(t[j]<0&&l1[s[k]]+l2[j]-g[s[k]][j]<p) $p=l1[s[k]]+l2[j]-G[s[k]][j]; -  -  the              for(j=0; j<n;j++)if(t[j]>=0) l2[j]+=p; -              for(k=0; k<=q;k++) l1[s[k]]-=p;Wuyi         } the     } -      for(i=0; i<m;i++) Wu     { -         if(match1[i]<0)return-1;//cannot match About         if(G[i][match1[i]]<=-inf)return-1; $ret+=G[i][match1[i]]; -     } -     returnret; - } A structNode + { the     intx, y; - }; $ Node NODE1[MAXN],NODE2[MAXN]; the CharSTR[MAXN]; the intMain () the { the     intn,m; -     intNx,ny; in      while(SCANF ("%d%d", &n,&m)! =EOF) the     { the         if(n==0&&m==0) Break; Aboutnx=0; theny=0; the          for(intI=0; i<n;i++) the         { +scanf"%s",&str); -              for(intj=0; j<m;j++) the             {Bayi                 if(str[j]=='m') the                 { thenode1[nx].x=i; -node1[nx].y=J; -nx++; the                 } the                 Else if(str[j]=='H') the                 { thenode2[ny].x=i; -node2[ny].y=J; theny++; the                 } the             }94         } the          for(intI=0; i<nx;i++) the          for(intj=0; j<ny;j++) the         {98G[i][j]=-abs (node1[i].x-node2[j].x)-abs (node1[i].y-node2[j].y); About         } -printf"%d\n",-KM (Nx,ny));101     }102     return 0;103}

POJ 21,952 Sub-graph with weighted matching

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.