hdu 1533 KM算法 二分图最优匹配


Going Home

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3338    Accepted Submission(s): 1692


Problem 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


暴力建图,,,本题是求最小的匹配,可以把权值全部变成负的,然后套KM算法模板就行了,最后统计答案的时候应该加权值的相反数。

定的时间是5秒,但很多人15ms就过了,,,数据略水。。


#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#define inf 0x3f3f3f3f
#define mem(a,x) memset(a,x,sizeof(a))

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

inline int in()
{
    int res=0;char c;
    while((c=getchar())<'0' || c>'9');
    while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
    return res;
}

const int N=102;
int g[N][N];
int lx[N];int ly[N];
int visx[N];
int visy[N];
char maze[N][N];
int match[N];
int slack[N];
int n,m;


bool dfs(int x,int tmp)
{
    visx[x]=1;
    for(int i=1;i<=tmp;i++)
    {
        if(visy[i])continue;
        int t=lx[x]+ly[i]-g[x][i];
        if(t==0)
        {
            visy[i]=1;
            if(match[i]==-1 || dfs(match[i],tmp))
            {
                match[i]=x;
                return 1;
            }
        }
        else if(slack[i]>t)slack[i]=t;
    }
    return 0;
}

int KM(int cnt,int tmp) //KM算法模板...
{
    mem(match,-1);
    mem(ly,0);
    for(int i=1;i<=cnt;i++)
    {
        lx[i]=-inf;
        for(int j=1;j<=tmp;j++)
        {
            lx[i]=max(lx[i],g[i][j]);
        }
    }
    for(int j=1;j<=cnt;j++)
    {
        mem(slack,inf);
        while(1)
        {
            mem(visx,0);
            mem(visy,0);
            if(dfs(j,tmp))break;
            int d=inf;
            for(int i=1;i<=cnt;i++)
            {
                if(!visy[i] && d>slack[i]) d=slack[i];
            }
            for(int i=1;i<=cnt;i++)
            {
                if(visx[i])lx[i]-=d;
                if(visy[i])ly[i]+=d;
                else slack[i]-=d;
            }

        }
    }
    int ans=0;
    for(int i=1;i<=cnt;i++)
    {
        if(match[i]!=-1)ans+=-g[match[i]][i];  //加权值的相反数
    }
    return ans;
}

int main()
{
    while(~scanf("%d%d",&n,&m) && n)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%s",maze[i]);
        }
        int cnt=0;  //计数人的数目的
        int tmp=0;   //房子数目
        for(int i=0;i<n;i++)//建图,直接暴力建图
        {
            for(int j=0;j<m;j++)
            {
                if(maze[i][j]=='m')
                {
                    ++cnt;
                    tmp=0;
                    for(int k=0;k<n;k++)
                    {
                        for(int x=0;x<m;x++)
                        {
                            if(maze[k][x]=='H')
                            {
                                ++tmp;
                                g[cnt][tmp]=-(abs(i-k)+abs(j-x));//变为负的
                            }
                        }
                    }
                }
            }
        }
        printf("%d\n",KM(cnt,tmp));
    }
    return 0;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值