1. 程式人生 > >Going Home(網路流 最小費用最大流)

Going Home(網路流 最小費用最大流)

Going Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 22035 Accepted: 11139

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

Sample Output

2
10
28
//第一次網路流,恕我只能參考網上程式碼http://blog.csdn.net/wangjian8006/article/details/7940499

題目大意:圖中有n個man和n個home,並且一個人只能住在一個房子裡面,房子和人的個數是相等的。並且每個人移動一步的代價是1,怎麼使 所有人住在房子裡,並且使所有人的代價和最小。 給出一個n×m的圖,m表示人,H表示房子,.表示空地,當然房子不算障礙物,可以穿過

解題思路:這是一個費用流的應用,構圖如下:

建一個源點指向所有人,容量為1,代價為0 使每個人指向所有的房子,容量為1,代價為人與房子的曼哈頓距離 建一個匯點使所有房子指向它,容量為1,代價為0

然後從源點到匯點求費用流即可。

題目要注意的是,在n×m的這張圖上說人的總數不超過100,那麼圖中節點總數為人+房子+源點+匯點=202

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<cmath>
#include<queue>
using namespace std;
#define maxn 210
#define INF INT_MAX
struct point
{
    int x,y;
}man[maxn],home[maxn];
int man_sum,home_sum,mincost;
int cost[maxn][maxn],res[maxn][maxn],souce,sink;
int d[maxn],p[maxn];
void spfa()
{
    queue<int>q;
    int v,i;
    bool vis[maxn];
    memset(vis,0,sizeof(vis));
    memset(p,-1,sizeof(p));

    for(i=souce;i<=sink;i++)
        d[i]=INF;
    d[souce]=0;
    q.push(souce);
    vis[souce]=1;
    while(!q.empty())
    {
         v=q.front();
        q.pop();
        vis[v]=0;
        for( i=souce;i<=sink;i++)
        {
            if(res[v][i] && d[v]+cost[v][i]<d[i])
            {
                d[i]=d[v]+cost[v][i];
                p[i]=v;
                if(!vis[i])
                {
                    q.push(i);
                    vis[i]=1;
                }
            }
        }
    }
}

void MCMF()
{
    int v;
    mincost=0;
    while(1)
    {
        spfa();
        if(p[sink]==-1) break;
         v=sink;
        while(p[v]!=-1)
        {
            res[p[v]][v]-=1;
            res[v][p[v]]+=1;
            v=p[v];
        }
        //printf("---%d\n",d[sink]);
        mincost+=d[sink];
    }
}
int main()
{

    int n,m,i,j;
    char s[maxn];
    while(~scanf("%d%d",&n,&m),(n+m))
    {

        home_sum=0;man_sum=0;
        for(i=1;i<=n;i++)
        {
            scanf("%s",s);
            for(j=0;j<m;j++)
            {
                if(s[j] == 'm')
                {
                    man[man_sum].x=i;
                    man[man_sum++].y=j+1;
                }
                if(s[j]=='H')
                {
                    home[home_sum].x=i;
                    home[home_sum++].y=j+1;
                }
            }
        }
        //printf("%d  %d\n",man_sum,home_sum);
        memset(cost,0,sizeof(cost));
        memset(res,0,sizeof(res));
        souce=0;sink=home_sum+man_sum+1;
        for(i=0;i<man_sum;i++) res[souce][i+1]=1;//源點指向人  

        for(i=0;i<man_sum;i++)   //人指向房子  
        {
            for(j=0;j<home_sum;j++)
            {
                res[i+1][man_sum+j+1]=1;
                cost[i+1][man_sum+j+1]=abs(man[i].x-home[j].x)+abs(man[i].y-home[j].y);
                cost[man_sum+j+1][i+1]=-cost[i+1][man_sum+j+1];
            }
        }

        for(i=0;i<home_sum;i++)//房子指向匯點  
        {
            res[man_sum+i+1][sink]=1;
        }
        MCMF();
        printf("%d\n",mincost);
    }
}