Going Home(最小费用最大流模板)

来源:互联网 发布:win7关闭端口批处理 编辑:程序博客网 时间:2024/05/08 08:38
 Going Home
Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u

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


#include<cstring>#include<iostream>#include<algorithm>#include<cstdio>using namespace std;#define INF 0x3f3f3f3f#define maxn 400000struct node{    int x,y;} A[105],B[105];int n,m;char g[105][105];int fir[maxn],nex[maxn],u[maxn],v[maxn],w[maxn],cap[maxn],flow[maxn];int pre[maxn],vis[maxn],d[maxn],que[2*maxn];int st,ed;int e_max;void init(){    memset(flow,0,sizeof flow);    memset(fir,-1,sizeof fir);    e_max=0;}void add_edge(int s,int t,int c,int cus){    int e=e_max++;    u[e]=s;    v[e]=t;    cap[e]=c;    w[e]=cus;    nex[e]=fir[s];    fir[s]=e;    e=e_max++;    u[e]=t;    v[e]=s;    cap[e]=0;    w[e]=-cus;    nex[e]=fir[t];    fir[t]=e;}bool spfa(){    for(int i=0; i<=ed; i++)    {        vis[i]=0;        d[i]=INF;        pre[i]=-1;    }    d[st]=0;    int f,r;    que[f=r=0]=st;    while(f<=r)    {        int k=que[f++];        vis[k]=0;     //   cout<<k<<endl;        for(int i=fir[k]; ~i; i=nex[i])        {            int e=v[i];            if(flow[i]<cap[i]&&d[k]+w[i]<d[e])            {                pre[e]=i;                d[e]=d[k]+w[i];                if(!vis[e])                {                    vis[e]=1;                    que[++r]=e;                }            }        }    }    if(pre[ed]==-1) return false;    return true;}int solve(){    int ans=0;    while(spfa())    {        int sum=0;        int f=pre[ed];        int minn=INF;        while(f!=-1)        {            if(u[f]!=st&&v[f]!=ed)                minn=min(minn,cap[f]-flow[f]);            f=pre[u[f]];        }        f=pre[ed];        while(f!=-1)        {            sum+=w[f]*minn;            flow[f]+=minn;            flow[1^f]-=minn;            f=pre[u[f]];        }        ans+=sum;    }    return ans;}void build_G(){    int num1=1,num2=1;    for(int i=0; i<n; i++)    {        for(int j=0; j<m; j++)        {            if(g[i][j]=='m')            {                A[num1].x=i;                A[num1++].y=j;            }            else if(g[i][j]=='H')            {                B[num2].x=i;                B[num2++].y=j;            }        }    }    st=0,ed=num1+num2;    for(int i=1; i<num1; i++)    {        add_edge(st,i,1,0);        add_edge(i+num1,ed,1,0);        for(int j=1; j<num2; j++)            add_edge(i,num1+j,1,abs(A[i].x-B[j].x)+abs(A[i].y-B[j].y));    }}int main(){    while(scanf("%d%d",&n,&m)!=EOF)    {        if(!n&&!m) break;        for(int i=0; i<n; i++)            scanf("%s",g[i]);        init();        build_G();        int ans=solve();        printf("%d\n",ans);    }    return 0;}


0 0
原创粉丝点击