zoukankan      html  css  js  c++  java
  • hdu 1533 Going Home(KM算法求最大权匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533

    我的链接:http://acm.hust.edu.cn:8080/judge/contest/view.action?cid=17728#problem/B

    Going Home

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


    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
     
    Recommend
    lwg
     
    思路将m的坐标记录到左集,h的坐标记录到右集,w[i][j]表示第i个m到第j个h的距离
              w[i][j]=△x+△y 然后因为是求最小值,而KM是求最大值
              所以只要这样:w[i][j] = -w[i][j]建图再套模板输出【-sum】就ok!

    PS:思路看的网上牛人的,初学KM,代码效率较低。

    //Accepted	348 KB	62 ms	C++	2069 B
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int maxn=110;
    char map[maxn][maxn];
    int w[maxn][maxn];
    bool s[maxn],t[maxn];
    int match[maxn];
    int lx[maxn],ly[maxn];
    int n;
    bool hungary(int u){
         s[u]=true;
         for(int v=1;v<=n;v++){
             if(!t[v] && lx[u]+ly[v]==w[u][v]){
                t[v]=true;//易遗忘 
                if(match[v]==-1 || hungary(match[v])){
                   match[v]=u;
                   return true;
                }
             }
         }
         return false;
    }
    int KM(){
        int ans=0;
        memset(match,-1,sizeof(match));
        for(int i=1;i<=n;i++){
        	lx[i]=-1<<30;//注意 
        }
        memset(ly,0,sizeof(ly));
        for(int i=1;i<=n;i++)
           for(int j=1;j<=n;j++)
              lx[i]=max(lx[i],w[i][j]);
        for(int i=1;i<=n;i++){
            while(1){
               memset(s,false,sizeof(s));
               memset(t,false,sizeof(t));
               if(hungary(i)) break;
               else{
                    int a=1<<30;
                    for(int j=1;j<=n;j++) if(s[j]){
                        for(int k=1;k<=n;k++) if(!t[k] && a>lx[j]+ly[k]-w[j][k])
                            a=lx[j]+ly[k]-w[j][k];
                    }
                    for(int j=1;j<=n;j++){
                       if(s[j]) lx[j]-=a;
                       if(t[j]) ly[j]+=a;
                    }
               }
            }
        }
        for(int i=1;i<=n;i++) ans+=w[match[i]][i];
        return -ans;//易遗忘 
    }
    int main(){
    	int row,col,numi,numj;
        while(scanf("%d%d%*c",&row,&col)!=EOF){
        	 if(row==0 || col==0) break;
        	 n=numi=numj=0;
             for(int i=1;i<=row;i++){
             	for(int j=1;j<=col;j++){
    	         	scanf("%c",&map[i][j]);
    	         	if(map[i][j]=='m') n++;
    	         }
    	         getchar();
             }
             for(int i=1;i<=row;i++){
             	for(int j=1;j<=col;j++){
    	         	if(map[i][j]=='m'){
    	         		numi++;numj=1;
    	         		for(int k=1;k<=row;k++){
    		         		for(int e=1;e<=col;e++){
    		         			if(map[k][e]=='H')
    		         			w[numi][numj++]=-(abs(i-k)+abs(j-e));
    		         		}
    		         	}
    	         	}
    	         }
             }
             printf("%d\n",KM());
        }
        return 0;
    }



  • 相关阅读:
    关于springMVC转换json出现的异常
    jQuery实现,动态自动定位弹窗。JS分页,Ajax请求
    servlet为什么要配置web.xml
    Jmeter系列(4)- Jmeter 脚本录制
    后缀数组模板
    NOIP2016 玩脱记
    TERSUS无代码开发(笔记21)-流程执行顺序思考(转载)
    ===>===>===>特色思TERSUS常用功能整理
    TERSUS无代码开发(笔记20)-本地开发测试mysql数据库连接
    TERSUS无代码开发(笔记19)-mysql-connector-java-5.-bin.jar下载方法
  • 原文地址:https://www.cnblogs.com/freezhan/p/2950443.html
Copyright © 2011-2022 走看看