E - Going Home POJ - 2195 (最小费用最大流)

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
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0

Sample Output

2
10
28

#include<cstdio>
#include<stack>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<iostream>
#include<cmath>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
const int N=444;
const int mmax = 200000+ 7;
int s,t,n,m;
int preb[44444],head[N],vis[N],dis[N];
struct nod
{
    int x,y;
} ma[111],ho[111];
char str[111][111];
struct node
{
    int v,w,co,next;
} gra[mmax];
int num;
void ad(int u,int v,int w,int co)
{
    gra[num].v=v;
    gra[num].next=head[u];
    gra[num].w=w;
    gra[num].co=co;
    head[u]=num++;
    gra[num].next=head[v];
    gra[num].v=u;
    gra[num].w=0;
    gra[num].co=-co;
    head[v]=num++;
}
int spfa(){    //判断最小费用的一条增广路径存在与否
    memset(dis,inf,sizeof(dis));
    memset(preb,-1,sizeof(preb));
    queue<int>q;
    while(!q.empty())q.pop();
    q.push(s);
    dis[s]=0;
    memset(vis,0,sizeof(vis));//vis用来记录是否已在队列里
    vis[0]=1;
    while(!q.empty()){  
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=gra[i].next){
            if(gra[i].w>0&&dis[u]+gra[i].co<dis[gra[i].v]){
                dis[gra[i].v]=dis[u]+gra[i].co;
                preb[gra[i].v]=i;   
                if(!vis[gra[i].v]){   //权值有松弛且不在队列中
                    vis[gra[i].v]=1;
                    q.push(gra[i].v);
                }
            }
        }
    }
    if(dis[t]!=inf)
        return true;
    else
        return false;
}
int solve(){
int ans=0;
while(spfa()){
   int minf=inf;
    for(int i=preb[t];i!=-1;i=preb[gra[i^1].v]){
       minf=min(minf,gra[i].w);  //求增广路径的流量
    }
    for(int i=preb[t];i!=-1;i=preb[gra[i^1].v]){   //更新正反边的流量
        gra[i].w-=minf;
        gra[i^1].w+=minf;
    }
    ans+=dis[t]*minf;
}
return ans;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0)
            break;
        int msum=0;
        int  hsum=0;
        num=0;
        memset(head,-1,sizeof(head));
        for(int i=1; i<=n; i++)
        {
            scanf("%s",str[i]+1);
            for(int j=1; j<=m; j++)
            {
                if(str[i][j]=='m')
                {
                    ma[msum].x=i;
                    ma[msum++].y=j;
                }
                else if(str[i][j]=='H')
                {
                    ho[hsum].x=i;
                    ho[hsum++].y=j;
                }
            }
/*        
建图:以s=0为超源点,1~msum为men的结点,s到men的权值为0,流量为1,
msum+1~msum+hsum为house的结点,mem的结点到house的结点的权值为两个
结点的路程距离,流量为1,house的结点到汇点msum+hsum+1的权值为0,
流量为1
*/
        for(int i=0; i<msum; i++)
        {
            ad(0,i+1,1,0);
            ad(msum+i+1,msum*2+1,1,0);
            for(int j=0; j<hsum; j++)
            {
                int d=abs(ma[i].x-ho[j].x)+abs(ma[i].y-ho[j].y);
                ad(i+1,msum+j+1,1,d);
            }
        }
        s=0,t=msum+hsum+1;
        printf("%d\n",solve());
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/clz16251102113/article/details/81430024