UVA 11383 Golden Tiger Claw golden Tiger Claw (km algorithm)

Source: Internet
Author: User

Test instructions: To a n*n matrix, each lattice has a positive integer w[i][j], try for each row and each column to determine a number row[i] and col[i], so that arbitrary lattice w[i][j]<=row[i]+col[j] constant set up. First lose row, then output col, and then output the total sum (the sum should be as small as possible).

Ideas:

Km the top mark in the algorithm is to keep the lx[i]+ly[j]>=g[i][j] and then the maximum right and matching, but this maximum power and does not matter. We can think of row[i] as a man, Col[i] as a woman, so that the total number of men and women is equal. In general, Lx[i] or ly[i] only need to take the largest number in the row/column to ensure that the requirements are met, but this is too large to be adjusted to make the sum smaller. The process of the KM algorithm is a process of adjustment, each pair of matching men and women of that side of the weight will meet the equal sign W[i][j=row[i]+col[j]], at least one to meet the equal sign, so as to ensure that row[i]+col[j] is the smallest, that is, from the J column to see, col[j ] satisfies the condition and the smallest, from the I line, row[i] satisfies the condition and the smallest. This is exactly the same as the KM algorithm for maximum power.

1#include <bits/stdc++.h>2 #defineLL Long Long_long_max3 #defineINF 0x7f7f7f7f4 #defineLL Long Long5 using namespacestd;6 Const intn=510;7 8 intGrid[n][n], girl[n];9 intLx[n], ly[n], slack[n];Ten BOOLS[n], t[n]; One intN; A  - BOOLDFS (intx) - { thes[x]=true; -      for(intI=1; i<=n; i++) -     { -         if(T[i])Continue; +         inttmp=lx[x]+ly[i]-Grid[x][i]; -         if(tmp==0) +         { At[i]=true; at             if(girl[i]==0||DFS (Girl[i])) -             { -girl[i]=x; -                 return true; -             } -         } in         Else if(tmp<Slack[i]) -slack[i]=tmp; to     } +     return false; - } the  *  $ Panax Notoginseng intKM () - { theMemset (Girl,0,sizeof(Girl)); +memset (Lx,0,sizeof(Lx)); Amemset (Ly,0,sizeof(Ly)); the      for(intI=1; i<=n; i++) +          for(intj=1; j<=n; J + +) -lx[i]=Max (Lx[i], grid[i][j]); $  $      for(intI=1; i<=n; i++)//for each tree -     { -          for(intj=1; j<=n; J + +) slack[j]=INF; the          while(1) -         {Wuyimemset (S),0,sizeof(S)); thememset (T,0,sizeof(T)); -             if(DFS (i)) Break;//find the matching ants. Wu  -  About             intD=INF; $              for(intj=1; j<=n; J + +)//Find minimum D -             { -                 if(! T[J] && d>Slack[j]) -D=Slack[j]; A             } +  the              for(intj=1; j<=n; J + +)//Update Tree -             { $                 if(S[j]) thelx[j]-=D; the             } the  the              for(intj=1; j<=n; J + +)//Update Ants -             { in                 if(T[j]) ly[j]+=D; the                 Elseslack[j]-=D; the             } About         } the     } the     intsum=0; the      for(intI=1; i<=n; i++) sum+=lx[i]+Ly[i]; +     returnsum; - } the Bayi  the  the  - intMain () - { theFreopen ("Input.txt","R", stdin); the      while(~SCANF ("%d",&N)) the     { thememset (Grid,0,sizeof(grid)); -          for(intI=1; i<=n; i++) the              for(intj=1; j<=n; J + +) thescanf"%d",&grid[i][j]); the 94         intans=KM (); theprintf"%d", lx[1]);//notable output formats.  the          for(intI=2; i<=n; i++) printf ("%d", Lx[i]); theprintf"\ n");98printf"%d", ly[1]); About          for(intI=2; i<=n; i++) printf ("%d", Ly[i]); -printf"\ n");101printf"%d\n", ans);102     }103     return 0;104}
AC Code

UVA 11383 Golden Tiger Claw golden Tiger Claw (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.