zoukankan      html  css  js  c++  java
  • poj2195 Going Home

    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
    

    正解:二分图最大权匹配。

    这题其实用费用流轻松水过,不过学了一下KM算法,就用了一下。

     1 //It is made by wfj_2048~
     2 #include <algorithm>
     3 #include <iostream>
     4 #include <complex>
     5 #include <cstring>
     6 #include <cstdlib>
     7 #include <cstdio>
     8 #include <vector>
     9 #include <cmath>
    10 #include <queue>
    11 #include <stack>
    12 #include <map>
    13 #include <set>
    14 #define inf (1<<30)
    15 #define il inline
    16 #define RG register
    17 #define ll long long
    18 
    19 using namespace std;
    20 
    21 int a[1010][1010],v[1010][1010],g[1010][1010],lk[10010],lx[10010],ly[10010],visx[10010],visy[10010],slack[10010],N,M,n,n1,n2,cnt,ans;
    22 
    23 il int gi(){
    24     RG int x=0,q=1; RG char ch=getchar(); while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
    25     if (ch=='-') q=-1,ch=getchar(); while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q*x;
    26 }
    27 
    28 il char gc(){ RG char ch=getchar(); while (ch!='.' && ch!='m' && ch!='H') ch=getchar(); return ch; }
    29 
    30 il int dfs(RG int x){
    31     visx[x]=cnt;
    32     for (RG int y=1;y<=n;++y){
    33     if (visy[y]==cnt) continue; RG int t=lx[x]+ly[y]-g[x][y];
    34     if (!t){ visy[y]=cnt; if (!lk[y] || dfs(lk[y])){ lk[y]=x; return 1; } }
    35     else if (slack[y]>t) slack[y]=t;
    36     }
    37     return 0;
    38 }
    39 
    40 il void KM(){
    41     memset(lk,0,sizeof(lk)),memset(lx,0,sizeof(lx)),memset(ly,0,sizeof(ly));
    42     for (RG int i=1;i<=n;++i) for (RG int j=1;j<=n;++j) lx[i]=max(lx[i],g[i][j]);
    43     for (RG int x=1;x<=n;++x){
    44     for (RG int i=1;i<=n;++i) slack[i]=inf;
    45     while (++cnt,!dfs(x)){
    46         RG int d=inf; for (RG int i=1;i<=n;++i) if (visy[i]!=cnt) d=min(d,slack[i]);
    47         for (RG int i=1;i<=n;++i){
    48         if (visx[i]==cnt) lx[i]-=d;
    49         if (visy[i]==cnt) ly[i]+=d; else slack[i]-=d;
    50         }
    51     }
    52     }
    53     return;
    54 }
    55 
    56 il void work(){
    57     n1=n2=ans=0;
    58     for (RG int i=1;i<=N;++i)
    59     for (RG int j=1;j<=M;++j){
    60         a[i][j]=gc();
    61         if (a[i][j]=='m') v[i][j]=++n1;
    62         if (a[i][j]=='H') v[i][j]=++n2;
    63     }
    64     for (RG int i=1;i<=N;++i)
    65     for (RG int j=1;j<=M;++j){
    66         if (a[i][j]!='m') continue;
    67         for (RG int p=1;p<=N;++p)
    68         for (RG int q=1;q<=M;++q)
    69             if (a[p][q]=='H') g[v[i][j]][v[p][q]]=-abs(i-p)-abs(j-q);
    70     }
    71     n=n1; KM(); for (RG int i=1;i<=n;++i) ans+=lx[i]+ly[i]; printf("%d
    ",-ans); return;
    72 }
    73 
    74 int main(){
    75     while (scanf("%d%d",&N,&M)==2 && N|M) work();
    76     return 0;
    77 }
  • 相关阅读:
    开发小技巧: 如何在jQuery中禁用或者启用滚动事件.scroll java程序员
    Spell Checker 新版Chrome的纠错特性 java程序员
    45个漂亮且有创意的HTML5网站展示 java程序员
    70个jquery触摸事件插件——支持手势触摸! java程序员
    40个超酷的jQuery动画教程 java程序员
    极客技术专题【002期】:开发小技巧 如何使用jQuery来处理图片坏链? java程序员
    30个热门的CSS3 Image Hover 脚本 java程序员
    2013年三月GBin1月刊 java程序员
    插入1000万数据的几种优质算法
    批量上传图片(带百分比进度显示)项目源码
  • 原文地址:https://www.cnblogs.com/wfj2048/p/6433979.html
Copyright © 2011-2022 走看看