poj 2195 - Going Home 二分图最优匹配 ek

来源:互联网 发布:js 设置div不可用 编辑:程序博客网 时间:2024/05/03 21:55

I - Going Home
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status

Description

On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, until he enters a house. The task is complicated with the restriction that each house can accommodate only one little man. 

Your task is to compute the minimum amount of money you need to pay in order to send these n little men into those n different houses. The input is a map of the scenario, a '.' means an empty space, an 'H' represents a house on that point, and am 'm' indicates there is a little man on that point. 

You can think of each point on the grid map as a quite large square, so it can hold n little men at the same time; also, it is okay if a little man steps on a grid with a house without entering that house.

Input

There are one or more test cases in the input. Each case starts with a line giving two integers N and M, where N is the number of rows of the map, and M is the number of columns. The rest of the input will be N lines describing the map. You may assume both N and M are between 2 and 100, inclusive. There will be the same number of 'H's and 'm's on the map; and there will be at most 100 houses. Input will terminate with 0 0 for N and M.

Output

For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.

Sample Input

2 2.mH.5 5HH..m...............mm..H7 8...H.......H.......H....mmmHmmmm...H.......H.......H....0 0

Sample Output

21028

在一个地图上给出房子的位置和人的位置,人和房子的数量是相等的。人要回到房子里,每个房子只能回一个人。人向房子每移动一个单位需要花费$1,求人全部回房子的最小花费。

ACcode:

#pragma warning(disable:4786)//使命名长度不受限制#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈#include <map>#include <set>#include <queue>#include <cmath>#include <stack>#include <cctype>#include <cstdio>#include <cstring>#include <stdlib.h>#include <iostream>#include <algorithm>#define rd(x) scanf("%d",&x)#define rd2(x,y) scanf("%d%d",&x,&y)#define rds(x) scanf("%s",x)#define rdc(x) scanf("%c",&x)#define ll long long int#define maxn 105#define mod 1000000007#define INF 0x3f3f3f3f //int 最大值#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)#define MT(x,i) memset(x,i,sizeof(x))#define PI  acos(-1.0)#define E  exp(1)using namespace std;struct P{int x,y;};P man[maxn],house[maxn];int mm,hh,n,m,res;int a[maxn],b[maxn],match[maxn],slack[maxn],mp[maxn][maxn];bool visa[maxn],visb[maxn];char str[maxn];bool find_path(int u){    visa[u]=true;    FOR(j,0,hh-1){        if(!visb[j]&&a[u]+b[j]==mp[u][j]){            visb[j]=true;            if(match[j]==-1||find_path(match[j])){                match[j]=u;                return true;            }        }        else if(a[u]+b[j]>mp[u][j])            slack[j]=min(slack[j],a[u]+b[j]-mp[u][j]);    }    return false;}void km(){    MT(a,0);MT(b,0);MT(match,-1);    FOR(i,0,mm-1)        FOR(j,0,hh-1)            a[i]=max(mp[i][j],a[i]);    FOR(i,0,mm-1){        FOR(j,0,hh-1)slack[j]=INF;        while(true){            FOR(j,0,maxn){visa[j]=visb[j]=0;}            if(find_path(i))break;            int d=INF;            FOR(j,0,hh-1)                if(!visb[j]&&d>slack[j])d=slack[j];            FOR(j,0,mm-1)                if(visa[j])a[j]-=d;            FOR(j,0,hh-1)                if(visb[j])b[j]+=d;                else slack[j]-=d;        }    }}int main(){    while(rd2(n,m)&&(n+m)){        hh=mm=res=0;        FOR(i,0,n-1){            rds(str);            FOR(j,0,m-1){                if(str[j]=='H'){                    house[hh].x=i;                    house[hh++].y=j;                }else if(str[j]=='m'){                    man[mm].x=i;                    man[mm++].y=j;                }            }        }        FOR(i,0,mm-1)            FOR(j,0,hh-1)                mp[i][j]=-(abs(man[i].x-house[j].x)+abs(man[i].y-house[j].y));        km();        FOR(j,0,hh-1)            res-=mp[match[j]][j];        printf("%d\n",res);    }    return 0;}/*2 2.mH.5 5HH..m...............mm..H7 8...H.......H.......H....mmmHmmmm...H.......H.......H....0 0*/


0 0