POJ 2195 Going Home

题目链接 : POJ2195

Going Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15209 Accepted: 7781

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

 

第一次写的最小费用流  也是个模板题吧。

很简单的建图。 

不过这题也能用二分图最大权匹配来搞  两份代码一起附上

最小费用流

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
using namespace std;



int cap[205][205],fee[205][205];
int a[205],p[205],dis[205],que[205];
int s,t;
int cost_flow()
{
	int ans=0;
	bool inque[205]={0};
	while(1)
	{
		int tail=0,front=1,x;
		a[s]=0x3fffffff;
		a[t]=0;
		for(int i=1;i<=t;i++) 
			dis[i]=0x3fffffff;
		dis[s]=0;
		que[0]=s;
		while(tail<front)
		{
			x=que[tail++];
			inque[x]=0;
			for(int i=1;i<=t;i++)
			if(fee[x][i]+dis[x]<dis[i] && cap[x][i]>0)
			{
				dis[i]=dis[x]+fee[x][i];
				p[i]=x;
				a[i]=min(a[x],cap[x][i]);
				if(inque[i]) continue;
				inque[i]=1;
				que[front++]=i;
			}
		} 
		if(a[t]==0) return ans;
		x=t;
		while(x!=s)
		{
			cap[p[x]][x]-=a[t];
			cap[x][p[x]]+=a[t];
			ans+=fee[p[x]][x]*a[t];
			x=p[x];
		}
	}
	return -1;
}
int main()
{
//	freopen("1.txt","r",stdin);
	int n,m;
	char str[105];
	int num[2][105];
	while(scanf("%d%d",&n,&m))
	{
		if(n==0 && m==0) break;
		int k1=0,k2=0;
		s=0;
		memset(cap,0,sizeof(cap));
		memset(fee,0,sizeof(fee));
		for(int i=0;i<n;i++)
		{
			scanf("%s",str);
			for(int j=0;str[j];j++)
				if(str[j]=='H') num[0][k1++]=i*1000+j;
				else if(str[j]=='m') num[1][k2++]=i*1000+j;
		}
		t=2*k1+1;
		for(int i=1;i<=k1;i++)
		{
			cap[0][i]=1;
			cap[i+k1][t]=1;
		}
		for(int i=0;i<k1;i++)
			for(int j=0;j<k1;j++)
			{
				cap[i+1][j+k1+1]=0x3fffffff;
				fee[i+1][j+k1+1]=abs(num[0][i]/1000-num[1][j]/1000)+abs(num[0][i]%1000-num[1][j]%1000);
				fee[j+k1+1][i+1]=-fee[i+1][j+k1+1];
			}
		printf("%d\n",cost_flow());
	}
    return 0;
}


 

二分图最大权匹配

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
using namespace std;


int n,m;
bool visx[105],visy[105];
int p[105],gra[105][105];
int x[105],y[105],slack[105];
bool find(int s)
{
    visx[s]=1;
    for(int i=1;i<=m;i++)
        if(visy[i]==0)
        {
            int f=x[s]+y[i]-gra[s][i];
            if(f==0)
            {
                visy[i]=1;
                if(p[i]==0 || find(p[i]))
                {
                    p[i]=s;
                    return 1;
                }
            }
            else if(slack[i]>f) slack[i]=f;
        }
    return 0;
}
int Kuhn_Munkras()
{
    int i,j;
    memset(y,0,sizeof(y));
    memset(p,0,sizeof(p));
    for(i=1;i<=n;i++)
        for(j=1,x[i]=-0x7fffffff;j<=m;j++)
            x[i]=max(x[i],gra[i][j]);
    for(i=1;i<=n;i++)
    {
        memset(slack,0x7f,sizeof(slack));
        while(1)
        {
            memset(visx,0,sizeof(visx));
            memset(visy,0,sizeof(visy));
            if(find(i)) break;
            int d=0x7ffffff;
            for(j=1;j<=m;j++)
                if(visy[j]==0 && d>slack[j]) d=slack[j];
            for(j=1;j<=n;j++)
                if(visx[j]) x[j]-=d;
            for(j=1;j<=m;j++)
                if(visy[j]) y[j]+=d;
                else slack[j]-=d;
        }
    }
    int ans=0;
    for(i=1;i<=m;i++)
        if(p[i]) ans+=gra[p[i]][i];
    return ans;
}
char maze[105];
int s1[105][2],s2[105][2];
int main()
{
    while(scanf("%d%d",&n,&m))
    {
        if(n==0) break;
        int k1=0,k2=0;
        for(int i=0;i<n;i++)
        {
            scanf("%s",maze);
            for(int j=0;j<m;j++)
            {
                if(maze[j]=='H')
                {
                    s1[k1][0]=i;
                    s1[k1++][1]=j;
                }
                if(maze[j]=='m')
                {
                    s2[k2][0]=i;
                    s2[k2++][1]=j;
                }
            }
        }
        for(int i=0;i<k1;i++)
            for(int j=0;j<k2;j++)
                gra[i+1][j+1]=-abs(s1[i][0]-s2[j][0])-abs(s1[i][1]-s2[j][1]);
        n=k1;m=k2;
        printf("%d\n",-Kuhn_Munkras());
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值