用二分图模型解决poj 2195

       之前这道题用了费用流来解决,这次用了二分图最佳匹配来解决。

       不过,用二分图最佳匹配解决这道题时,要注意题目要求花费最小,所以是求权值之和最小的最佳匹配。要用一个大数减去原有权值作为新的权值,最后输出答案时,要注意还原真正的权值。

       通过这道题,也可以发现费用流与二分图最佳匹配之间有所关联,而且就这道题来看,二分图的代码量会小于费用流的代码量,所以尽可能采用二分图模型来解决问题可能会简单些。

代码(C++):

#include <cstdlib>
#include <iostream>
#include <cmath>

#define MAX 150
#define INF (1<<30)
using namespace std;

//#define LOCAL

typedef pair<int,int> pii;
pii array_h[MAX],array_m[MAX];

int w[MAX][MAX],lx[MAX],ly[MAX],cy[MAX],vx[MAX],vy[MAX];

bool match(int u,int n)
{
    int i;
    vx[u]=true;
    for(i=0;i<n;i++)
    {
        if(lx[u]+ly[i]==w[u][i]&&!vy[i])
        {
            vy[i]=true;                            
            if(cy[i]==-1||match(cy[i],n))
            {
                cy[i]=u;
                return true;                       
            }                            
        }            
    } 
    return false;
}

void update(int n)
{
    int i,j,d;
    d=INF;
    for(i=0;i<n;i++) if(vx[i])
    {
        for(j=0;j<n;j++) if(!vy[j])
        {
            d=min(d,lx[i]+ly[j]-w[i][j]);                     
        }             
    } 
    for(i=0;i<n;i++)
    {
        if(vx[i]) lx[i]-=d;
        if(vy[i]) ly[i]+=d;            
    }
}

int km(int n)
{
    int i,j,ans;
    for(i=0;i<n;i++)
    {
        lx[i]=-INF;
        ly[i]=0;
        cy[i]=-1;
        for(j=0;j<n;j++) lx[i]=max(lx[i],w[i][j]);           
    }
    for(i=0;i<n;i++)
    {
        while(1)
        {
            memset(vx,false,sizeof(vx));
            memset(vy,false,sizeof(vy));
            if(match(i,n)) break;
            else update(n);    
        }            
    }
    ans=0;
    for(i=0;i<n;i++) ans+=w[cy[i]][i];
    return ans;
}

int main(int argc, char *argv[])
{
#ifdef LOCAL 
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif
    int n,m,a,b,i,j,fee,max;
    char ch;
    while(scanf("%d %d",&n,&m)&&n+m!=0)
    {
        a=b=0;            
        for(i=0;i<n;i++)
        {
            getchar();            
            for(j=0;j<m;j++)
            {
                 scanf("%c",&ch);
                 if(ch=='H') array_h[a++]=make_pair(i,j);
                 else if(ch=='m') array_m[b++]=make_pair(i,j);        
            }            
        }
        max=n+m;
        for(i=0;i<b;i++)
        {
            for(j=0;j<a;j++)
            {
                fee=abs(array_m[i].first-array_h[j].first)+abs(array_m[i].second-array_h[j].second);     
                w[i][j]=max-fee;       
            }            
        } 
        printf("%d\n",max*a-km(a));           
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

题目( http://poj.org/problem?id=2195):

Going Home
Time Limit: 1000MS Memory Limit: 65536K
   

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值