• Going Home POJ


    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 <queue>
      4 #include <cmath>
      5 #include <algorithm>
      6 #include <set>
      7 #include <iostream>
      8 #include <map>
      9 #include <stack>
     10 #include <string>
     11 #include <vector>
     12 #define pi acos(-1.0)
     13 #define eps 1e-6
     14 #define fi first
     15 #define se second
     16 #define lson l,m,rt<<1
     17 #define rson m+1,r,rt<<1|1
     18 #define bug         printf("******
    ")
     19 #define mem(a,b)    memset(a,b,sizeof(a))
     20 #define fuck(x)     cout<<"["<<x<<"]"<<endl
     21 #define f(a)        a*a
     22 #define sf(n)       scanf("%d", &n)
     23 #define sff(a,b)    scanf("%d %d", &a, &b)
     24 #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
     25 #define pf          printf
     26 #define FRE(i,a,b)  for(i = a; i <= b; i++)
     27 #define FREE(i,a,b) for(i = a; i >= b; i--)
     28 #define FRL(i,a,b)  for(i = a; i < b; i++)
     29 #define FRLL(i,a,b) for(i = a; i > b; i--)
     30 #define FIN freopen("DATA.txt","r",stdin)
     31 #define lowbit(x)   x&-x
     32 #pragma comment (linker,"/STACK:102400000,102400000")
     33 
     34 using namespace std;
     35 const int maxn = 1e5 + 10;
     36 typedef long long LL;
     37 const int MX = 505;
     38 const int inf = 0x3f3f3f3f;
     39 const int MXE = MX * MX * 4;
     40 struct MinCost_MaxFlow {
     41     struct Edge {
     42         int v, w, nxt;
     43         int cost;
     44     } E[MXE];
     45     int head[MX], tot, level[MX], pre[MX], d[MX];
     46     bool vis[MX];
     47     void init() {
     48         memset(head, -1, sizeof(head));
     49         tot = 0;
     50     }
     51     void add(int u, int v, int w, int cost) {
     52         E[tot].v = v;
     53         E[tot].w = w;
     54         E[tot].cost = cost;
     55         E[tot].nxt = head[u];
     56         head[u] = tot++;
     57         E[tot].v = u;
     58         E[tot].w = 0;
     59         E[tot].cost = -cost;
     60         E[tot].nxt = head[v];
     61         head[v] = tot++;
     62     }
     63     bool spfa(int s, int t) {
     64         memset(vis, 0, sizeof(vis));
     65         memset(d, 0x3f, sizeof(d));
     66         memset(pre, -1, sizeof(pre));
     67         queue<int>q;
     68         q.push(s);
     69         d[s] = 0;
     70         vis[s] = 1;
     71         while (!q.empty()) {
     72             int u = q.front();
     73             q.pop();
     74             vis[u] = 0;
     75             for (int i = head[u]; ~i; i = E[i].nxt) {
     76                 int w = E[i].w, v = E[i].v, cost = E[i].cost;
     77                 if (w > 0 && d[v] > d[u] + cost) {
     78                     d[v] = d[u] + cost;
     79                     pre[v] = i;
     80                     if (!vis[v]) {
     81                         q.push(v);
     82                         vis[v] = 1;
     83                     }
     84                 }
     85             }
     86         }
     87         //如果是最小费用可行流则要这一句(要求费用最小,不要求流量最大)
     88         //if (d[t] > 0) return false;
     89         return pre[t] != -1;
     90     }
     91     int solve(int s, int t, int &cost) {
     92         int flow = 0;
     93         cost = 0;
     94         while (spfa(s, t)) {
     95             int minFlow = inf;
     96             for (int i = pre[t]; ~i; i = pre[E[i ^ 1].v])
     97                 minFlow = min(minFlow, E[i].w);
     98             for (int i = pre[t]; ~i; i = pre[E[i ^ 1].v]) {
     99                 cost += minFlow * E[i].cost;
    100                 E[i].w -= minFlow;
    101                 E[i ^ 1].w += minFlow;
    102             }
    103             flow += minFlow;
    104         }
    105         return flow;
    106     }
    107 } F;
    108 int n, m;
    109 struct  Point {
    110     int x, y;
    111     Point (int x, int y): x(x), y(y) {}
    112 };
    113 int cal(Point a, Point b) {
    114     return abs(a.x - b.x) + abs(a.y - b.y);
    115 }
    116 char tu[105][105];
    117 vector<Point>men;
    118 vector<Point>home;
    119 int main() {
    120     while(~sff(n, m), n + m) {
    121         F.init();
    122         men.clear();
    123         home.clear();
    124         for (int i = 0 ; i < n ; i++) {
    125             scanf("%s", tu[i]);
    126             for (int j = 0 ; j < m ; j++) {
    127                 if (tu[i][j] == 'm') men.push_back(Point(i, j));
    128                 if (tu[i][j] == 'H') home.push_back(Point(i, j));
    129             }
    130         }
    131         int s=0,len1=men.size(),len2=home.size(),t;
    132         t=len1+len2+1;
    133         for (int i=0 ;i<len1 ;i++)
    134             for (int j=0 ;j<len2 ;j++)
    135                 F.add(i+1,j+1+len1,1,cal(men[i],home[j]));
    136         for (int i=1 ;i<=len1 ;i++) F.add(0,i,1,0);
    137         for (int i=1 ;i<=len2 ;i++) F.add(i+len1,t,1,0);
    138         int cost = 0;
    139         F.solve(s, t, cost);
    140         printf("%d
    ", cost);
    141     }
    142     return 0;
    143 }
  • 相关阅读:
    可能会搞砸你的面试:你知道一个TCP连接上能发起多少个HTTP请求吗?
    iOS笔记055
    iOS笔记053- Quartz2D-练习
    iOS笔记052- Quartz2D-绘图
    IOS笔记051-手势使用
    IOS笔记050-事件处理
    IOS笔记049-UITabBarController
    IOS笔记048-数据存储
    IOS笔记047-代理传值和block传值
    IOS笔记046-UIApplication/导航控制器
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9416362.html
Copyright © 2020-2023  润新知