poj2195——Going Home(最小费用最大流)

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

看了两天的最小费用最大流,算是看懂了点皮毛。
这个算法顾名思义,就是在流量最大的前提下,达到所用的费用最小的要求。这题最小费用就是最短距离。
大致就是先用SPFA求出最短路径,该路径就是增广链上费用最小的一条,再在这条链上找最大流。
再就是调整容量,直到找不到增广路径为止,最后求得的最小费用就是答案。
建图的过程:
超级源点连接人,容量为1,代价为0;
人连接房屋,容量为1,代价为曼哈顿距离(最短距离);
房屋连接超级汇点,容量为1,代价为0。

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <cmath>
#include <map>
#include <algorithm>
#define INF 0x3f3f3f3f
#define MAXN 1005
#define Mod 10001
using namespace std;
struct Node
{
    int x,y;
};
Node man[MAXN],house[MAXN];
int n,m,source,ending,mincost,maxflow;
int res[MAXN][MAXN],cost[MAXN][MAXN],parent[MAXN],d[MAXN];
int need[MAXN],have[MAXN],nn[MAXN][MAXN],hh[MAXN][MAXN];
void spfa()
{
    queue<int> q;
    bool vis[MAXN];
    memset(vis,false,sizeof(vis));
    memset(parent,-1,sizeof(parent));
    for(int i=0; i<=ending; ++i)
        d[i]=INF;
    q.push(0);
    d[0]=0;
    vis[0]=true;
    while(!q.empty())
    {
        int v=q.front();
        q.pop();
        vis[v]=false;
        for(int i=0; i<=ending; ++i)
        {
            if(res[v][i]&&d[v]+cost[v][i]<d[i])
            {
                d[i]=d[v]+cost[v][i];
                parent[i]=v;
                if(!vis[i])
                {
                    q.push(i);
                    vis[i]=true;
                }
            }
        }
    }
}
void MCMF()
{
    int v,minflow;
    maxflow=0;
    mincost=0;
    while(1)
    {
        spfa();
        if(parent[ending]==-1)
            break;
        minflow=INF;
        v=ending;
        while(parent[v]!=-1)
        {
            minflow=min(minflow,res[parent[v]][v]);
            v=parent[v];
        }
        v=ending;
        while(parent[v]!=-1)
        {
            res[parent[v]][v]-=minflow;
            res[v][parent[v]]+=minflow;
            v=parent[v];
        }
        maxflow+=minflow;
        mincost+=d[ending];
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        getchar();
        if(n==0&&m==0)
            break;
        char s;
        int num1=0,num2=0;
        for(int i=1; i<=n; ++i)
            for(int j=1; j<=m; ++j)
            {
                cin>>s;
                if(s=='m')
                {
                    man[num1].x=i;
                    man[num1++].y=j;
                }
                if(s=='H')
                {
                    house[num2].x=i;
                    house[num2++].y=j;
                }
            }
        source=0,ending=num1+num2+1;
        memset(res,0,sizeof(res));
        memset(cost,0,sizeof(cost));
        for(int i=0; i<num1; ++i)
            res[source][i+1]=1;
        for(int i=0; i<num1; ++i)
            for(int j=0; j<num2; ++j)
            {
                res[i+1][num1+j+1]=1;
                cost[i+1][num1+j+1]=abs(man[i].x-house[j].x)+abs(man[i].y-house[j].y);
                cost[num1+j+1][i+1]=-cost[i+1][num1+j+1];
            }
        for(int i=0; i<num2; ++i)
            res[num1+i+1][ending]=1;
        MCMF();
        printf("%d\n",mincost);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值