最小费用最大流模板题

http://acm.hdu.edu.cn/showproblem.php?pid=1533

Going Home

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


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

Sample Output
  
  
2 10 28
题意:

对于图中的x个人和x个房子,要求用最小的时间让每个人都进入房内,人到房间的距离定义为曼哈顿距离

算法:最小费用流,让源点和人建边,流量为1,费用为0,让房间和汇点建边,流量为1,费用为0,让人和房子两两建边,流量为inf,费用为曼哈顿距离;

程序:

#include"string.h"
#include"stdio.h"
#include"iostream"
#include"queue"
#define M 10009
#define inf 999999999
using namespace std;
struct node
{
    int x,y;
}house[M],men[M];
struct st
{
    int u,v,w,cost,next;
}edge[M*10];
int head[M],t,use[M],dis[M],pre[M];
int Fabs(int x)
{
    if(x<0)
        x=-x;
    return x;
}
void init()
{
    t=0;
    memset(head,-1,sizeof(head));
}
void add(int u,int v,int w,int cost)
{
    edge[t].u=u;
    edge[t].v=v;
    edge[t].w=w;
    edge[t].cost=cost;
    edge[t].next=head[u];
    head[u]=t++;

    edge[t].u=v;
    edge[t].v=u;
    edge[t].w=0;
    edge[t].cost=-cost;
    edge[t].next=head[v];
    head[v]=t++;
}
int min_flow(int S,int T)
{
    int ans=0;
    while(1)
    {
        int i;
        queue<int>q;
        for(i=0;i<=T;i++)
            dis[i]=inf;
        dis[S]=0;
        memset(use,0,sizeof(use));
        memset(pre,-1,sizeof(pre));
        q.push(S);
        while(!q.empty())
        {
            int u=q.front();
            use[u]=0;
            q.pop();
            for(i=head[u];i!=-1;i=edge[i].next)
            {
                int v=edge[i].v;
                if(edge[i].w&&dis[v]>dis[u]+edge[i].cost)
                {
                    dis[v]=dis[u]+edge[i].cost;
                    pre[v]=i;
                    if(!use[v])
                    {
                        use[v]=1;
                        q.push(v);
                    }
                }
            }
        }
        if(dis[T]==inf)
            break;
        int Min=inf+1;
        for(i=pre[T];i!=-1;i=pre[edge[i].u])
        {
            Min=min(Min,edge[i].w);
        }
        for(i=pre[T];i!=-1;i=pre[edge[i].u])
        {
            edge[i].w-=Min;
            edge[i^1].w+=Min;
        }
        ans+=Min*dis[T];
    }
    return ans;
}
int main()
{
    int n,m,i,j;
    char mp[111][111];
    while(scanf("%d%d",&n,&m),m||n)
    {
        int hh=0,mm=0;
        for(i=0;i<n;i++)
        {
            scanf("%s",mp[i]);
            for(j=0;j<m;j++)
            {
                if(mp[i][j]=='m')
                {
                    mm++;
                    men[mm].x=i;
                    men[mm].y=j;
                }
                if(mp[i][j]=='H')
                {
                    hh++;
                    house[hh].x=i;
                    house[hh].y=j;
                }
            }
        }
        init();
        for(i=1;i<=mm;i++)
            add(0,i,1,0);
        for(i=1;i<=hh;i++)
            add(i+mm,hh+mm+1,1,0);
        for(i=1;i<=mm;i++)
        {
            for(j=1;j<=hh;j++)
            {
                add(i,mm+j,inf,Fabs(men[i].x-house[j].x)+Fabs(men[i].y-house[j].y));
            }
        }
        int ans=min_flow(0,mm+hh+1);
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值