POJ 2195 Going Home(最小费用最大流)

Going Home

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13994 Accepted: 7167

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       以前用二分图最大权值匹配做过这题。 现在用最小费用最大流写了一下,总结下模板吧!

/*
POJ 2195 Going Home
邻接矩阵形式最小费用最大流
*/

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;


//************************************************************
//最小费用最大流算法
//SPFA求最短路
//邻接矩阵形式
//初始化:cap:容量,没有边为0
//cost:耗费,对称形式,没有边的也为0
//c是最小费用
//f是最大流
//*******************************************************
const int MAXN=500;
const int INF=0x3fffffff;
int cap[MAXN][MAXN];//容量,没有边为0
int flow[MAXN][MAXN];
//耗费矩阵是对称的,有i到j的费用,则j到i的费用为其相反数
int cost[MAXN][MAXN];


int n;//顶点数目0~n-1
int f;//最大流
int c;//最小费用
int start,end;//源点和汇点

bool vis[MAXN];//在队列标志
int que[MAXN];
int pre[MAXN];
int dist[MAXN];//s-t路径最小耗费
bool SPFA()
{
    int front=0,rear=0;
    for(int u=0;u<=n;u++)
    {
        if(u==start)
        {
            que[rear++]=u;
            dist[u]=0;
            vis[u]=true;
        }
        else
        {
            dist[u]=INF;
            vis[u]=false;
        }
    }
    while(front!=rear)
    {
        int u=que[front++];
        vis[u]=false;
        if(front>=MAXN)front=0;
        for(int v=0;v<=n;v++)
        {
            if(cap[u][v]>flow[u][v]&&dist[v]>dist[u]+cost[u][v])
            {
                dist[v]=dist[u]+cost[u][v];
                pre[v]=u;
                if(!vis[v])
                {
                    vis[v]=true;
                    que[rear++]=v;
                    if(rear>=MAXN)rear=0;
                }
            }
        }
    }
    if(dist[end]>=INF)return false;
    return true;
}

void minCostMaxflow()
{
    memset(flow,0,sizeof(flow));
    c=f=0;
    while(SPFA())
    {
        int Min=INF;
        for(int u=end;u!=start;u=pre[u])
           Min=min(Min,cap[pre[u]][u]-flow[pre[u]][u]);
        for(int u=end;u!=start;u=pre[u])
        {
            flow[pre[u]][u]+=Min;
            flow[u][pre[u]]-=Min;
        }
        c+=dist[end]*Min;
        f+=Min;
    }
}
//************************************************************

struct Node
{
    int x,y;
};
Node node1[MAXN],node2[MAXN];
char str[MAXN][MAXN];
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int N,M;
    while(scanf("%d%d",&N,&M))
    {
        if(N==0&&M==0)break;
        int tol1=0,tol2=0;
        for(int i=0;i<N;i++)
        {
            scanf("%s",&str[i]);
            for(int j=0;j<M;j++)
            {
                if(str[i][j]=='m')
                {
                    tol1++;
                    node1[tol1].x=i;
                    node1[tol1].y=j;
                }
                else if(str[i][j]=='H')
                {
                    tol2++;
                    node2[tol2].x=i;
                    node2[tol2].y=j;
                }
            }
        }
        start=0;
        n=tol1+tol2+1;
        end=tol1+tol2+1;
        memset(cap,0,sizeof(cap));
        memset(cost,0,sizeof(cost));
        for(int i=1;i<=tol1;i++)
        {
            cost[0][i]=cost[i][0]=0;
            cap[0][i]=1;
        }
        for(int i=1;i<=tol2;i++)
        {
            cost[tol1+i][end]=0;
            cap[tol1+i][end]=1;
        }
        for(int i=1;i<=tol1;i++)
          for(int j=1;j<=tol2;j++)
          {
              cost[i][tol1+j]=abs(node1[i].x-node2[j].x)+abs(node1[i].y-node2[j].y);
              cost[tol1+j][i]=-cost[i][tol1+j];
              cap[i][tol1+j]=1;
          }
        minCostMaxflow();
        printf("%d\n",c);
    }
    return 0;
}

 

 

 

邻接表形式:

/*
POJ 2195 Going Home
邻接表形式最小费用最大流
*/

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;

const int MAXN=1000;
const int MAXE=100000;
const int INF=0x3f3f3f3f;
struct Edge
{
    int from;
    int to;
    int next;
    int re;//记录逆边的下标
    int cap;//容量
    int cost;//费用
}edge[MAXE];
int pre[MAXN];
int head[MAXN];
bool vis[MAXN];
int que[MAXN];
int dist[MAXN];
int tol;//边的总数
void add(int u,int v,int ca,int co)
{
    edge[tol].from=u;
    edge[tol].to=v;
    edge[tol].cap=ca;
    edge[tol].cost=co;
    edge[tol].re=tol+1;
    edge[tol].next=head[u];
    head[u]=tol++;

    edge[tol].from=v;//加逆边
    edge[tol].to=u;
    edge[tol].cap=0;
    edge[tol].cost=-co;
    edge[tol].re=tol-1;
    edge[tol].next=head[v];
    head[v]=tol++;
}
int n;
int start;
int end;
bool SPFA()
{
    int front=0,rear=0;
    for(int v=0;v<=n;v++)
    {
        if(v==start)
        {
            que[rear++]=v;
            vis[v]=true;
            dist[v]=0;
        }
        else
        {
            dist[v]=INF;
            vis[v]=false;
        }
    }
    while(front!=rear)
    {
        int u=que[front++];
        vis[u]=false;
        if(front>=MAXN)front=0;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].cap&&dist[v]>dist[u]+edge[i].cost)
            {
                dist[v]=dist[u]+edge[i].cost;
                pre[v]=i;
                if(!vis[v])
                {
                    que[rear++]=v;
                    vis[v]=true;
                    if(rear>=MAXN)rear=0;
                }
            }
        }
    }
    if(dist[end]==INF)return false;
    return true;
}
int c;//费用
int f;//最大流

void minCostMaxflow()
{
    c=f=0;
    int u,p;
    while(SPFA())
    {
        int Min=INF;
        for(u=end;u!=start;u=edge[p].from)
        {
            p=pre[u];
            Min=min(Min,edge[p].cap);
        }
        for(u=end;u!=start;u=edge[p].from)
        {
            p=pre[u];
            edge[p].cap-=Min;
            edge[edge[p].re].cap+=Min;

        }
        c+=dist[end]*Min;
        f+=Min;
    }
}


struct Node
{
    int x,y;
};
Node node1[MAXN],node2[MAXN];
char str[MAXN][MAXN];
int main()
{
   // freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int N,M;
    while(scanf("%d%d",&N,&M))
    {
        if(N==0&&M==0)break;
        int tol1=0,tol2=0;
        for(int i=0;i<N;i++)
        {
            scanf("%s",&str[i]);
            for(int j=0;j<M;j++)
            {
                if(str[i][j]=='m')
                {
                    tol1++;
                    node1[tol1].x=i;
                    node1[tol1].y=j;
                }
                else if(str[i][j]=='H')
                {
                    tol2++;
                    node2[tol2].x=i;
                    node2[tol2].y=j;
                }
            }
        }
        start=0;
        n=tol1+tol2+1;
        end=tol1+tol2+1;

        //加边之前一定要初始化
        tol=0;//边数
        memset(head,-1,sizeof(head));//一定要初始化为-1


        for(int i=1;i<=tol1;i++)
        {
            add(0,i,1,0);
        }
        for(int i=1;i<=tol2;i++)
        {
            add(tol1+i,end,1,0);
        }
        for(int i=1;i<=tol1;i++)
          for(int j=1;j<=tol2;j++)
          {
              int temp=abs(node1[i].x-node2[j].x)+abs(node1[i].y-node2[j].y);
              add(i,tol1+j,1,temp);
          }
        minCostMaxflow();
        printf("%d\n",c);
    }
    return 0;
}

 

    原文作者:kuangbin
    原文地址: https://www.cnblogs.com/kuangbin/archive/2012/08/22/2650897.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞