zoukankan      html  css  js  c++  java
  • POJ 2195 Going Home

    http://poj.org/problem?id=2195

    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

    代码:

    #include <iostream>
    #include <string.h>
    #include <algorithm>
    #include <stdio.h>
    using namespace std;
    
    const int inf = 0x3f3f3f3f;
    const int maxn = 1010;
    int N, M;
    int l1[maxn], l2[maxn], s[maxn], t[maxn];
    int match1[maxn], match2[maxn];
    int mp[maxn][maxn];
    char maze[maxn][maxn];
    int nx = 0, ny = 0;
    
    struct Node {
        int x;
        int y;
    }nodex[maxn], nodey[maxn];
    
    int KM(int M, int N) {
        int p, q, k, i, j;
        int res = 0;
        for( i = 0; i < M; i ++) {
            l1[i] = -inf;
            for( j = 0; j < N; j ++)
                if(mp[i][j] > l1[i])
                    l1[i] = mp[i][j];
            if(l1[i] == -inf) return -1;
        }
        memset(l2, 0, sizeof(l2));
        memset(match1, -1, sizeof(match1));
        memset(match2, -1, sizeof(match2));
        for( i = 0; i < M; i ++) {
            memset(t, -1, sizeof(t));
            for(s[p = q = 0] = i; p <= q && match1[i] < 0; p ++) {
                for(k = s[p], j = 0; j < N && match1[i] < 0; j ++) {
                    if(l1[k] + l2[j] == mp[k][j] && t[j] < 0) {
                        s[++ q] = match2[j], t[j] = k;
                        if(s[q] < 0)
                            for(p = j; p >= 0; j = p)
                                match2[j] = k = t[j], p = match1[k], match1[k] = j;
                    }
                }
            }
            if(match1[i] < 0) {
                for(i --, p = inf, k = 0; k <= q; k ++)
                    for( j = 0; j < N; j ++)
                    if(t[j] < 0 && l1[s[k]] + l2[j] - mp[s[k]][j] < p)
                        p = l1[s[k]] + l2[j] - mp[s[k]][j];
    
                for( j = 0; j < N; j ++)
                if(t[j] > 0) l2[j] += p;
                for(k = 0; k <= q; k ++)
                    l1[s[k]] -= p;
            }
        }
        for( i = 0; i <M; i ++) {
            if(match1[i] < 0) return -1;
            if(mp[i][match1[i]] <= -inf) return -1;
            res += mp[i][match1[i]];
        }
        return res;
    }
    
    int main() {
        while(~scanf("%d%d", &N, &M)) {
            nx = 0, ny = 0;
            memset(mp, 0, sizeof(mp));
            if(!N && !M) break;
            for(int i = 0; i < N; i ++)
                scanf("%s", maze[i]);
    
            for(int i = 0; i < N; i ++) {
                for(int j = 0; j < M; j ++) {
                    if(maze[i][j] == 'm') {
                        nodex[nx].x = i;
                        nodex[nx].y = j;
                        nx ++;
                    } else if(maze[i][j] == 'H') {
                        nodey[ny].x = i;
                        nodey[ny].y = j;
                        ny ++;
                    }
                }
            }
    
            for(int i = 0; i < nx; i ++) {
                for(int j = 0; j < ny; j ++) {
                    mp[i][j] = -abs(nodex[i].x - nodey[j].x) - abs(nodex[i].y - nodey[j].y);
                }
            }
            printf("%d
    ", -KM(nx, ny));
        }
        return 0;
    }
    

      KM 求最小权匹配

    刚刚结束携程的笔试 最后一道编程题看着乱糟糟 样例打表居然能过 85.7% 数据可以说是很水了

  • 相关阅读:
    完整的CRUD——javaweb
    JDBC预编译语句表名占位异常
    自定义标签2.x
    实现一个自定义<table>标签
    带动态属性的自定义标签
    自定义标签库_tag
    poj 3783
    The North American Invitational Programming Contest 2018 E. Prefix Free Code
    hdu 6383
    ACM-ICPC 2016 Qingdao Preliminary Contest G. Sort
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/10673255.html
Copyright © 2011-2022 走看看