Going Home--最小费用最大流

Going Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 22013 Accepted: 11127

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://poj.org/problem?id=2195

第一次写最小费用最大流,这里就不做太详细的讲解了,本来想讲讲的,但是网上的大神的博客实在是把最小费用最大流写的太详细了,我自认为我写不出来更详细的了,最小费用最大流可以理解为从最大流上加了个费用(权值),所以加上更新函数,最大流算法用EK算法,判断有没有增广路。

增广路是这样一条从s到t的路径 路径上每条边残留容量都为正。

最小费用最大流不算太难,就是写起来比较麻烦。。。太考验我了


代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <algorithm>
#define V 60100
#define E 1000100
#define inf 0x3f3f3f3f
using namespace std;
struct node{
    int v,pre,w,cost,u;
}edge[V];
int p[V],nEdge;
int dis[V];
int pre[V];
bool vis[V];
void Init(){
    memset(p,-1,sizeof(p));
    nEdge=0;
}
void connect(int u,int v,int w,int cost){//前向星
    edge[nEdge].v=v;
    edge[nEdge].u=u;
    edge[nEdge].w=w;
    edge[nEdge].cost=cost;
    edge[nEdge].pre=p[u];
    p[u]=nEdge++;

    edge[nEdge].v=u;
    edge[nEdge].u=v;
    edge[nEdge].w=0;//反向残余网络流量为零
    edge[nEdge].cost=-cost;//反向为负
    edge[nEdge].pre=p[v];
    p[v]=nEdge++;
}
bool spfa(int Begin,int End){//寻找增广路
    for(int i=0;i<End+2;i++){
        dis[i]=inf;
        vis[i]=false;
        pre[i]=-1;
    }
    queue<int >Q;
    vis[Begin]=true;
    dis[Begin]=0;
    Q.push(Begin);
    while(!Q.empty()){
        int t=Q.front();
        Q.pop();
        vis[t]=false;
        for(int i=p[t];~i;i=edge[i].pre){
            if(edge[i].w>0){
                int v=edge[i].v;
                if(dis[v]>dis[t]+edge[i].cost){
                    dis[v]=dis[t]+edge[i].cost;
                    pre[v]=i;
                    if(!vis[v]){
                        vis[v]=true;
                        Q.push(v);
                    }
                }
            }
        }
    }
    return dis[End]!=inf;
}
int fax(int Begin,int End){//移动
    int ans=0,flow;
    int flow_sum=0;
    while(spfa(Begin,End)){
        flow=inf;
        for(int i=pre[End];~i;i=pre[edge[i].u]){
            if(edge[i].w<flow){
                flow=edge[i].w;
            }
        }
        for(int i=pre[End];~i;i=pre[edge[i].u]){
            edge[i].w-=flow;
            edge[i^1].w+=flow;//临近边要加上流量,残余网络,要有反悔的机会
        }
        ans+=dis[End];
        flow_sum+=flow;
    }
    return ans;
}
char mp[202][202];
int HH[V];
int HL[V];
int MH[V];
int ML[V];
int S,T;
int cm,ch;
int main(){
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        if(n+m==0)
            break;
        Init();
        scanf("%*c");
        for(int i=0;i<n;i++){
            gets(mp[i]);
        }
        cm=ch=1;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(mp[i][j]=='H'){
                    HH[ch]=i;
                    HL[ch++]=j;
                }
                else if(mp[i][j]=='m'){
                    MH[cm]=i;
                    ML[cm++]=j;
                }
            }
        }
        cm--;
        ch--;
        S=0;
        T=cm+ch+1;
        for(int i=1;i<=cm;i++){//超级源点
            connect(S,i,1,0);
        }
        for(int i=1;i<=ch;i++){//超级汇点
            connect(cm+i,T,1,0);
        }
        for(int i=1;i<=cm;i++){//加边
            for(int j=1;j<=ch;j++){
                int tm=abs(HH[j]-MH[i])+abs(HL[j]-ML[i]);
                connect(i,cm+j,1,tm);
            }
        }
        int ans=fax(S,T);
        printf("%d\n",ans);
    }
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值