zoukankan      html  css  js  c++  java
  • POJ 2195 Going Home 【二分图最小权值匹配】

    传送门:http://poj.org/problem?id=2195

    Going Home
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 26151   Accepted: 13117

    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

    题意概括:

    给一个长为 H 宽为 W 的地图,地图上 m 代表人, H 代表家,求所有人回到家的最小权值之和。(人到家的距离为 哈密顿距离)

    解题思路:

    拆点建二分图,距离反转,KM算法求二分图的最大权值匹配,那么去掉负号最大的就是最小的权值之和了。

    AC code:

      1 #include <cstdio>
      2 #include <iostream>
      3 #include <algorithm>
      4 #include <cstring>
      5 #define INF 0x3f3f3f3f
      6 using namespace std;
      7 const int MAXN = 110;
      8 
      9 char str[MAXN][MAXN];
     10 int c[MAXN][MAXN];
     11 int ex[MAXN], ey[MAXN];
     12 bool visx[MAXN], visy[MAXN];
     13 int match[MAXN];
     14 int HH, WW;
     15 int numx, numy;
     16 
     17 struct date
     18 {
     19     int x, y;
     20 }M[MAXN], H[MAXN];
     21 
     22 bool dfs(int x)
     23 {
     24     visx[x] = true;
     25 
     26     for(int y = 0; y < numx; y++){
     27         if(!visy[y] && ex[x]+ey[y]-c[x][y] == 0){
     28             visy[y] = true;
     29 
     30             if(match[y] == -1 || dfs(match[y])){
     31                 match[y] = x;
     32                 return true;
     33             }
     34         }
     35     }
     36     return false;
     37 }
     38 
     39 void KM()
     40 {
     41     memset(ey, 0, sizeof(ey));
     42     memset(match, -1, sizeof(match));
     43 
     44     for(int i = 0; i < numx; i++){
     45         ex[i] = c[i][0];
     46         for(int j = 1; j < numx; j++){
     47             if(c[i][j] > ex[i])
     48                 ex[i] = c[i][j];
     49         }
     50     }
     51 
     52     for(int i = 0; i < numx; i++){
     53         while(1){
     54             memset(visx, 0, sizeof(visx));
     55             memset(visy, 0, sizeof(visy));
     56 
     57             if(dfs(i)) break;
     58 
     59             int d = INF;
     60             for(int j = 0; j < numx; j++){
     61                 if(visx[j]){
     62                     for(int k = 0; k < numx; k++){
     63                         if(!visy[k] && ex[j]+ey[k]-c[j][k] < d)
     64                             d = ex[j] + ey[k] - c[j][k];
     65                     }
     66                 }
     67             }
     68 
     69             for(int j = 0; j < numx; j++){
     70                 if(visx[j]) ex[j]-=d;
     71                 if(visy[j]) ey[j]+=d;
     72             }
     73         }
     74     }
     75 
     76     int res = 0;
     77     for(int i = 0; i <numx; i++){
     78         res+=c[match[i]][i];
     79     }
     80     printf("%d
    ", -res);
     81 }
     82 
     83 int main()
     84 {
     85     while(~scanf("%d %d", &HH, &WW) && (HH+WW)){
     86         numx = numy = 0;
     87         for(int i = 0; i < HH; i++){
     88             scanf("%s", &str[i]);
     89 
     90             for(int j = 0; j < WW; j++){
     91                 if(str[i][j] == 'm'){
     92                     M[numx].x = i;
     93                     M[numx++].y = j;
     94                 }
     95                 else if(str[i][j] == 'H'){
     96                     H[numy].x = i;
     97                     H[numy++].y = j;
     98                 }
     99             }
    100         }
    101 
    102         for(int i = 0; i < numx; i++){
    103             for(int j = 0; j < numx; j++)
    104                 c[i][j] = -(abs(M[i].x - H[j].x) + abs(M[i].y - H[j].y));
    105         }
    106         KM();
    107     }
    108     return 0;
    109 }
    View Code
  • 相关阅读:
    Hibernate 查询,返回结果设置到DTO
    sqlserver计算时间差DATEDIFF 函数
    SQL语句 不足位数补0
    Redis详细用法
    windows下安装Redis并部署成服务
    Redis命令
    ajax请求在参数中添加时间戳
    JS获取子节点、父节点和兄弟节点的方法实例总结
    js关闭当前页面清除session
    Java面试题一览
  • 原文地址:https://www.cnblogs.com/ymzjj/p/10004591.html
Copyright © 2011-2022 走看看