zoukankan      html  css  js  c++  java
  • hdu 三部曲 Going Home 最小费用最大流 EK算法

    Problem 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
     **************************************************************************************************************************
    最小费用最大流   EK算法
    ***************************************************************************************************************************
      1 #include<iostream>
      2 #include<string>
      3 #include<cstring>
      4 #include<cstdio>
      5 #include<queue>
      6 #include<stack>
      7 #include<algorithm>
      8 #define inf  0x7fffffff
      9 using namespace std;
     10 int pre[501],cost[501][501];
     11 int dis[522],cap[521][521];
     12 int vis[511];
     13 int que[10000011];
     14 int n,m,k,i,j,src;
     15 int end1,s;
     16 char str[1001];
     17 int top1,top2;
     18 struct node
     19  {
     20      int x,y;
     21  }H[1001],man[1001];
     22 
     23 int bfs()//注意用STL超时
     24 {
     25     for(int it=0;it<=end1;it++)
     26       dis[it]=inf;
     27     memset(vis,0,sizeof(vis));
     28     int base,top;
     29     base=top=0;
     30     que[top++]=src;
     31     dis[src]=0;
     32     vis[src]=1;
     33     pre[src]=0;
     34     while(base<top)
     35     {
     36         int fs=que[base++];
     37         vis[fs]=0;
     38         for(int it=1;it<=end1;it++)
     39          {
     40             if(it!=fs&&cap[fs][it]>0)
     41              if(dis[it]>(dis[fs]+cost[fs][it]))//此处找到最小费用
     42              {
     43                 pre[it]=fs;
     44                 dis[it]=dis[fs]+cost[fs][it];
     45                 if(!vis[it])
     46                 {
     47                     que[top++]=it;
     48                     vis[it]=1;
     49                 }
     50              }
     51          }
     52     }
     53     if(dis[end1]==inf)
     54       return -1;
     55     return 1;
     56 
     57 }
     58 int EK()//算出最小费用最大流
     59 {
     60     int jt,kt;
     61     int sm=0;
     62     int min1=inf;
     63     while(1)
     64     {
     65         kt=bfs();
     66         if(kt==-1)
     67           break;
     68         for(int it=end1;it!=0;it=pre[it])
     69         {
     70             jt=pre[it];
     71             if(min1>cap[jt][it])
     72               min1=cap[jt][it];
     73         }
     74         for(int it=end1;it!=0;it=pre[it])
     75         {
     76             jt=pre[it];
     77             sm+=cost[jt][it]*min1;
     78             cap[jt][it]-=min1;
     79             cap[it][jt]+=min1;
     80         }
     81     }
     82     return sm;
     83 
     84 }
     85 int main()
     86 {
     87   while(scanf("%d%d",&n,&m)!=EOF)
     88   {
     89       if(n==0&&m==0)
     90          break;
     91        top1=top2=0;
     92       for(i=0;i<n;i++)
     93         {
     94             scanf("%s",str);
     95             getchar();
     96             for(j=0;j<m;j++)
     97              {
     98                if(str[j]=='H')
     99                {
    100                  H[top1].x=i;
    101                  H[top1++].y=j;
    102                }
    103               if(str[j]=='m')
    104               {
    105                 man[top2].x=i;
    106                 man[top2++].y=j;
    107               }
    108              }
    109         }
    110         if(top1==0)
    111         {
    112             printf("0
    ");
    113             continue;
    114         }
    115         memset(cap,0,sizeof(cap));
    116         memset(cost,0,sizeof(cost));
    117         for(i=1;i<=top2;i++)
    118          cap[0][i]=1;
    119         for(i=1;i<=top2;i++)
    120         {
    121             int x1=man[i-1].x;
    122             int y1=man[i-1].y;
    123             for(j=1;j<=top1;j++)
    124             {
    125                 int x2=H[j-1].x;
    126                 int y2=H[j-1].y;
    127                 x2=x2-x1;
    128                 if(x2<0)
    129                   x2=-x2;
    130                 y2=y2-y1;
    131                 if(y2<0)
    132                   y2=-y2;
    133                 cap[i][j+top2]=1;
    134                 cost[i][j+top2]=x2+y2;
    135                 cost[j+top2][i]=-(x2+y2);
    136 
    137             }
    138         }
    139         for(i=1;i<=top2;i++)
    140           cap[top2+i][2*top2+1]=1;
    141 
    142      s=0;
    143      src=0;end1=2*top1+1;
    144      s=EK();
    145      printf("%d
    ",s);
    146 
    147   }
    148   return 0;
    149 }
    View Code
  • 相关阅读:
    吴裕雄--天生自然 R语言开发学习:图形初阶
    吴裕雄--天生自然 R语言开发学习:导入数据
    如何基于阿里云搭建适合初创企业的轻量级架构?
    基于协同过滤算法的推荐
    重磅!阿里巴巴工程师获得 containerd 社区席位,与社区共建云时代容器标准
    完爆 Best Fit,看阿里如何优化 Sigma 在线调度策略节约亿级成本
    阿里云物联网边缘计算加载MQTT驱动
    阿里云物联网平台体验(树莓派+Python篇)
    RAM SSO功能重磅发布 —— 满足客户使用企业本地账号登录阿里云
    阿里云高级技术专家带你全面了解云主机性能评测
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3352750.html
Copyright © 2011-2022 走看看