Poj Training Plan 2195_going home (network stream/fee Stream)

Source: Internet
Author: User

Solution report

Question Portal

Ideas:

Create a BFS graph and run the billing flow.

#include <iostream>#include <cstdio>#include <cstring>#include <queue>#define inf 0x3f3f3f3fusing namespace std;struct E {    int v,cost,cap,next;} edge[100000];int head[1000],cnt,dis[1000],pre[1000],vis[1000],s,t,mmap[110][110],h,w,f[1100],cost,flow;struct N {    int x,y,step;};void add(int u,int v,int cost,int cap) {    edge[cnt].v=v;    edge[cnt].cost=cost;    edge[cnt].cap=cap;    edge[cnt].next=head[u];    head[u]=cnt++;    edge[cnt].v=u;    edge[cnt].cost=-cost;    edge[cnt].cap=0;    edge[cnt].next=head[v];    head[v]=cnt++;}int dx[]= {-1,0,1,0};int dy[]= {0,1,0,-1};void bfs(int x,int y) {    queue<N>q;    int v[110][110];    memset(v,0,sizeof(v));    N next,now;    now.x=x;    now.y=y;    now.step=0;    v[x][y]=1;    q.push(now);    while(!q.empty()) {        now=q.front();        q.pop();        if(mmap[now.x][now.y]>=101) {            add(mmap[x][y],mmap[now.x][now.y],now.step,1);        }        for(int i=0; i<4; i++) {            next.x=now.x+dx[i];            next.y=now.y+dy[i];            if(next.x>=0&&next.x


Going home
Time limit:1000 ms   Memory limit:65536 K
Total submissions:18017   Accepted:9185

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

Source

Pacific Northwest 2004

Poj Training Plan 2195_going home (network stream/fee Stream)

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.