码迷,mamicode.com
首页 > 编程语言 > 详细

(KM算法)hdu 1533

时间:2015-03-19 18:02:17      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

Going Home

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


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是用来求最大匹配最优值的,而这题要求最小,那么我们不妨把权值变为负数就可以了。
 
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
#define INF 1<<30
int n,m,numi,numj,nx,ny;
char s[205][205];
int lx[205],ly[205],visx[205],visy[205],w[205][205],slack[205],link[301];
int dfs(int x)
{
    visx[x]=1;
    for(int y=1;y<=ny;y++)
    {
        if(visy[y])
            continue;
        int t=lx[x]+ly[y]-w[x][y];
        if(t==0)
        {
            visy[y]=1;
            if(link[y]==-1||dfs(link[y]))
            {
                link[y]=x;
                return 1;
            }
        }
        else if(slack[y]>t)
            slack[y]=t;
    }
    return 0;
}
int km()
{
    memset(link,-1,sizeof(link));
    memset(ly,0,sizeof(ly));
    for(int i=1;i<=nx;i++)
    {
        lx[i]=-INF;
        for(int j=1;j<=ny;j++)
            lx[i]=max(lx[i],w[i][j]);
    }
    for(int x=1;x<=nx;x++)
    {
        for(int i=1;i<=ny;i++)
            slack[i]=INF;
        while(1)
        {
            memset(visx,0,sizeof(visx));
            memset(visy,0,sizeof(visy));
            if(dfs(x))
                break;
            int d=INF;
            for(int i=1;i<=ny;i++)
            {
                if(!visy[i]&&d>slack[i])
                    d=slack[i];
            }
            for(int i=1;i<=nx;i++)
            {
                if(visx[i])
                    lx[i]-=d;
            }
            for(int i=1;i<=ny;i++)
            {
                if(visy[i])
                    ly[i]+=d;
                else
                    slack[i]-=d;
            }
        }
    }
    int res=0;
    for(int i=1;i<=ny;i++)
        res+=w[link[i]][i];
    return res;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0)
            break;
        numi=0,numj=0;
        for(int i=0;i<n;i++)
                scanf("%s",s[i]);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(s[i][j]==‘m‘)
                {
                    for(int l=0;l<n;l++)
                    {
                        for(int k=0;k<m;k++)
                        {
                            if(s[l][k]==‘H‘)
                            {
                                w[numi+1][numj+1]=-(abs(i-l)+abs(j-k));
                                numj++;
                            }
                        }
                    }
                    numi++;
                    numj=0;
                }
            }
        }
        //printf("%d\n",numi);
        nx=ny=numi;
        int ans=km();
        printf("%d\n",-ans);
    }
    return 0;
}

  

(KM算法)hdu 1533

标签:

原文地址:http://www.cnblogs.com/a972290869/p/4350815.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!