首页 > 代码库 > POJ 2195 Going Home (最小费用最大流)

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


题目链接:http://poj.org/problem?id=2195


题意:n*m的矩阵,地图上有若干个人(m)和房子(H),且人与房子的数量一致。man每移动一格费用为1,一个房子只能住一个人。现在要求所有的人出发,都入住房子,求最少话费。


思路:建立一个超级源点和汇点,源点与人相连费用为0,容量为1,人与房子相连,费用为人与房子的距离,容量为1,房子与汇点相连,费用为0,容量为1



#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
const int maxn = 3010;
const int maxm = 80000;
const int inf = 1e8;
#define MIN INT_MIN
#define MAX 1e6
#define LL long long
#define init(a) memset(a,0,sizeof(a))
#define FOR(i,a,b) for(int i = a;i<b;i++)
#define max(a,b) (a>b)?(a):(b)
#define min(a,b) (a>b)?(b):(a)
using namespace std;
struct node{
    int u,v,w,cap,next;
}edge[maxm];
struct point
{
    int x,y;
}M[110],H[110];
int head[maxn],dis[maxn],pre[maxn];
int cnt,n,Mnum,Hnum;
bool vis[maxn];
void add(int u,int v,int w,int cap)
{
    edge[cnt].u = u;
    edge[cnt].v = v;
    edge[cnt].w = w;
    edge[cnt].cap = cap;
    edge[cnt].next = head[u];
    head[u] = cnt++;

    edge[cnt].u = v;
    edge[cnt].v = u;
    edge[cnt].w = -w;
    edge[cnt].cap = 0;
    edge[cnt].next = head[v];
    head[v] = cnt++;
}
void initt()
{
    cnt = 0;
    memset(head,-1,sizeof(head));
    Mnum = 1;
    Hnum = 1;
}
bool SPFA(int s,int t)
{
    queue<int>q;
    while(q.empty()==false) q.pop();

    q.push(s);

    init(vis);
    memset(pre,-1,sizeof(pre));

    FOR(i,s,t+1)
    dis[i] = inf;
    vis[s] = 1;
    dis[s] = 0;

    while(!q.empty())
    {
        int uu = q.front();
        q.pop();
        vis[uu] = 0;
        for(int i = head[uu];i!=-1;i = edge[i].next)
        {
            if(edge[i].cap && dis[edge[i].v] > dis[uu] + edge[i].w)//找最小费用
            {
                dis[edge[i].v] = dis[uu] + edge[i].w;
                pre[edge[i].v] = i;//记录路径
                if(!vis[edge[i].v])
                {
                    vis[edge[i].v] = 1;

                q.push(edge[i].v);
                }

            }
        }
    }
    if(dis[t]!=inf)
        return 1;
    return 0;

}
int MinCostMaxFlow(int s,int t)
{
    int flow = 0,cost = 0;//总流量、总费用
    while(SPFA(s,t))
    {
        int df = inf;
        for(int i = pre[t];i!=-1;i=pre[edge[i].u])
        {
            if(df > edge[i].cap)
                df = edge[i].cap;
        }
        flow += df;//这条路径的流量
        for(int i = pre[t];i!=-1;i=pre[edge[i].u])//更新流量
        {
            edge[i].cap -= df;
            edge[i^1].cap += df;
        }
        cost += df*dis[t];//单位费用诚意流量
    }
    return cost;
}
int main()
{
    int m,s,t;
    char ma[110][110];
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0 && m==0) break;
        initt();
        FOR(i,0,n)
        {
            scanf("%*c%s",ma[i]);
            FOR(j,0,m)
            {
                if(ma[i][j]=='m')
                {
                    M[Mnum].x = i;
                    M[Mnum++].y = j;
                }
                else if(ma[i][j]=='H')
                {
                    H[Hnum].x = i;
                    H[Hnum++].y = j;
                }
            }
        }
        Mnum--,Hnum--;
        s = 0;
        t = Mnum + Hnum + 1;
       // printf("Mnum = %d  Hnum  =  %d   t = %d\n",Mnum,Hnum,t);


        FOR(i,1,Mnum+1)
        {
            add(s,i,0,1);
            FOR(j,1,Hnum+1)
            {
                int dd = abs(M[i].x - H[j].x) + abs(M[i].y - H[j].y);
               // printf("dd = %d\n",dd);
                add(i,Mnum+j,dd,1);
                //printf("i->j %d->%d = %d\n",i,Mnum+j,dd);
                //add(Mnum+j,t,0,1);
            }
        }
        FOR(j,1,Hnum+1)
       {
           add(Mnum+j,t,0,1);
               // printf("j->t = %d->%d\n",Mnum+j,t);
       }
       int ans =  MinCostMaxFlow(s,t);
       cout<<ans<<endl;
    }
    return 0;
}