HDU1533-KM

来源:互联网 发布:跳水 知乎 编辑:程序博客网 时间:2024/04/29 19:03

Going Home

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4896    Accepted Submission(s): 2572


Problem 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
 

题目大意:

                  给你一个n*m的地图,有人和房子,每个人到房子的代价是曼哈顿距离即xy轴距离差的绝对值的和,问你所有人都住到房子里的总的代价最少是对少,每个房子只能住一个人


题目思路:

                更具地图建一个人和房子的二分图权值为曼哈顿距离,然后就转换成了二分图带权问题,然后就是跑一遍KM算法就是这里要求的是最短所有我们可以把权值取反,求出来的答案取反就是我们要求的最小值


AC代码:


#include<cstring>#include<cstdio>#define min(x,y) (x<y?x:y)#define max(x,y) (x>y?x:y)#define abs(x)  ((x)<0?-(x):(x))const int maxn = 1e2+20;const int inf = 0x3f3f3f3f;class KM{public:    int n,m,numh,numm;    int ph[maxn][2],pm[maxn][2];    int mp[maxn][maxn],slack[maxn],link[maxn],ex_l[maxn],ex_r[maxn];    bool vis_l[maxn],vis_r[maxn];    bool dfs(int u){        vis_l[u]=true;        for(int i=1;i<=n;i++){            if(!vis_r[i]){                if(ex_l[u]+ex_r[i]==mp[u][i]){                    vis_r[i]=true;                    if(link[i]==-1||dfs(link[i])){                        link[i]=u;                        return true;                    }                }else {                    slack[i]=min(slack[i],ex_l[u]+ex_r[i]-mp[u][i]);                }            }        }        return false;    }    int sove(){        for(int i=1;i<=n;i++){            memset(slack,0x3f,sizeof(slack));            while(1){                memset(vis_l,false,sizeof(vis_l));                memset(vis_r,false,sizeof(vis_r));                if(dfs(i))break;                int d = inf;                for(int j=1;j<=n;j++){                    if(!vis_r[j])d=min(d,slack[j]);                }                for(int j=1;j<=n;j++){                    if(vis_l[j])ex_l[j]-=d;                    if(vis_r[j])ex_r[j]+=d;                    else slack[j]-=d;                }            }        }        int res = 0;        for(int i=1;i<=n;i++){            if(link[i]!=-1){                res+=mp[link[i]][i];            }        }        return res;    }    void star(){        while(~scanf("%d%d",&n,&m),n+m){            init();            char str[maxn];            for(int i=1;i<=n;i++){                scanf("%s",str+1);                for(int j=1;j<=m;j++){                    if(str[j]=='H')ph[++numh][0]=i,ph[numh][1]=j;                    if(str[j]=='m')pm[++numm][0]=i,pm[numm][1]=j;                }            }            n=numh;            for(int i=1;i<=n;i++){                for(int j=1;j<=n;j++){                    int x = abs(pm[i][0]-ph[j][0])+abs(pm[i][1]-ph[j][1]);                    mp[i][j]=-x;                    ex_l[i]=max(ex_l[i],mp[i][j]);                }            }            printf("%d\n",-sove());        }    }    void init(){        memset(link,-1,sizeof(link));        memset(ex_r,0,sizeof(ex_r));        memset(ex_l,-0x3f,sizeof(ex_l));        numh=numm=0;    }}km;int main(){    km.star();    return 0;}






0 0