/*
题目大意:
给你一个N行M列的矩阵,其中“.”代表空地,“H”代表房子,“m”代表人,其中有n个房子和n个人。现在要求每个人进入一间房子,且人走一步需要支付1美元。
求最小需要花费多少美元才能让所有人都进入到房子中(每个人只能进入一间房子,每个房子只能容纳一个人)。
解题思路:
这道题其实就是二分图最优匹配的变形而已。
因为要求的其实是最小权值之和。而KM算法求的是最大权值之和。
为此,我们可以有两种方法来解决:
1.在建图的时候,将每条边的权值变为负数。然后lx[i]初始化为-INT_MAX,结果输出-ans,就可以得到最小权值。
2.在建图的时候,用一个相对的大数减去每个权值,比如现在边为6,8,10.我用100来减,得到的边为94,92,90.最后输出时,用100*n-ans也可以得到最小的权值。
*/
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int maxn = 105;
const int inf = 1<<30;
int nx,ny,m;
int dis;
int map[maxn][maxn];
char maps[maxn][maxn];
int cx[maxn],cy[maxn];
int lx[maxn],ly[maxn];
bool visx[maxn],visy[maxn];
int Max( int a,int b )
{
return a>=b?a:b;
}
int abs( int x )
{
return x>=0?x:-x;
}
bool findpath(int u) //给x[u]找匹配,这个过程和匈牙利匹配是一样的
{
visx[u] = 1;
for( int v=1; v<=ny; v++ )
{
if( !visy[v] && lx[u] + ly[v] == map[u][v] )
{
visy[v] = 1;
if( cy[v]==-1 || findpath( cy[v] ) )
{
cy[v] = u;
cx[u] = v;
return true;
}
}
}
return false;
}
void updata()
{
int slack = inf;
for( int i = 1; i <= nx; i ++ )
{
if( visx[i] )
{
for( int j = 1; j <= ny; j ++ )
{
if( !visy[j] && slack > lx[i] + ly[j] - map[i][j] )
slack = lx[i] + ly[j] - map[i][j];
}
}
}
for( int i = 1; i <= nx; i ++ )
{
if( visx[i] )
lx[i] -= slack;
if( visy[i] )
ly[i] += slack;
}
}
int KM_perfect_match()
{
int ans = 0;
memset( cx,-1,sizeof(cx) );
memset( cy,-1,sizeof(cy) );
memset( lx,0,sizeof(lx) );
memset( ly,0,sizeof(ly) );
for( int i = 1; i <= nx; i ++ )
{
for( int j = 1; j <= ny; j ++ )
{
lx[i] = Max( lx[i],map[i][j] ); //lx[i]为i点权值最大的边
}
//if( lx[i] == -1 )
// return -1; //无法匹配
}
for( int k = 1; k <= nx; k ++ ) //为x里的每一个点找匹配 不断修改顶标,直到找到完备匹配或完美匹配
{
while(1)
{
memset( visx,0,sizeof(visx) );
memset( visy,0,sizeof(visy) );
if( cx[k] == -1 && findpath(k) )
break;
else //匹配失败,找最小值
{
updata();
}
}
}
for( int i = 1; i <= nx; i ++ ) //权值相加
ans += map[cy[i]][i];
return ans;
}
int main()
{
int row,col,x,y;
while( scanf("%d%d",&row,&col)==2 ,( row||col ) )
{
nx = 1;
ny = 0;
memset( map,0,sizeof(map) );
for( int i = 1; i <= row; i++ )
{
getchar();
for( int j = 1; j <= col; j++ )
{
scanf("%c",&maps[i][j] );
//if( map[i][j] == 'm' )
// nx++;
}
}
for( int i = 1; i <= row; i++ )
{
for( int j = 1; j <= col; j++ )
{
if( maps[i][j] == 'm' )
{
ny = 0;
for( int k = 1; k <= row; k++ )
{
for( int l = 1; l <= col; l++ )
{
if( maps[k][l] == 'H')
{
map[nx][++ny] = 100 - abs( k - i ) - abs( l -j );
}
}
}
nx++;
}
}
}
nx--;
printf("%d\n",100*nx-KM_perfect_match() );
}
}
网络流:最小费用最大流
#include "stdio.h"
#include "queue"
#include "string.h"
#include "vector"
#include "algorithm"
using namespace std;
const int maxn = 310;
const int inf = 1<<30;
int n,row,col,nx,ny,k;
int cap[maxn][maxn],flow[maxn][maxn],cost[maxn][maxn];
char maps[maxn][maxn];
int dis[maxn],inq[maxn],path[maxn*maxn];
bool Bellman_Ford( int st,int end )
{
queue<int>que;
memset(inq,0,sizeof(inq));
for( int i = 1; i <= n; i ++ )
dis[i] = inf;
dis[st] = 0;
que.push( st );
while( !que.empty() )
{
int u = que.front(); que.pop();
inq[u] = false;
for( int v = 2; v <= n; v ++ ){
if( cap[u][v] > flow[u][v] && dis[v] > dis[u] + cost[u][v] ){
dis[v] = dis[u] + cost[u][v];
path[v] = u;
if( !inq[v] )
{
inq[v] = true;
que.push(v);
}
}
}
}
return dis[n] != inf;
}
int Min_Cost_Flow( int st,int end )
{
int ans_Flow = 0, ans_Cost = 0;
int mn;
while( Bellman_Ford( st,end ) )
{
mn = inf;
for( int i = end; i != st; i = path[i] )
mn = mn < cap[path[i]][i]-flow[path[i]][i]?mn:cap[path[i]][i]-flow[path[i]][i];
for( int i = end; i != st; i = path[i] )
{
flow[path[i]][i] += mn;
flow[i][path[i]] -= mn;
}
ans_Cost += dis[end]*mn;
ans_Flow += mn;
}
return ans_Cost;
}
int main()
{
//freopen("data.txt","r",stdin);
while( scanf("%d%d",&row,&col) == 2,( row||col ) ){
int sum = 0;
memset(cap,0,sizeof(cap));
memset(flow,0,sizeof(flow));
for( int i = 1; i <= row; i ++ )
{
getchar();
for( int j = 1; j <= col; j ++ )
{
scanf("%c",&maps[i][j]);
cost[i][j] = inf;
if( maps[i][j] == 'm' )
sum ++;
}
}
nx = 1;
for( int i = 1; i <= row; i ++ )
{
for( int j = 1; j <= col; j ++ )
{
if( maps[i][j] == 'm' )
{
ny = sum+1;
nx ++;
for( int k = 1; k <= row; k ++ )
{
for( int l = 1; l <= col; l ++ )
{
if( maps[k][l] == 'H' )
{
++ny;
cost[nx][ny] = abs( k - i ) + abs( l -j );
cost[ny][nx] = -cost[nx][ny];
cap[nx][ny] = 1;
}
}
}
}
}
}
n = nx+ny+2;
for( int i = 2; i <= nx; i ++ )
{
cap[1][i] = 1;
cost[1][i] = cost[i][1] = 0;
}
for( int i = nx+1; i < n; i ++ )
{
cap[i][n] = 1;
cost[i][n] = cost[n][i] = 0;
}
printf("%d\n", Min_Cost_Flow(1,n));
}
return 0;
}