hdu 1533 Going Home(最小费用最大流)

来源:互联网 发布:高中教学视频 淘宝网 编辑:程序博客网 时间:2024/04/28 23:48

Going Home

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


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的矩阵,.代表空地,H代表房子,m代表人,m的数量和H相同,H最多不超过100
现在问所有的人去房子里最少要多久,走一步算一个单位时间
思路:很容易看出是个二分带权匹配问题,这里练习最小费用,所以用网络流来解。
构图很简单,构建超级源点和超级汇点,所有的容量都为1,m与H之间的费用为两者间的距离,跑一遍MCMF即可
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <queue>using namespace std;#define N 410#define INF 99999999struct Node{    int x,y;} p[N],q[N];struct Edge{    int u,v,cap,cost;    int next;} edge[N*N];int cnt,head[N],vis[N],pp[N],d[N];int n,m,cn,cm;char ma[N][N];void init(){    cnt=cn=cm=0;    memset(head,-1,sizeof(head));}void addedge(int u,int v,int cost,int cap){    edge[cnt].u=u,edge[cnt].v=v;    edge[cnt].cost=cost,edge[cnt].cap=cap;    edge[cnt].next=head[u];    head[u]=cnt++;    edge[cnt].u=v,edge[cnt].v=u;    edge[cnt].cost=-cost,edge[cnt].cap=0;    edge[cnt].next=head[v];    head[v]=cnt++;}int dist(Node a,Node b){    return abs(b.x-a.x)+abs(b.y-a.y);}int spfa(int s,int t,int n){    memset(pp,-1,sizeof(pp));    memset(vis,0,sizeof(vis));    for(int i=0; i<=n; i++)        d[i]=INF;    d[s]=0;    queue<int>que;    vis[s]=1;    que.push(s);    while(!que.empty())    {        int u=que.front();        que.pop();        vis[u]=0;        for(int i=head[u]; i!=-1; i=edge[i].next)        {            int v=edge[i].v;            if(edge[i].cap>0&&d[v]>d[u]+edge[i].cost)            {                d[v]=d[u]+edge[i].cost;                pp[v]=i;                if(!vis[v])                {                    vis[v]=1;                    que.push(v);                }            }        }    }    if(d[t]==INF) return 0;    return 1;}int MCMF(int s,int t,int n){    int mincost=0,minflow;    while(spfa(s,t,n))    {        minflow=INF+1;        for(int i=pp[t]; i!=-1; i=pp[edge[i].u])            minflow=min(minflow,edge[i].cap);        for(int i=pp[t]; i!=-1; i=pp[edge[i].u])        {            edge[i].cap-=minflow;            edge[i^1].cap+=minflow;        }        mincost+=minflow*d[t];    }    return mincost;}int main(){    while(~scanf("%d %d",&n,&m)&&(n+m))    {        init();        for(int i=0; i<n; i++)            scanf("%s",ma[i]);        for(int i=0; i<n; i++)            for(int j=0; j<m; j++)                {                    if(ma[i][j]=='H')                    p[++cn].x=i+1,p[cn].y=j+1;                    else if(ma[i][j]=='m')                    q[++cm].x=i+1,q[cm].y=j+1;                }        for(int i=1; i<=cm; i++)            for(int j=1; j<=cn; j++)                        addedge(i,j+cm,dist(q[i],p[j]),1);        int s=0,t=2*cn+1;        for(int i=1; i<=cm; i++)            addedge(0,i,0,1);        for(int i=1; i<=cn; i++)            addedge(i+cm,t,0,1);        printf("%d\n",MCMF(s,t,t));    }    return 0;}





0 0
原创粉丝点击