HDU 2255 Ben-off make money (km algorithm template)

Source: Internet
Author: User

Topic Links:

http://acm.hdu.edu.cn/showproblem.php?pid=2255

Problem Solving Ideas:

After understanding the KM algorithm, you can directly apply the KM algorithm, KM algorithm: Complete matching under the maximum weight matching,

Code:

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5 using namespacestd;6 7 #defineMAXN 3108 #defineINF 0x3f3f3f3f9 intMAP[MAXN][MAXN], USED[MAXN], S[MAXN], N, m;Ten intLX[MAXN], LY[MAXN], VISX[MAXN], VISY[MAXN]; One  A intFind (intx) -{//Hungarian algorithm, augmented road -VISX[X] =1; the      for(intI=1; i<=n; i++) -     { -         if(!visy[i] && lx[x] + ly[i] = =Map[x][i]) -         { +Visy[i] =1; -             if(!used[i] | |find (Used[i])) +             { AUsed[i] =x; at                 return 1; -             } -         } -         Else -S[i] = min (s[i], lx[x]+ly[i]-map[x][i]); -     } in     return 0; - } to  + intKM () - { thememset (LX,0,sizeof(LX)); *memset (Ly,0,sizeof(ly)); $memset (Used,0,sizeof(used));Panax Notoginseng      for(intI=1; i<=n; i++) -          for(intj=1; j<=n; J + +) theLx[i] =Max (Lx[i], map[i][j]); +      for(intI=1; i<=n; i++) A     { the          for(intj=1; j<=n; J + +) +S[J] =INF; -  $          while(1) $         { -memset (VISX,0,sizeof(VISX)); -memset (Visy,0,sizeof(Visy)); the  -             if(Find (i))Wuyi                  Break; the             intnum =INF; -              for(intj=1; j<=n; J + +) Wu                 if(!Visy[j]) -num =min (num, s[j]); About              for(intj=1; j<=n; J + +) $             { -                 if(Visx[j]) -LX[J]-=num; -                 if(Visy[j]) ALY[J] + =num; +                 Else theS[J]-=num; -             } $         } the     } the     intres =0; the      for(intj=1; j<=n; J + +) theRes + =Map[used[j]][j]; -     returnRes; in } the intMain () the { About      while(SCANF ("%d", &n)! =EOF) the     { the          for(intI=1; i<=n; i++) the              for(intj=1; j<=n; J + +) +scanf ("%d", &map[i][j]); -printf ("%d\n", KM ()); the     }Bayi     return 0; the}

HDU 2255 Ben-off make money (km algorithm template)

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.