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)