sicily 1303 最大权 km
2010-11-15 01:03
363 查看
//这种题编过就变水了 #include <iostream> #include <cstring> #include <cstdio> #define Mx 101; using namespace std; int n,result,d; int w[101][101],kmx[101],kmy[101]; bool _usex[101],_usey[101]; int linky[101]; bool find( int i ) { _usex[i] = true; for( int j = 1;j <= n;j++ ) { if( _usey[j] ) continue; int t = kmx[i] + kmy[j] - w[i][j]; if( t == 0 ) { _usey[j] = true; if( !linky[j] || find( linky[j] ) ) { linky[j] = i; return true; } } else if( d > t ) d = t; } return false; } void KM() { for( int i = 1;i <= n;i++ ) { for( ; ; ) { memset( _usex,false,sizeof(_usex) ); memset( _usey,false,sizeof(_usey) ); d = Mx; if( find( i ) ) break; for( int j = 1;j <= n;j++ ) { if( _usex[j] ) kmx[j] -= d; if( _usey[j] ) kmy[j] += d; } } } } int main() { //freopen( "1.txt","r",stdin ); while( scanf("%d",&n) && 0 != n ) { result = 0; memset( linky,0,sizeof( linky ) ); memset( kmx,0,sizeof( kmx ) ); memset( kmy,0,sizeof( kmy ) ); for( int i = 1;i <= n;i++ ) for( int j = 1;j <= n;j++ ) { scanf( "%d",&w[i][j] ); if( kmx[i] < w[i][j] ) kmx[i] = w[i][j]; } KM(); for( int i = 1;i <= n;i++ ) result += ( kmx[i] + kmy[i] ); printf( "%d/n",result ); } return 0; }
相关文章推荐
- sicily 1303 Job Assigment(二分图最大权匹配)
- HDU 3523 Image copy detection(KM最大匹配)
- HDU-2255(记录km算法的板子,km求最大权,最小权匹配)
- HDU2255 KM完备最大匹配
- HDU 2853 Assignment(KM最大匹配好题)
- hdu3722Card Game(KM最大带权匹配)
- URAL 1076 Trash 【最大权匹配KM快速模板O(N^3)】
- ★ HDU 2853 求KM最大时,要求改动最少
- HDU 3523 Image copy detection(KM最大匹配)
- HDU3315 My Brute (求KM最大时,要求改动最少★★)
- 【Sicily】1005. 最大和
- sicily 6418. 最大公约数
- URAL 1076 Trash 【最大权匹配KM快速模板O(N^3)】
- Assignment (HDU 2853 最大权匹配KM)
- 二分图最大匹配(匈牙利KM)
- hdu3722Card Game(KM最大带权匹配)
- ★ HDU 3315 求KM最大时,要求改动最少
- HDU3718Similarity(KM最大匹配)
- 【Sicily】1005. 最大和
- HDU 2853 Assignment(KM最大匹配好题)