标签:
Description
Input
Output
Sample Input
Sample Output
#include<stdio.h> #include<algorithm> #include<string.h> #include<vector> using namespace std; const int maxn = 550; const int INF = 0x3f3f3f3f; int nx, ny, lx[maxn], ly[maxn]; bool visx[maxn], visy[maxn]; int linker[maxn], slack[maxn]; int G[maxn][maxn]; bool dfs(int x){ visx[x] = 1; //标记X的匈牙利树节点 for(int y = 1; y <= ny; y++){ if(visy[y]) continue; int tmp = lx[x] + ly[y] - G[x][y]; if(tmp == 0){ //找到了一条可以加入相等子图的新边 visy[y] = 1; //标记Y的匈牙利树节点 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)); //初始化Y节点顶标 for(int i = 1; i <= nx; i++){ lx[i] = -INF; for(int j = 1; j <= ny; j++){ if(lx[i] < G[i][j]){ lx[i] = G[i][j]; //初始化X节点顶标 } } } for(int x = 1; x <= nx; x++){ //找每个X的增广路 for(int i = 1; i <= ny; i++){ //初始化松弛量 slack[i] = INF; } while(true){ memset(visx,0,sizeof(visx)); memset(visy,0,sizeof(visy)); if(dfs(x)) break; //找到了增广路 int d = INF; for(int i = 1; i <= ny; i++){ if(!visy[i] && d > slack[i]){ //Yi不在匈牙利树(交错树)中 d = slack[i]; } } for(int i = 1; i <= nx; i++){ //Xi在匈牙利树中,更新顶标 if(visx[i]){ lx[i] -= d; } } for(int i = 1; i <= ny; i++){ if(visy[i]){ //Yi在匈牙利树中,更新顶标 ly[i] += d; }else{ //更新松弛量 slack[i] -= d; } } } } int ret = 0; for(int i = 1; i <= ny; i++){ //求和 if(linker[i] != -1){ ret += G[linker[i]][i]; } } return ret; } int main(){ int n; while(scanf("%d",&n)!=EOF){ memset(G,0,sizeof(G)); int c; for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ scanf("%d",&c); G[i][j] = c; } } nx = ny = n; int res = KM(); printf("%d\n",res); } return 0; }
HDU 2255 ——奔小康赚大钱——————【KM算法裸题】
标签:
原文地址:http://www.cnblogs.com/chengsheng/p/4964051.html