HDU 1533 Going Home 最小费用流

//author: CHC
//First Edit Time:	2014-09-12 10:15
//Last Edit Time:	2014-09-12 12:03
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#include <limits.h>
using namespace std;
typedef long long LL;
const int MAXN=1e+4 + 10;
const int MAXM=1e+6;
const int INF = INT_MAX;
struct Edge
{
    int from,to,ci,cost,next;
    Edge(){}
    Edge(int _from,int _to,int _ci,int _cost,int _next):from(_from),to(_to),ci(_ci),cost(_cost),next(_next){}
}e[MAXM];
int head[MAXN],tot;
int q[MAXM];
int dis[MAXN],pre[MAXN],rec[MAXN],vis[MAXN];
inline void init(){
    memset(head,-1,sizeof(head));
    tot=0;
}
inline void AddEdge1(int u,int v,int ci,int cost){
    e[tot]=Edge(u,v,ci,cost,head[u]);
    head[u]=tot++;
    e[tot]=Edge(v,u,0,-cost,head[v]);
    head[v]=tot++;
}
inline bool spfa(int S,int T,LL &cost,LL &flow){
    int i,h=0,t=0;
    for(i=0;i<=MAXN;i++){
        dis[i]=INF;
        vis[i]=false;
    }
    q[h]=S;
    dis[S]=0;
    vis[S]=true;
    while(h<=t){
        int u=q[h++];
        vis[u]=false;
        for(i=head[u];~i;i=e[i].next){
            int v=e[i].to;
            if(e[i].ci>0&&dis[v]>dis[u]+e[i].cost){
                dis[v]=dis[u]+e[i].cost;
                pre[v]=u;
                rec[v]=i;
                if(!vis[v]){
                    vis[v]=1;
                    q[++t]=v;
                }
            }
        }
    }
    if(dis[T]==INF)return false;
    int minn=INF;
    for(i=T;i!=S;i=pre[i]){
        if(e[rec[i]].ci<minn)
            minn=e[rec[i]].ci;
    }
    for(i=T;i!=S;i=pre[i]){
        //cost+=minn*e[rec[i]].cost;
        e[rec[i]].ci-=minn;
        e[rec[i]^1].ci+=minn;
    }
    cost+=dis[T]*minn;
    flow+=minn;
    return true;
}
inline void mincostmaxflow(int S,int T,LL &cost,LL &flow){
    while(spfa(S,T,cost,flow));
}
char map1[110][110],disx[110][110];
struct point
{
    int i,j;
    point(){}
    point(int _i,int _j):i(_i),j(_j){}
}hh[MAXN],mm[MAXN];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int n,m;
int Dis(point a,point b){
    return abs(a.i-b.i)+abs(a.j-b.j);
}
int main()
{
    while(scanf("%d%d",&n,&m),(n||m)){
        init();
        int cnt1=0,cnt2=0;
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            scanf(" %c",&map1[i][j]);
            if(map1[i][j]=='H') hh[cnt1++]=point(i,j);
            if(map1[i][j]=='m') mm[cnt2++]=point(i,j);
        }
        for(int i=0;i<cnt2;i++){
            for(int j=0;j<cnt1;j++){
                AddEdge1(i,j+cnt2,1,Dis(mm[i],hh[j]));
                //printf("%d-->%d %d %d\n",i,j+cnt2,1,disx[hh[j].i][hh[j].j]);
            }
        }
        for(int i=0;i<cnt2;i++){
            AddEdge1(cnt1+cnt2,i,1,0);
            //printf("%d-->%d %d %d\n",cnt1+cnt2,i,1,0);
        }
        for(int i=0;i<cnt1;i++){
            AddEdge1(i+cnt2,cnt1+cnt2+1,1,0);
            //printf("%d-->%d %d %d\n",i+cnt2,cnt1+cnt2+1,1,0);
        }
        LL cost=0,flow=0;
        mincostmaxflow(cnt1+cnt2,cnt1+cnt2+1,cost,flow);
        printf("%I64d\n",cost);
    }
    return 0;
}

你可能感兴趣的:(费用流)