zoukankan      html  css  js  c++  java
  • HDU1533 Going Home

    HDU1533 Going Home

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


    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
     
    **********************************************************************************
    题目大意:给定一些人和一些房子,每个人每次只能向上下左右走一步,每个房子只能容纳一个人,求最后每个人都找到房子的需要的最少总步数
    解题思路:每个人到每个房子连一条边,边权是这两者的距离,在这个二分图中求出最小权匹配。
    没看模版,一次写成一次AC,哦也。
    #include <stdio.h>
    #include <string.h>
    #include <vector>
    #define N 105
    #define INF 0x3f3f3f3f
    #define ABS(a) ((a)>0?(a):-(a))
    using namespace std;
    
    int hx[N],hy[N],px[N],py[N];
    int gra[N][N];
    int visx[N],mat[N],visy[N];
    int n,m,hn,pn;
    int lx[N],ly[N];
    int lack;
    
    int dfs(int x)
    {
        visx[x]=1;
        int minn=INF;
        for(int i=1;i<=hn;i++)
        {
            if(visx[mat[i]])continue;
            if(lx[x]+ly[i]!=gra[x][i])
            {
                minn=min(lx[x]+ly[i]-gra[x][i],minn);
                continue;
            }
            visy[i]=1;
            if(!mat[i]||dfs(mat[i]))
            {
                mat[i]=x;
                return 1;
            }
        }
        lack=min(minn,lack);
        return 0;
    }
    
    void re(void)
    {
        hn=pn=0;
        for(int i=1;i<=n;i++)
        {
            char str[105];
            getchar();
            scanf("%s",str);
            for(int j=0;j<m;j++)
                if(str[j]=='H')
                    hx[++hn]=i,hy[hn]=j+1;
                else    if(str[j]=='m')
                    px[++pn]=i,py[pn]=j+1;
        }
    }
    
    void run(void)
    {
        memset(lx,-1,sizeof(lx));
        memset(ly,0,sizeof(ly));
        for(int i=1;i<=hn;i++)
            for(int j=1;j<=pn;j++)
            {
                gra[i][j]=1000-ABS(hx[i]-px[j])-ABS(hy[i]-py[j]);
                lx[i]=max(lx[i],gra[i][j]);
            }
        memset(mat,0,sizeof(mat));
        for(int i=1;i<=hn;i++)
        {
            memset(visx,0,sizeof(visx));
            memset(visy,0,sizeof(visy));
            lack=INF;
            while(!dfs(i))
            {
                for(int a=1;a<=hn;a++)
                    if(visx[a])
                        visx[a]=0,lx[a]-=lack;
                for(int b=1;b<=hn;b++)
                    if(visy[b])
                        visy[b]=0,ly[b]+=lack;
                lack=INF;
            }
        }
        int ans=0;
        for(int i=1;i<=hn;i++)
            ans+=1000-gra[mat[i]][i];
        printf("%d\n",ans);
    }
    
    int main()
    {
        while(scanf("%d%d",&n,&m),n+m)
        {
            re();
            run();
        }
        return 0;
    }
    

      

  • 相关阅读:
    CentOS7.5 防火墙指令
    监控Tomcat
    MySQL.ERROR 1133 (42000): Can't find any matching row in the user table
    Hadoop.之.入门部署
    Jenkins. 安装过程中出现一个错误: No such plugin: cloudbees-folder
    Spring.之.报错:Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException
    Spring.之.jar包官网下载
    Linux 安装Eclipse
    Linux SSH 免秘钥登录
    Linux rz命令无效
  • 原文地址:https://www.cnblogs.com/Fatedayt/p/2212956.html
Copyright © 2011-2022 走看看