POJ2195 going home[Cost flow | Binary graph maximum weight matching]

Source: Internet
Author: User

going Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 22088 Accepted: 11155

Description

On a grid map there is n little men and N houses. In each unit time, every little mans can move one unit step, either horizontally, or vertically, to a adjacent point. For each little mans, you need to pay a $ fee for every step he moves, until he enters a house. The task is complicated with the restriction, 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 Diffe Rent 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 ' indica TES There is a little man on this point.

You can think of all the the grid map as a quite large square, so it can hold n little men at the same time; Also, it is the okay if a little man steps on a grid with a house without entering this house.

Input

There is one or more test cases in the input. Each case starts with a line giving-integers n and m, where N is the number of rows of the map, and M are the number of Columns. The rest of the input would be N lines describing the map. Assume both N and M are between 2 and inclusive. There would be the same number of ' H ' s and ' M's on the map; And there'll is at the most houses. Input would terminate with 0 0 for N and M.

Output

For each test case, the output one line with the single integer, which are the minimum amount, in dollars, and 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 each and every house has a double-edged, two-figure maximum-weight matchsolution with SPFA cost flow (or km)
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespaceStd;typedefLong Longll;Const intn=5005, m=1e6+5, inf=1e9;intRead () {CharC=getchar ();intx=0, f=1;  while(c<'0'|| C>'9'){if(c=='-') f=-1; C=GetChar ();}  while(c>='0'&&c<='9') {x=x*Ten+c-'0'; C=GetChar ();} returnx*F;}intn,m,s,t,n1,n2;Charss[ the];structdata{intx, y;} A[n],b[n];inlineintDis (data &a,data &b) {returnABS (a.x-b.x) +abs (a.y-b.y);}structedge{intV,ne,c,f,w;} E[m<<1];intCnt,h[n];inlinevoidInsintUintVintCintW) {CNT++; E[CNT].V=v;e[cnt].c=c;e[cnt].f=0; e[cnt].w=W; E[cnt].ne=h[u];h[u]=CNT; CNT++; E[CNT].V=u;e[cnt].c=0; e[cnt].f=0; e[cnt].w=-W; E[cnt].ne=h[v];h[v]=CNT;}voidbuild () {CNT=0; memset (H,0,sizeof(h)); S=0; t=n1+n2+1;  for(intI=1; i<=n1;i++)         for(intj=1; j<=n2;j++) ins (i,n1+j,1, Dis (a[i],b[j]));  for(intI=1; i<=n1;i++) ins (S,i,1,0);  for(intI=1; i<=n2;i++) ins (n1+i,t,1,0);}intD[n],q[n],head,tail,inq[n],pre[n],pos[n];inlinevoidLopint&AMP;X) {if(x==n) x=1;}BOOLSPFA () {memset (d,127,sizeof(d)); memset (INQ,0,sizeof(INQ)); Head=tail=1; D[s]=0; inq[s]=1; q[tail++]=s; Pre[t]=-1;  while(head!=tail) {        intu=q[head++];inq[u]=0; Lop (head);  for(intI=h[u];i;i=e[i].ne) {            intv=e[i].v,w=E[I].W; if(d[v]>d[u]+w&&e[i].c>e[i].f) {D[v]=d[u]+W; PRE[V]=u;pos[v]=i; if(!inq[v]) q[tail++]=v,inq[v]=1, lop (tail); }        }    }    returnpre[t]!=-1;}intMCMF () {intflow=0, cost=0;  while(SPFA ()) {intf=INF;  for(intI=t;i!=s;i=pre[i]) f=min (f,e[pos[i]].c-e[pos[i]].f); Flow+=f;cost+=d[t]*F;  for(intI=t;i!=s;i=Pre[i]) {E[POS[I]].F+=F; e[((pos[i)-1)^1)+1].f-=F; }    }    returnCost ;}intMainintargcConst Char*argv[]) {     while(true) {N1=n2=0; N=read (); m=read (); if(n==0&&m==0) Break;  for(intI=1; i<=n;i++) {scanf ("%s", ss+1);  for(intj=1; j<=m;j++){                if(ss[j]=='H') a[++n1]=(data) {I,J}; if(ss[j]=='m') b[++n2]=(data) {I,J};        }} build (); printf ("%d\n", MCMF ()); }}

POJ2195 going home[cost flow | Binary graph maximum weight matching]

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.