HDU1533-KM

Going Home

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


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的地图,有人和房子,每个人到房子的代价是曼哈顿距离即xy轴距离差的绝对值的和,问你所有人都住到房子里的总的代价最少是对少,每个房子只能住一个人


题目思路:

                更具地图建一个人和房子的二分图权值为曼哈顿距离,然后就转换成了二分图带权问题,然后就是跑一遍KM算法就是这里要求的是最短所有我们可以把权值取反,求出来的答案取反就是我们要求的最小值


AC代码:


#include<cstring>
#include<cstdio>
#define min(x,y) (x<y?x:y)
#define max(x,y) (x>y?x:y)
#define abs(x)  ((x)<0?-(x):(x))
const int maxn = 1e2+20;
const int inf = 0x3f3f3f3f;

class KM{
public:
    int n,m,numh,numm;
    int ph[maxn][2],pm[maxn][2];
    int mp[maxn][maxn],slack[maxn],link[maxn],ex_l[maxn],ex_r[maxn];
    bool vis_l[maxn],vis_r[maxn];

    bool dfs(int u){
        vis_l[u]=true;
        for(int i=1;i<=n;i++){
            if(!vis_r[i]){
                if(ex_l[u]+ex_r[i]==mp[u][i]){
                    vis_r[i]=true;
                    if(link[i]==-1||dfs(link[i])){
                        link[i]=u;
                        return true;
                    }
                }else {
                    slack[i]=min(slack[i],ex_l[u]+ex_r[i]-mp[u][i]);
                }
            }
        }
        return false;
    }

    int sove(){
        for(int i=1;i<=n;i++){
            memset(slack,0x3f,sizeof(slack));
            while(1){
                memset(vis_l,false,sizeof(vis_l));
                memset(vis_r,false,sizeof(vis_r));
                if(dfs(i))break;
                int d = inf;
                for(int j=1;j<=n;j++){
                    if(!vis_r[j])d=min(d,slack[j]);
                }
                for(int j=1;j<=n;j++){
                    if(vis_l[j])ex_l[j]-=d;
                    if(vis_r[j])ex_r[j]+=d;
                    else slack[j]-=d;
                }
            }
        }
        int res = 0;
        for(int i=1;i<=n;i++){
            if(link[i]!=-1){
                res+=mp[link[i]][i];
            }
        }
        return res;
    }

    void star(){
        while(~scanf("%d%d",&n,&m),n+m){
            init();
            char str[maxn];
            for(int i=1;i<=n;i++){
                scanf("%s",str+1);
                for(int j=1;j<=m;j++){
                    if(str[j]=='H')ph[++numh][0]=i,ph[numh][1]=j;
                    if(str[j]=='m')pm[++numm][0]=i,pm[numm][1]=j;
                }
            }
            n=numh;
            for(int i=1;i<=n;i++){
                for(int j=1;j<=n;j++){
                    int x = abs(pm[i][0]-ph[j][0])+abs(pm[i][1]-ph[j][1]);
                    mp[i][j]=-x;
                    ex_l[i]=max(ex_l[i],mp[i][j]);
                }
            }
            printf("%d\n",-sove());
        }
    }

    void init(){
        memset(link,-1,sizeof(link));
        memset(ex_r,0,sizeof(ex_r));
        memset(ex_l,-0x3f,sizeof(ex_l));
        numh=numm=0;
    }

}km;

int main()
{
    km.star();
    return 0;
}






全部评论

相关推荐

程序员花海:实习和校招简历正确格式应该是教育背景+实习+项目经历+个人评价 其中项目经历注意要体现业务 实习经历里面的业务更是要自圆其说 简历模板尽可能保持干净整洁 不要太花哨的
点赞 评论 收藏
分享
2025-11-13 20:16
已编辑
厦门理工学院 软件测试
专业嗎喽:硕佬,把学校背景放后面几段,学校背景双非还学院,让人看了就不想往下看。 把实习经历和个人奖项放前面,用数字化简述自己实习的成果和掌握的技能,比如负责项目一次通过率90%,曾4次发现项目潜在问题风险为公司减少损失等等
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务