hdoj 1533 Going Home(EK增强版求最小费用最大流)

Going Home

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
 

Source
Pacific Northwest 2004

最小费用最大流
最小费用最大流算法其实就是EK增强版,就是把EK中的bfs()找增广路变成spfa()找cost最小的增广路
虽然这题大部分题解是二分图匹配,但还是先学下网络流

#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;
#define M 40050
#define N 205
#define inf 0x3f3f3f3f
int n,m,cnt,num;
int head[N],pre[N],preedge[N],vis[N],dist[N];
struct node
{
    int u,v,f,cost,next;
}edge[M];
void add(int u,int v,int f,int cost)
{

    edge[cnt].u=u;
    edge[cnt].v=v;
    edge[cnt].f=f;
    edge[cnt].cost=cost;
    edge[cnt].next=head[u];
    head[u]=cnt++;

    edge[cnt].u=v;
    edge[cnt].v=u;
    edge[cnt].f=0;
    edge[cnt].cost=-cost;
    edge[cnt].next=head[v];
    head[v]=cnt++;
};
int spfa(int s,int t)//寻找代价最小的增广路
{
    int i,now,u,v,cost;
    for(i=0;i<num;i++)
    {
        vis[i]=0;
        dist[i]=inf;
        pre[i]=-1;
    }
    queue<int>q;
    q.push(s);
    dist[s]=0;
    vis[s]=1;
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        vis[now]=0;
        for(i=head[now];i!=-1;i=edge[i].next)
        {
            u=edge[i].u;
            v=edge[i].v;
            cost=edge[i].cost;
            if(edge[i].f>0&&dist[v]>dist[u]+cost)
            {
                dist[v]=dist[u]+cost;
                pre[v]=u;
                preedge[v]=i;
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    return dist[t]<inf;
}
int mincost(int s,int t)
{
    int tmp,flow,res,i;
    tmp=inf;
    flow=0;
    res=0;
    while(spfa(s,t))//每次增广代价最小的增广路
    {
        for(i=t;i!=s;i=pre[i])
        {
            if(tmp>edge[preedge[i]].f)
                tmp=edge[preedge[i]].f;
        }
        for(i=t;i!=s;i=pre[i])
        {
            edge[preedge[i]].f-=tmp;
            edge[preedge[i]^1].f+=tmp;
        }
        flow+=tmp;
        res+=tmp*dist[t];
    }
    return res;
}
int main()
{
    int cnt1,cnt2,i,j,hx[N],hy[N],mx[N],my[N];
    char s[N];
    while(scanf("%d%d",&n,&m)&&n+m)
    {
        cnt1=cnt2=0;
        for(i=1;i<=n;i++)
        {
            scanf("%s",s);
            for(j=0;j<m;j++)
            {
                if(s[j]=='H')
                {
                    hx[++cnt1]=i;
                    hy[cnt1]=j+1;
                }
                else if(s[j]=='m')
                {
                    mx[++cnt2]=i;
                    my[cnt2]=j+1;
                }
            }
        }
        num=cnt1*2+2;
        cnt=0;
        memset(head,-1,sizeof(head));
        for(i=1;i<=cnt1;i++)
        {
            for(j=1;j<=cnt2;j++)
            {
                int cost=abs(hx[j]-mx[i])+abs(hy[j]-my[i]);
                add(i,cnt1+j,1,cost);
            }
        }
        for(i=1;i<=cnt1;i++)
        {
            add(0,i,1,0);
            add(i+cnt1,2*cnt1+1,1,0);
        }
        printf("%d\n",mincost(0,2*cnt1+1));
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值