• <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>
            posts - 18,  comments - 5,  trackbacks - 0
            一、題目描述

            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
            

            二、分析
                  一個(gè)簡(jiǎn)單的最優(yōu)匹配問題,但要注意它要求的是最小權(quán)匹配,可以改成最大權(quán)匹配(第57行)用KM算法,詳細(xì)算法:匹配問題
            三、代碼
             1#include<iostream>
             2#include<cmath>
             3using namespace std;
             4int n, m;
             5char str[101];
             6int hc, home[100][2];
             7int mc, man[100][2];
             8int map[100][100];
             9int lx[100], ly[100];
            10int mat[100];
            11bool vx[100], vy[100];
            12int  slack[100];
            13bool dfs(int u)
            14{
            15    vx[u] = true;
            16    for(int v=0; v<mc; v++)
            17    {
            18        if(!vy[v] && lx[u]+ly[v] == map[u][v])
            19        {
            20            vy[v] = true;
            21            if(mat[v]==-1 || dfs(mat[v]))
            22            {
            23                mat[v] = u;
            24                return true;
            25            }

            26        }

            27        else if(lx[u]+ly[v] > map[u][v])
            28            slack[v] = min(slack[v], lx[u] + ly[v] - map[u][v]);
            29    }

            30    return false;
            31}

            32int main()
            33{
            34    while(1)
            35    {
            36        scanf("%d%d"&n, &m);
            37        if(n==0 && m==0)
            38            break;
            39        hc = mc = 0;
            40        for(int i=0; i<n; i++)
            41        {
            42            scanf("%s", str);
            43            for(int j=0; j<m; j++)
            44            {
            45                if(str[j] == 'H')
            46                {
            47                    home[hc][0= i; home[hc++][1= j;
            48                }

            49                else if(str[j] == 'm')
            50                {
            51                    man[mc][0= i; man[mc++][1= j;
            52                }

            53            }

            54        }

            55        for(int i=0; i<mc; i++)
            56            for(int j=0; j<mc; j++)
            57                map[i][j] = 200 - abs(man[i][0]-home[j][0]) - abs(man[i][1]-home[j][1]);
            58        memset(lx, 0sizeof(lx));
            59        memset(ly, 0sizeof(ly));
            60        for(int u=0; u<mc; u++)
            61            for(int v=0; v<hc; v++)
            62                lx[u] = max(lx[u], map[u][v]);
            63        memset(mat, -1sizeof(mat));
            64        for(int u=0; u<mc; u++)
            65        {
            66            while(1)
            67            {
            68                memset(vx, 0sizeof(vx));
            69                memset(vy, 0sizeof(vy));
            70                for(int j=0; j<mc; j++)
            71                    slack[j] = INT_MAX;
            72                if(dfs(u))
            73                    break;
            74                int al = INT_MAX;
            75                for(int i=0; i<mc; i++)
            76                    if(!vy[i])
            77                        al = min(al, slack[i]);
            78                for(int i=0; i<mc; i++)
            79                {
            80                    if(vx[i]) lx[i] -= al;
            81                    if(vy[i]) ly[i] += al;
            82                }

            83            }

            84        }

            85        int res = 0;
            86        for(int v=0; v<mc; v++)
            87            res += 200 - map[mat[v]][v];
            88        printf("%d\n", res);
            89    }

            90}
            posted on 2009-06-29 14:42 Icyflame 閱讀(678) 評(píng)論(0)  編輯 收藏 引用 所屬分類: 解題報(bào)告
            国内精品九九久久精品| 狠狠色丁香久久婷婷综合_中| 无码国内精品久久人妻蜜桃| 国色天香久久久久久久小说| 久久久一本精品99久久精品88| 亚洲国产精品久久久久| 伊人色综合久久天天人守人婷| 久久精品国产99久久久| 久久99热这里只有精品国产 | 狠色狠色狠狠色综合久久| 久久久久亚洲精品天堂久久久久久 | 久久久亚洲欧洲日产国码aⅴ| 99久久精品国产免看国产一区| 久久精品亚洲欧美日韩久久| 久久影院综合精品| 四虎国产精品成人免费久久| 日本免费久久久久久久网站| 亚洲人成伊人成综合网久久久| 久久久久九九精品影院| 国产69精品久久久久777| 久久狠狠爱亚洲综合影院| 亚洲午夜无码久久久久小说| 91秦先生久久久久久久| 狠狠色噜噜狠狠狠狠狠色综合久久| 久久天天躁夜夜躁狠狠| 亚洲美日韩Av中文字幕无码久久久妻妇 | 久久人妻少妇嫩草AV无码蜜桃| 久久99亚洲网美利坚合众国| 久久精品国产亚洲av麻豆蜜芽| 色偷偷88欧美精品久久久| 国产精品久久久久久久久久免费| 久久久久亚洲Av无码专| 亚洲精品无码专区久久久| 久久天天躁夜夜躁狠狠| 久久国内免费视频| 久久人妻无码中文字幕| 久久久久av无码免费网| 77777亚洲午夜久久多喷| 久久亚洲私人国产精品vA | 精品久久久久久久久久中文字幕 | 四虎国产精品免费久久|