poj2195 Going Home(费用流|KM)

来源:互联网 发布:网络配线架报价 编辑:程序博客网 时间:2024/05/29 16:23

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

分析:
很显然的费用流,
只要处理出任意一对小人和房子之间的曼哈顿距离就好了
这里写图片描述

这里写代码片#include<cstdio>#include<cstring>#include<queue>#include<iostream>#include<cmath>using namespace std;const int INF=1000010;struct node{    int x,y,v,cost,next;};node way[40040];char ch[101][101],c;int n,m,s,e,tt,tot,toto;int st[10010],home[5010][2],man[5010][2];bool p[10010];int pre[10010],d[10010];int ab(int x){    if (x>=0) return x;    else return -x;}void add(int u,int w,int z,int co){    tot++;    way[tot].x=u;way[tot].y=w;way[tot].v=z;way[tot].cost=co;way[tot].next=st[u];st[u]=tot;    tot++;    way[tot].x=w;way[tot].y=u;way[tot].v=0;way[tot].cost=-co;way[tot].next=st[w];st[w]=tot;    return;}bool spfa(){    int i;    memset(p,1,sizeof(p));    memset(d,0x7f,sizeof(d));    memset(pre,0,sizeof(pre));    queue<int> q;    p[s]=0;    d[s]=0;    q.push(s);    while (!q.empty())    {        int r=q.front();        q.pop();        for (i=st[r];i!=-1;i=way[i].next)        {            if (way[i].v&&d[way[i].y]>d[r]+way[i].cost)            {                d[way[i].y]=d[r]+way[i].cost;                 pre[way[i].y]=i;                if (p[way[i].y])                {                    q.push(way[i].y);                    p[way[i].y]=0;                }            }        }        p[r]=1;    }    return d[e]<0x7f;}int doit(){    int ans=0,sum,i;    while (spfa())    {        sum=INF;        for (i=e;i!=s;i=way[pre[i]].x)            sum=min(sum,way[pre[i]].v);        ans+=sum*d[e];        for (i=e;i!=s;i=way[pre[i]].x)        {            way[pre[i]].v-=sum;            way[pre[i]^1].v+=sum;        }    }    return ans;}int main(){    scanf("%d%d%c",&n,&m,&c);    while (n&&m)    {        tt=toto=0;        tot=-1;        for (int i=1;i<=n;i++)        {            for (int j=1;j<=m;j++)            {                scanf("%c",&ch[i][j]);                if (ch[i][j]=='H')                {                    tt++;                    home[tt][0]=i; home[tt][1]=j;                }                else if (ch[i][j]=='m')                {                    toto++;                    man[toto][0]=i;                    man[toto][1]=j;                }            }            scanf("%c",&c);        }        memset(st,-1,sizeof(st));        s=0;        e=2*tt+1;        for (int i=1;i<=tt;i++)        {           add(0,i,1,0);           add(i+tt,e,1,0);           for (int j=1;j<=tt;j++)           {                int r=ab(man[i][0]-home[j][0])+ab(man[i][1]-home[j][1]);                add(i,j+tt,1,r);           }        }        printf("%d\n",doit());        scanf("%d%d%c",&n,&m,&c);    }    return 0;} 

还有一种KM做法,因为KM是求最大权完美匹配的
所以所有的边权*-1

时间上是网络流的十分之一
这里写图片描述

tip

这道题要用G++交(很迷)

//这里写代码片#include<cstdio>#include<cstring>#include<iostream>using namespace std;const int INF=0x33333333;int Lx[110],Ly[110],slack[110],W[110][110];bool L[110],R[110];int man[110][2],hou[110][2],tt1,tt2,belong[110],n,m;char s[110];int abs(int x){if (x>0) return x;else return -x;}int match(int i,int n){    L[i]=1;    for (int j=1;j<=n;j++)        if (!R[j])        {            int v=Lx[i]+Ly[j]-W[i][j];            if (!v)            {                R[j]=1;                if (!belong[j]||match(belong[j],n))                {                    belong[j]=i;                    return 1;                }            }            else slack[j]=min(slack[j],v);        }    return 0;}int KM(){    int n=tt1;    memset(belong,0,sizeof(belong));    for (int i=1;i<=n;i++)    {        Ly[i]=0;        Lx[i]=W[i][1];        for (int j=2;j<=n;j++)            Lx[i]=max(Lx[i],W[i][j]);    }    for (int i=1;i<=n;i++)    {        for (int j=1;j<=n;j++) slack[j]=INF;        while (1)        {            memset(L,0,sizeof(L));            memset(R,0,sizeof(R));            if (match(i,tt1)) break;            int a=INF;            for (int j=1;j<=n;j++)                if (!R[j]) a=min(slack[j],a);            for (int j=1;j<=n;j++)                if (L[j]) Lx[j]-=a;            for (int j=1;j<=n;j++)                if (R[j]) Ly[j]+=a;                else slack[j]-=a;        }    }    int ans=0;    for (int i=1;i<=n;i++) ans+=W[belong[i]][i];    return -ans;}int main(){    scanf("%d%d",&n,&m);    while (n&&m)    {        tt1=tt2=0;        for (int i=1;i<=n;i++)        {            scanf("%s",s);            for (int j=0;j<m;j++)                if (s[j]=='H') hou[++tt2][0]=i,hou[tt2][1]=j+1;                else if (s[j]=='m') man[++tt1][0]=i,man[tt1][1]=j+1;        }        for (int i=1;i<=tt1;i++)            for (int j=1;j<=tt2;j++)                W[i][j]=-(abs(man[i][0]-hou[j][0])+abs(man[i][1]-hou[j][1]));        printf("%d\n",KM());        scanf("%d%d",&n,&m);    }}
原创粉丝点击