HDU 1533 Going Home

Going Home

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


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
 
题目大意:给出一个n*m的地图,H表示房子,m表示人,一个房子只能住一个人,人每走一格就要花1块钱,问所有人都住进房子的最小花费

用坐标见图,然后建负权边,跑一遍km算法

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
const int maxn=310;
const int inf=0x3f3f3f3f;
int lena,lenb;
int map[maxn][maxn];
int match[maxn];
int slack[maxn];
int ex_boy[maxn];
int ex_girl[maxn];
bool vis_boy[maxn];
bool vis_girl[maxn];
struct Node
{
    int x,y;
    Node(){}
    Node(int x,int y)
    {
        this->x=x;
        this->y=y;
    }
}home[maxn],man[maxn];
int hn,mn;
int dis(Node a,Node b)
{
    return (abs(a.x-b.x)+abs(a.y-b.y));
}
bool dfs(int node)
{
    vis_girl[node]=true;
    for(int i=1;i<=hn;i++)
    {
        if(vis_boy[i]) continue;
        int gap=ex_boy[i]+ex_girl[node]-map[node][i];
        if(!gap)
        {
            vis_boy[i]=true;
            if(match[i]==-1||dfs(match[i]))
            {
                match[i]=node;
                return true;
            }
        }
        else
        {
            slack[i]=min(slack[i],gap);
        }
    }
    return false;
}
int km()
{
    memset(ex_boy,0,sizeof(ex_boy));
    memset(match,-1,sizeof(match));
    for(int i=1;i<=mn;i++)
    {
        ex_girl[i]=-inf;
        for(int j=1;j<=hn;j++)
        {
            ex_girl[i]=max(ex_girl[i],map[i][j]);
        }
    }
    for(int i=1;i<=mn;i++)
    {
        memset(slack,inf,sizeof(slack));
        while(true)
        {
            memset(vis_girl,false,sizeof(vis_girl));
            memset(vis_boy,false,sizeof(vis_boy));
            if(dfs(i))
            {
                break;
            }
            int d=inf;
            for(int j=1;j<=hn;j++)
            {
                if(!vis_boy[j])
                {
                    d=min(d,slack[j]);
                }
            }
            for(int j=1;j<=mn;j++)
            {
                if(vis_girl[j]) 
                {
                    ex_girl[j]-=d;
                }
            }
            for(int j=1;j<=hn;j++)
            {
                if(vis_boy[j]) 
                {
                    ex_boy[j]+=d;
                }
                else
                {
                    slack[j]-=d;
                }
            }
        }
    }
    int ans=0;
    for(int i=1;i<=hn;i++)
    {
        if(match[i]>-1)
        { 
            ans+=map[match[i]][i];
        }
    }
    return ans;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        getchar();
        if(!n&&!m) break;
        hn=mn=0;
        char ch;
        for(int i=0;i<n;i++)
        {
            //scanf("%s",ch);
            for(int j=0;j<m;j++)
            {
                //scanf("%c",&ch);
                cin>>ch;
                if(ch=='m')
                {
                    man[++mn]=Node(i,j);
                }
                else if(ch=='H')
                {
                    home[++hn]=Node(i,j);
                }
            }
        }
        for(int i=1;i<=hn;i++)
        {
            for(int j=1;j<=mn;j++)
            {
                map[i][j]=-dis(home[i],man[j]);
            }
        }
        printf("%d\n",-km());
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37943488/article/details/80530299
今日推荐