HDU2255-奔小康赚大钱-二分图最大权值匹配-KM算法
2016-09-01 13:34
579 查看
二分图最大权值匹配问题。用KM算法。
最小权值的时候把权值设置成相反数
最小权值的时候把权值设置成相反数
/*--------------------------------------------------------------------------------------*/ #include <algorithm> #include <iostream> #include <cstring> #include <ctype.h> #include <cstdlib> #include <cstdio> #include <vector> #include <string> #include <queue> #include <stack> #include <cmath> #include <set> #include <map> //debug function for a N*M array #define debug_map(N,M,G) printf("\n");for(int i=0;i<(N);i++)\ {for(int j=0;j<(M);j++){\ printf("%d",G[i][j]);}printf("\n");} //debug function for int,float,double,etc. #define debug_var(X) cout<<#X"="<<X<<endl; #define LL long long const int INF = 0x3f3f3f3f; const LL LLINF = 0x3f3f3f3f3f3f3f3f; /*--------------------------------------------------------------------------------------*/ using namespace std; int N,M,T; const int maxn = 310; int nx,ny; int g[maxn][maxn]; int linker[maxn],lx[maxn],ly[maxn]; int slack[maxn]; bool visx[maxn],visy[maxn]; bool DFS(int x) { visx[x] = true; for(int y=0;y<ny;y++) { if(visy[y]) continue; int tmp = lx[x] + ly[y] - g[x][y]; if(tmp == 0) { visy[y] = true; if(linker[y] == -1 || DFS(linker[y])) { linker[y] = x; return true; } } else if(slack[y] > tmp) slack[y] = tmp; } return false; } int KM() { memset(linker,-1,sizeof linker); memset(ly,0,sizeof ly); for(int i=0;i<nx;i++) { lx[i] = -INF; for(int j=0;j<ny;j++) lx[i] = max(lx[i],g[i][j]); } for(int x=0;x<nx;x++) { for(int i=0;i<ny;i++) slack[i] = INF; while(true) { memset(visx,false,sizeof visx); memset(visy,false,sizeof visy); if(DFS(x)) break; int d = INF; for(int i=0;i<ny;i++) if(!visy[i] && d > slack[i]) d = slack[i]; for(int i=0;i<nx;i++) if(visx[i]) lx[i] -= d; for(int i=0;i<ny;i++) { if(visy[i]) ly[i] += d; else slack[i] -= d; } } } int res = 0; for(int i=0;i<ny;i++) if(linker[i] != -1) res += g[linker[i]][i]; return res; } int main() { while(~scanf("%d",&N)) { for(int i=0;i<N;i++) { for(int j=0;j<N;j++) { scanf("%d",&g[i][j]); } } nx = ny = N; printf("%d\n",KM()); } }
相关文章推荐
- HDU 2255 奔小康赚大钱(二分图的最大带权匹配 裸的KM算法)
- Hdu2255 奔小康赚大钱(二分图最大权匹配KM算法)
- [ACM] HDU 2255 奔小康赚大钱 (二分图最大权匹配,KM算法)
- [ACM] HDU 2255 奔小康赚大钱 (二分图最大权匹配,KM算法)
- KM算法 最优匹配(最大权匹配) hdu 2255 奔小康赚大钱 最小权匹配 poj 2195 Going Home
- hdu 2255 奔小康赚大钱 (最大权匹配,KM算法)
- HDU-2255 奔小康赚大钱 最大权值匹配
- [HDOJ2255]奔小康赚大钱(二分图最大匹配, KM算法)
- hdoj 2255 奔小康赚大钱 (KM算法 详解+模板) && HDU 1533 Going Home (二分图最小权匹配 KM模板)纯模板
- HDU 2255 奔小康赚大钱 (KM算法+二分图最大权匹配)
- hdu2255 奔小康赚大钱(二部图最大权匹配,KM算法)
- hdu 2255 奔小康赚大钱(完美最大权匹配 KM算法)
- hdu 2255 奔小康赚大钱 (二分图最优匹配,KM算法)
- HDU 2255 KM算法 二分图最大权值匹配
- HDU 2255 奔小康赚大钱 (二分图最大权匹配)
- HDU 2255 奔小康赚大钱(带权二分图最大匹配)
- HDU 2255-奔小康赚大钱(Kuhn-Munkras算法/KM算法-完备匹配下的最大权匹配)
- HDU 2255 奔小康赚大钱(二分匹配之KM算法)
- HDU 2255 奔小康赚大钱(二分图最优匹配:模板题)
- HDU 2255 奔小康赚大钱(二分图最优匹配)