Minimum fee stream poj2195

Source: Internet
Author: User
Going home
Time limit:1000 ms   Memory limit:65536 K
Total submissions:17955   Accepted:9145

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 done 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, '. '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, aggressive. 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.mH.5 5HH..m...............mm..H7 8...H.......H.......H....mmmHmmmm...H.......H.......H....0 0

Sample output

21028
Scan the path to get the coordinates of the person and the house, subtract the toll, and then build the minimum cost flow.
  1 #include<cstdio>  2 #include <queue>  3 #include <algorithm>  4 #include <assert.h>  5 #include <cstring>  6 using namespace std;  7 const int inf=0x7fffffff;  8 int n,m;  9  10 char maz[101][101]; 11 int man[101][2]; 12 int house[101][2]; 13 int hlen,mlen; 14  15  16 const int sups=201; 17 const int supt=202; 18  19 int cost[203][203]; 20 int f[203][203]; 21 int e[203][203]; 22 int len[203]; 23  24 int d[203]; 25 int pre[203]; 26 bool vis[203]; 27  28 queue<int  >que; 29  30 int main(){ 31     while(scanf("%d%d",&n,&m)==2&&n&&m){ 32 input: 33         hlen=mlen=0; 34         gets(maz[0]); 35         for(int i=0;i<n;i++){ 36             gets(maz[i]); 37         } 38         for(int i=0;i<n;i++){ 39             for(int j=0;j<m;j++){ 40                 if(maz[i][j]==‘m‘){ 41                     man[mlen][0]=i;man[mlen++][1]=j; 42                 } 43                 else if(maz[i][j]==‘H‘){ 44                     house[hlen][0]=i;house[hlen++][1]=j; 45                 } 46             } 47         } 48  49         memset(f,0,sizeof(f)); 50         memset(cost,0,sizeof(cost)); 51         fill(len,len+mlen,hlen+1);fill(len+mlen,len+mlen+hlen,mlen+1);len[sups]=mlen;len[supt]=hlen; 52 bulidedge: 53         for(int i=0;i<mlen;i++){ 54             f[sups][i]=1; 55             e[sups][i]=i; 56             e[i][0]=sups; 57             for(int j=0;j

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.