POJ2195 Going Home

来源:互联网 发布:java 二分法排序代码 编辑:程序博客网 时间:2024/05/20 05:09

Going Home
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 21926 Accepted: 11090

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个小人和n个房子,在每个单位时间内,每个小人可以往水平方向或者竖直方向移动一步,走到相邻的方格中。对于每个小人,每走一步的花费是1美元,直到自己走到一栋房子中。

现在是要让你求所有的小人全部到达房子的最小花费。

输入:r,c代表图有r行c列,其中m代表的是小人h代表的是房子。

思路:最小花费问题,因为它每次搜最短路不一定能得到最优解。可以构建成一个最小花费最大流模型,需要一下反向弧。

1.超级源点连接每个小人,花费为0,流量为1。

2.图中的点4个方向不出边界的点连一条边,花费为1,流量为无穷大。

3.每个房子连接超级汇点,花费为0,流量为1。

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <queue>using namespace std;const int MAXN=10000+10;const int inf=1e9;int n,m;int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};char tu[110][110];int s,e;int cnt,head[MAXN];struct node{    int u,v,w,f;    int next;}edge[50000];void build(){    cnt=0;    for(int i=0;i<=e;++i)head[i]=-1;}void add(int u,int v,int w,int f){    edge[cnt].u=u;    edge[cnt].v=v;    edge[cnt].w=w;    edge[cnt].f=f;    edge[cnt].next=head[u];    head[u]=cnt++;    edge[cnt].u=v;    edge[cnt].v=u;    edge[cnt].w=-w;    edge[cnt].f=0;    edge[cnt].next=head[v];    head[v]=cnt++;}bool vis[MAXN];int pre[MAXN],dis[MAXN];bool spfa(){    int i;    queue<int>q;    for(i=0;i<=e;++i)    {        pre[i]=-1;        dis[i]=inf;        vis[i]=0;    }    q.push(s);    dis[s]=0;    while(!q.empty())    {        int u=q.front();        q.pop();        vis[u]=0;        for(i=head[u];i!=-1;i=edge[i].next)        {            int v=edge[i].v;            int w=edge[i].w;            int f=edge[i].f;            int t=dis[u]+w;            if(f>0&&dis[v]>t)            {                dis[v]=t;                pre[v]=i;                if(!vis[v])                {                    vis[v]=1;                    q.push(v);                }            }        }    }    if(pre[e]==-1)return 0;    return 1;}int get_mincost(){    int ans=0;    int flow=inf;    while(spfa())    {        int p=pre[e];        while(p!=-1)        {            flow=min(flow,edge[p].f);            p=pre[edge[p].u];        }        p=pre[e];        while(p!=-1)        {            edge[p].f-=flow;            edge[p^1].f+=flow;            ans+=flow*edge[p].w;            p=pre[edge[p].u];        }    }    return ans;}int main(){    int i,j,k;    while(~scanf("%d%d",&n,&m))    {        if(!n&&!m)break;        s=n*m;        e=s+1;        for(i=0;i<n;++i)scanf("%s",tu[i]);        build();        for(i=0;i<n;++i)            for(j=0;j<m;++j)        {            int u=i*m+j;            for(k=0;k<4;++k)            {                int nx=i+dx[k];                int ny=j+dy[k];                if(nx>=0&&nx<n&&ny>=0&&ny<m)                {                    int v=nx*m+ny;                    add(u,v,1,inf);                }            }            if(tu[i][j]=='m')            {                add(s,u,0,1);            }            if(tu[i][j]=='H')            {                add(u,e,0,1);            }        }        printf("%d\n",get_mincost());    }    return 0;}




1 0
原创粉丝点击