Poj 2195 Going Home(费用流)

Going Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 22327 Accepted: 11280
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

/*
费用流裸题.
二分图搞.
从源点向man连边流量为1费用为0.
从man向house连边流量为1费用为曼哈顿距离.
从house向汇点连边流量为1费用为0. 
*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#define MAXN 101
#define INF 1e9
using namespace std;
int n,m,tot1,tot2,cut=1,S,T,ans;
int g[MAXN][MAXN],vis[MAXN*MAXN],fa[MAXN*MAXN],head[MAXN*MAXN],dis[MAXN*MAXN];
struct data{int x,y;}a[MAXN*MAXN],b[MAXN*MAXN];
struct edge{int u,v,c,f,next;}e[MAXN*MAXN*2];
queue<int>q;
void add(int u,int v,int c,int f)
{
    e[++cut].u=u;e[cut].v=v;e[cut].next=head[u];e[cut].c=c;e[cut].f=f;head[u]=cut;
    e[++cut].u=v;e[cut].v=u;e[cut].next=head[v];e[cut].c=0;e[cut].f=-f;head[v]=cut;
}
bool bfs(int t)
{
    for(int i=1;i<=T;i++) dis[i]=INF;dis[0]=0;
    q.push(S);
    while(!q.empty())
    {
        int u=q.front();q.pop();vis[u]=0;
        for(int i=head[u];i;i=e[i].next)
        {
            int v=e[i].v;
            if(dis[v]>dis[u]+e[i].f&&e[i].c)
            {
                dis[v]=dis[u]+e[i].f;fa[v]=i;
                if(vis[v]!=t) vis[v]=t,q.push(v);
            }
        }
    }
    return dis[T]!=INF;
}
void mincost()
{
    int t=1;ans=0;
    while(bfs(t))
    {
        int tmp=fa[T],x=INF;
        while(tmp) x=min(x,e[tmp].c),tmp=fa[e[tmp].u];
        tmp=fa[T];
        while(tmp)
        {
            e[tmp].c-=x;
            e[tmp^1].c+=x;
            e[tmp].f*=x;
            ans+=e[tmp].f;
            tmp=fa[e[tmp].u];
        }
        t++;
    }
    printf("%d
",ans);
}
void slove()
{
    for(int i=1;i<=tot1;i++) add(0,i,1,0);
    for(int i=1;i<=tot2;i++) add(i+tot1,tot1+tot2+1,1,0);
    for(int i=1;i<=tot1;i++)
      for(int j=1;j<=tot2;j++)
      {
        int x=fabs(a[i].x-b[j].x)+fabs(a[i].y-b[j].y);
        add(i,tot1+j,1,x);
      }
    mincost();
    return ;
}
void Clear()
{
    tot1=tot2=0;cut=1;
    memset(head,0,sizeof head);
    memset(b,0,sizeof b);
    return ;
}
int main()
{
    char ch;
    while(scanf("%d%d",&n,&m))
    {
        if(!n&&!m) break;
        Clear();
        for(int i=1;i<=n;i++)
          for(int j=1;j<=m;j++)
          {
            cin>>ch;
            if(ch=='m') a[++tot1].x=i,a[tot1].y=j;
            else if(ch=='H') b[++tot2].x=i,b[tot2].y=j;
          }
        T=tot1+tot2+1;slove();
    }
    return 0;
}
原文地址:https://www.cnblogs.com/nancheng58/p/10068077.html