• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            POJ 2195 Going Home 二分圖完美匹配

            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


                題目大意:有m個(gè)人要進(jìn)h間房子,從當(dāng)前位置(x1,y1)進(jìn)入房子(x2,y2)的時(shí)間為|x1-x2|+|y1-y2|,問(wèn)這m個(gè)人都進(jìn)入房間所需的最小時(shí)間是多少。問(wèn)題可以轉(zhuǎn)化為帶權(quán)二分圖的最小權(quán)匹配,以sample 2為例先建立二分圖:
             
            (m1,h1)=4,(m1,h2)=3,(m1,h3)=4,(m2,h1)=4,(m2,h2)=5,(m2,h3)=4,(m3,h1)=5,(m3,h2)=4,(m3,h3)=3.
            然后用KM算法求解,代碼中的注釋部分為最大權(quán)匹配。
            #include <iostream>

            const int MAX = 101;
            const int MAXN = 10001;
            const int inf = 0x7FFFFFFF;
            struct point{
                
            int x,y;
            }
            man[MAXN],home[MAXN];
            bool vx[MAX],vy[MAX];
            int m,h,map[MAX][MAXN],lx[MAX],ly[MAX],match[MAX];

            bool dfs(int u){
                
            int i;
                
            for(vx[u]=true,i=0;i<h;i++)
                    
            if(!vy[i] && lx[u]+ly[i]==map[u][i]){
                        vy[i]
            =true;
                        
            if(match[i]==-1 || dfs(match[i])){
                            match[i]
            =u;
                            
            return true;
                        }

                    }

                
            return false;
            }

            int kuhn_munkras(){
                
            int i,j,k,min,ans;
                
            for(i=0;i<m;i++)
                    
            for(lx[i]=inf,j=0;j<h;j++)
                        
            if(map[i][j]<lx[i]) lx[i]=map[i][j];
              
            //for(i=0;i<m;i++)
              
            //    for(lx[i]=-inf,j=0;j<h;j++)
              
            //        if(map[i][j]>lx[i]) lx[i]=map[i][j]; 最大權(quán)匹配
                for(i=0;i<h;i++) ly[i]=0;
                memset(match,
            -1,sizeof(match));
                
            for(i=0;i<m;i++){
                    
            while(true){
                        memset(vx,
            false,sizeof(vx));
                        memset(vy,
            false,sizeof(vy));
                        min
            =inf;
                        
            if(dfs(i)) break;
                        
            for(j=0;j<m;j++){
                            
            if(vx[j]){
                                
            for(k=0;k<h;k++)
                                    
            if(!vy[k] && map[j][k]-lx[j]-ly[k]<min)
                                        min
            =map[j][k]-lx[j]-ly[k];
                                  
            //if(!vy[k] && lx[j]+ly[k]-map[j][k]<min)
                                  
            //    min=map[j][k]-lx[j]-ly[k]; 最大權(quán)匹配
                            }

                        }

                        
            for(j=0;j<m;j++if(vx[j]) lx[j]+=min;
                        
            for(j=0;j<h;j++if(vy[j]) ly[j]-=min;
                    }

                }

                
            for(ans=i=0;i<h;i++) ans+=map[match[i]][i];
                
            return ans;
            }

            int main(){
                
            char ch;
                
            int i,j,row,colum;
                
            while(scanf("%d %d",&row,&colum),row||colum){
                    
            for(getchar(),m=h=i=0;i<row;i++){
                        
            for(j=0;j<colum;j++){
                            ch
            =getchar();
                            
            if(ch=='m')
                                man[m].x
            =i,man[m].y=j,m++;
                            
            else if(ch=='H')
                                home[h].x
            =i,home[h].y=j,h++;
                        }

                        getchar();
                    }

                    memset(map,
            0,sizeof(map));
                    
            for(i=0;i<m;i++)
                        
            for(j=0;j<h;j++)
                            map[i][j]
            =abs(man[i].x-home[j].x)+abs(man[i].y-home[j].y);
                    printf(
            "%d\n",kuhn_munkras());
                }

                
            return 0;
            }

            posted on 2009-06-03 12:45 極限定律 閱讀(809) 評(píng)論(0)  編輯 收藏 引用 所屬分類: ACM/ICPC

            <2009年5月>
            262728293012
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(10)

            隨筆分類

            隨筆檔案

            友情鏈接

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            2021国产精品午夜久久| 久久不射电影网| 亚洲国产精品无码久久久秋霞2| 亚洲AV日韩精品久久久久久久| 国产亚洲综合久久系列| 国产精品久久久天天影视香蕉| 怡红院日本一道日本久久| 亚洲国产小视频精品久久久三级 | 久久久久国产一级毛片高清板| 人妻中文久久久久| 97久久精品人妻人人搡人人玩| 久久精品人妻中文系列| 精品久久一区二区三区| 亚洲国产精品无码久久九九| 久久精品毛片免费观看| 亚洲国产婷婷香蕉久久久久久| www.久久热| 亚洲国产另类久久久精品黑人| 免费精品久久久久久中文字幕| 久久99国产精一区二区三区| 久久久SS麻豆欧美国产日韩| 久久九九久精品国产| 久久国产精品-久久精品| 久久人妻无码中文字幕| 女同久久| 精品无码人妻久久久久久| 国产亚洲色婷婷久久99精品| 久久人人爽人人爽人人片AV高清 | 国产激情久久久久久熟女老人| 国产激情久久久久影院小草| 久久精品国产精品青草app| 国内精品九九久久精品| 无码人妻久久一区二区三区蜜桃| 丁香久久婷婷国产午夜视频| 久久国产精品99久久久久久老狼| 少妇久久久久久被弄高潮| 亚洲精品乱码久久久久久中文字幕| 亚洲国产成人精品女人久久久| 99久久99久久精品国产片| 国内精品欧美久久精品| 99热热久久这里只有精品68|