zoukankan      html  css  js  c++  java
  • HDU 1533:Going Home(KM算法求二分图最小权匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=1533

    Going Home

    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
     
      1 #include <cstdio>
      2 #include <cstring>
      3 #include <algorithm>
      4 using namespace std;
      5 #define N 105
      6 #define INF 0x3f3f3f
      7 char maze[N][N];
      8 int mp[N][N],match[N],lx[N],ly[N],visx[N],visy[N],slack[N];
      9 int n,m,cnt;
     10 struct node
     11 {
     12     int a,b;
     13 }sa[N],sb[N];
     14 //KM求二分图最小匹配模板:只需把权值都变成负的,再用KM算出最大权匹配,然后取反就是答案
     15 //学习KM地址:http://blog.sina.com.cn/s/blog_691ce2b701016reh.html
     16 bool dfs(int x)
     17 {
     18     visx[x]=1;
     19     for(int y=1;y<=cnt;y++){
     20         if(visy[y]) continue;
     21         int t=lx[x]+ly[y]-mp[x][y];
     22         if(t==0){
     23             visy[y]=1;
     24             if(match[y]==-1||dfs(match[y])){
     25                 match[y]=x;
     26                 return true;
     27             }
     28         }
     29         else if(slack[y]>t) slack[y]=t;
     30     }
     31     return false;
     32 }
     33 
     34 int KM()
     35 {
     36     memset(match,-1,sizeof(match));
     37     memset(lx,-INF,sizeof(lx));
     38     memset(ly,0,sizeof(ly));
     39     for(int i=1;i<=cnt;i++){
     40         for(int j=1;j<=cnt;j++){
     41             if(mp[i][j]>lx[i]) lx[i]=mp[i][j];
     42         }
     43     }
     44     for(int i=1;i<=cnt;i++){
     45         for(int y=1;y<=cnt;y++)
     46             slack[y]=INF;
     47         while(1){
     48             memset(visx,0,sizeof(visx));
     49             memset(visy,0,sizeof(visy));
     50             if(dfs(i)) break;
     51             int d=INF;
     52             for(int y=1;y<=cnt;y++){
     53                 if(!visy[y]&&d>slack[y]) d=slack[y];
     54             }
     55             for(int x=1;x<=cnt;x++){
     56                 if(visx[x]) lx[x]-=d;
     57             }
     58             for(int y=1;y<=cnt;y++){
     59                 if(visy[y]) ly[y]+=d;
     60                 else slack[y]-=d;
     61             }
     62         }
     63     }
     64     int res=0;
     65     for(int i=1;i<=cnt;i++){
     66         if(match[i]>-1) res+=mp[match[i]][i];
     67     }
     68     return res;
     69 }
     70 
     71 int main()
     72 {
     73     int n,m;
     74     while(~scanf("%d%d",&n,&m)){
     75         if(n+m==0) break;
     76         for(int i=1;i<=n;i++){
     77             scanf("%s",maze[i]+1);
     78         }
     79         int cnt1=0,cnt2=0;
     80         for(int i=1;i<=n;i++){
     81             for(int j=1;j<=m;j++){
     82                 if(maze[i][j]=='m'){
     83                      sa[++cnt1].a=i;
     84                      sa[cnt1].b=j;
     85                 }
     86                 if(maze[i][j]=='H'){
     87                     sb[++cnt2].a=i;
     88                     sb[cnt2].b=j;
     89                 }
     90             }
     91         }
     92         cnt=cnt1;
     93         for(int i=1;i<=cnt1;i++){
     94             for(int j=1;j<=cnt2;j++){
     95                 mp[i][j]=abs(sa[i].a-sb[j].a)+abs(sa[i].b-sb[j].b);
     96                 mp[i][j]=-mp[i][j];
     97             }
     98         }
     99         printf("%d
    ",-KM());
    100     }
    101     return 0;
    102 }
  • 相关阅读:
    Android 富文本框实现 RichEditText
    mmap和普通文件读写的区别和比较 & mmap的注意点
    exit和_exit的区别
    绑定线程到特定CPU处理器
    软中断与硬中断 & 中断抢占 中断嵌套
    线程与信号处理
    内核信号处理 & CPU8个通用寄存器
    SIGSEGV 和 SIGBUS & gdb看汇编
    Linux内核态用户态相关知识 & 相互通信
    Linux进程空间分布 & 上下文
  • 原文地址:https://www.cnblogs.com/fightfordream/p/5663747.html
Copyright © 2011-2022 走看看