POJ-2195 going Home---miles algorithm for minimum weight matching (negative edge)

Source: Internet
Author: User

Topic Links:

https://vjudge.net/problem/POJ-2195

Main topic:

Given a map of n*m, there are several man and house on the map, and the number of mans is the same as that of House. The man spends $ $ per shift (i.e. unit cost = unit distance) and a house can only be accommodated in one man. All the man is now required to stay at house for a minimum fee.

Ideas:

KM Algorithm Portal: Understanding the use of the article

Each man and house to establish a weighted dichotomy, the Manhattan distance is the value of the edge, here requires the minimum cost, that is, the minimum weight of the binary map matching, but the KM algorithm is the binary graph maximum weight matching, so here with the negative number of the edge to find the best match, the answer to the negative number is the minimum right matching.

Note: The title says house up to 100, but does not specify the range of man, so man should be up to 100*100.

We should use House for the X part of the two, because the complexity of the algorithm is related to the number of X points, so a house with a few points is the X part

Because there is a negative number, the top value in the pre-processing x initialization should be-inf, cannot be 0

The rest is the template.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 Const intMaxx = -+5;//House 's upper limit7 Const intMaxy =10000+5;//Man 's upper limit8 Const intINF =0x3f3f3f3f;9 intCntx, Cnty;//number of points at x, number of y pointsTen BOOLVisx[maxx], Visy[maxy];//whether to join the augmented path One intWx[maxx], Wy[maxy];//Top Label Value A intCx[maxx], Cy[maxy];//Matching points - intMinz//the difference between the minimum value of the top and the right edge - intMap[maxx][maxy];//Save Edge the  - BOOLDfsintu) - { -Visx[u] =1; +      for(intv =1; V <= cnty; v++) -     { +         if(!visy[v])//have not yet joined the augmented road A         { at             intt = Wx[u] + wy[v]-Map[u][v]; -             //calculates the difference between the edge and the top mark, which is 0 for the equal sub-graph -             if(T = =0) -             { -VISY[V] =1; -                 if(Cy[v] = =-1|| DFS (Cy[v]))//has not matched or found the augmented path in reverse in                 { -CY[V] =u; toCx[u] =v; +                     //cout<<u<< "V" <<v<<endl; -                     return 1; the                 } *             } $             Else if(T >0) Minz =min (Minz, t);Panax Notoginseng         } -     } the     return 0; + } A  the intKM () + { -memset (CX,-1,sizeof(CX)); $memset (CY,-1,sizeof(CY)); $memset (WX,-inf,sizeof(WX));//note that the negative edges are stored here, so the initialization must be negative infinity -memset (WY,0,sizeof(WY)); -      for(inti =1; I <= Cntx; i++)//preprocessing the top mark value of x the     { -          for(intj =1; J <= Cnty; J + +)Wuyi         { theWx[i] =Max (Wx[i], map[i][j]); -             //cout<<wx[i]<<endl; Wu         } -     } About      for(inti =1; I <= Cntx; i++) $     { -          while(1) -         { -Minz =INF; Amemset (VISX,0,sizeof(VISX)); +memset (Visy,0,sizeof(Visy)); the             if(Dfs (i)) Break; -  $              for(intj =1; J <= Cntx; J + +) the                 if(Visx[j]) wx[j]-=Minz; the              for(intj =1; J <= Cnty; J + +) the                 if(Visy[j]) wy[j] + =Minz; the         } -     } in     intAns =0; the      for(inti =1; I <= Cntx; i++) the         if(Cx[i]! =-1) ans + =Map[i][cx[i]]; About     returnans; the } the structnode the { +     intx, y; - node () {} theNodeintXinty): x (x), Y (y) {}Bayi }; the node House[maxx], Man[maxy]; the Charm[ the][ the]; - intMain () - { the     intN, M; the      while(Cin >> n >> m && (n &&m)) the     { theCntx = Cnty =0; -          for(inti =0; I < n; i++)//Read Graph the         { theCIN >>M[i]; the              for(intj =0; J < M; J + +)94             { the                 if(M[i][j] = ='H') House[++cntx] =node (i, j); the                 Else if(M[i][j] = ='m') Man[++cnty] =node (i, j); the             }98         } About  -          for(inti =1; I <= Cntx; i++)//Building Map101         {102              for(intj =1; J <= Cnty; J + +)103             {104MAP[I][J] =-abs (house[i].x-man[j].x)-ABS (HOUSE[I].Y-man[j].y); the             }106         }107cout<< (-km ()) <<Endl;108     }109     return 0; the}

POJ-2195 going Home---miles algorithm for minimum weight matching (negative edge)

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.