zoukankan      html  css  js  c++  java
  • Poj(2195),最小费用流,SPFA

    题目链接:http://poj.org/problem?id=2195

    Going Home
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 21530   Accepted: 10871

    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

     
    听阳哥说,可以用二分图得最大匹配就可以出来,还没学,用的是书上的SPFA,然后改了半天没改好。2333
    思路:构造一个源点,一个汇点01,下面的点是2,3... ... 把每条路的花费作为最短路,用SPFA,找到一条最短路,更新残余网络。
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <queue>
    #include <algorithm>
    
    using namespace std;
    
    #define MAXN 205
    #define INF 20000
    
    bool vis[MAXN];
    int cnt, cnt_h, cnt_m, result;
    int d[MAXN], pre[MAXN], cost[MAXN][MAXN], cap[MAXN][MAXN];
    
    struct node
    {
        int x, y;
    } hos[MAXN], man[MAXN];
    
    int step(int i, int j)
    {
        return (int)fabs((man[i].x-hos[j].x)*1.0) + fabs((man[i].y-hos[j].y)*1.0);
    }
    void make_map()
    {
        int i, j;
        memset(cap, 0, sizeof(cap));
        for (i = 0; i < cnt_m; i++)
        {
            cap[0][i+2] = 1;
            cost[0][i+2] = 0;
        }
        for (i = 0; i < cnt_h; i++)
        {
            cap[cnt_m+i+2][1] = 1;
            cost[cnt_m+i+2][1] = 0;
        }
        for (i = 0; i < cnt_m; i++)
            for (j = 0; j < cnt_h; j++)
            {
                cap[i+2][cnt_m+j+2] = 1;
                cost[i+2][cnt_m+j+2] = step(i, j);
                cost[cnt_m+j+2][i+2] = -cost[i+2][cnt_m+j+2];
            }
    }
    
    bool spfa()
    {
        int i, u;
        for (i = 1; i <= cnt; i++)
        {
            d[i] = INF;
            vis[i] = false;
        }
        d[0] = 0;
        queue <int> q;
        q.push(0);
        while (!q.empty())
        {
            u = q.front();
            q.pop();
            vis[u] = true;
            for (i = 1; i <= cnt; i++)
                if (cap[u][i] && d[i] > d[u] + cost[u][i])
                {
                    d[i] = d[u] + cost[u][i];
                    pre[i] = u;
                    if (!vis[i])
                    {
                        vis[i] = true;
                        q.push(i);
                    }
                }
            vis[u] = false;
        }
        if (d[1] < INF)
            return true;
        return false;
    }
    
    int main()
    {
        char c;
        int i, j, n, m;
        while (scanf("%d%d", &n, &m), n && m)
        {
            cnt_h = cnt_m = 0;
            for (i = 0; i < n; i++)
                for (j = 0; j < m; j++)
                {
                    scanf(" %c", &c);
                    if (c == 'H')
                    {
                        hos[cnt_h].x = i;
                        hos[cnt_h].y = j;
                        cnt_h++;
                    }
                    else if (c == 'm')
                    {
                        man[cnt_m].x = i;
                        man[cnt_m].y = j;
                        cnt_m++;
                    }
                }
            cnt = cnt_h + cnt_m + 1;
            make_map();
            result = 0;
            while (spfa())
            {
                int i, cf;
                cf = INF;
                for (i = 1; i != 0; i = pre[i])
                    cf = min(cf, cap[pre[i]][i]);
                for (i = 1; i != 0; i = pre[i])
                {
                    cap[pre[i]][i] -= cf;
                    cap[i][pre[i]] += cf;
                    result += cost[pre[i]][i] * cf;
                }
            }
            printf("%d
    ", result);
        }
        return 0;
    }
     
  • 相关阅读:
    uva10912 Simple Minded Hashing(DP)
    uva10401 Injured Queen Problem(DP)
    uva702 The Vindictive Coach(DP)
    忍者X4将采取自动开通vip,论坛充值淘宝自助购买均可。步骤如下
    C盘不够大,可以这样操作
    任务思维1
    PHP 获取指定日期的星期方法如下
    学学C#开发client,server,C/S架构的程序
    今天的主角就是protobuf-net
    关于忍者站群X4-小飞镖服务器配置帮助汇总。
  • 原文地址:https://www.cnblogs.com/TreeDream/p/5755369.html
Copyright © 2011-2022 走看看