HDU 1533 going Home minimum cost max flow

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1533

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.

Test instructions Description: In the n*m matrix lattice has x person to go to x room, each room can only put one person, the person in the current lattice place to go up and down the adjacent lattice walk one step to spend 1 dollars, ask the last all people walk to the room after the smallest cost.

Algorithm analysis: This problem can be used in km algorithm or minimum cost maximum flow algorithm solution, here to explain the minimum cost maximum flow method.

New source point from and meeting point to,from-> person (W is 1,cost 0)

House->to (W for 1,cost for 0)

Per room (W for 1,cost as the shortest path for USD cost)

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <cmath>6#include <algorithm>7#include <vector>8#include <queue>9 #defineINF 0x7fffffffTen using namespacestd; One Const intmaxn=10000+ -; A Const intM =40000+ -; -  - intNm from, to; the structnode - { -     intV,flow,cost; -     intNext; +}edge[m*4]; - intHead[maxn],edgenum; + intDIS[MAXN],PRE[MAXN],PID[MAXN],VIS[MAXN]; A  at voidAddintUintVintFlowintCost ) - { -Edge[edgenum].v=v; edge[edgenum].flow=flow; -Edge[edgenum].cost=cost; edge[edgenum].next=Head[u]; -head[u]=edgenum++; -  inEdge[edgenum].v=u; edge[edgenum].flow=0; -Edge[edgenum].cost=-cost; edge[edgenum].next=Head[v]; tohead[v]=edgenum++; + } -  the intSPFA () * { $      for(intI=1; i<=to; i++.)Panax Notoginseng     { -dis[i]=inf; thevis[i]=0; +     } Aqueue<int>Q; theQ.push ( from); +dis[ from]=0; -vis[ from]=1; $      while(!q.empty ()) $     { -         intu=Q.front (); Q.pop (); -vis[u]=0; the          for(intI=head[u]; i!=-1; i=edge[i].next) -         {Wuyi             intv=edge[i].v; the             if(edge[i].flow>0&& dis[v]>dis[u]+edge[i].cost) -             { Wudis[v]=dis[u]+Edge[i].cost; -pre[v]=u; Aboutpid[v]=i; $                 if(!Vis[v]) -                 { -vis[v]=1; - Q.push (v); A                 } +             } the         } -     } $     returnDis[to]; the } the  the intmincost () the { -     intaug=0, maxflow=0; in     intans=0, tmp=0; the      while(1) the     { Abouttmp=SPFA (); the         if(Tmp==inf) Break; theaug=inf; the          for(intI=to; i!= from; i=Pre[i]) +         { -             if(Edge[pid[i]].flow<( ) theaug=Edge[pid[i]].flow;Bayi         } the          for(intI=to; i!= from; i=Pre[i]) the         { -Edge[pid[i]].flow-=; -edge[pid[i]^1].flow + =; the         } theMaxflow + =; theAns + =tmp; the     } -     returnans; the } the  the intMain ()94 { the      while(SCANF ("%d%d", &n,&m)! =EOF) the     { the         if(!n &&!m) Break;98memset (head,-1,sizeof(head)); Aboutedgenum=0; -         Charstr[111][111];101memset (str,0,sizeof(str));102          for(intI=1; i<=n; i++.)103         {104scanf"%s", str[i]+1); the         }106          from=n*m+1;107to= from+1;108         inth[111],c[111],cnt=0;109         inth2[111],c2[111],cnt2=0; the          for(intI=1; i<=n; i++.)111         { the              for(intj=1; j<=m; j + +)113             { the                 if(str[i][j]=='m') the                 { theAdd from, (I-1) *m+j,1,0);117H[cnt]=i, C[cnt]=j, cnt++ ;118                 }119                 Else if(str[i][j]=='H') -                 {121Add (I-1) *m+j,to,1,0);122H2[cnt2]=i, C2[cnt2]=j, cnt2++ ;123                 }124             } the         }126          for(intI=0; i<cnt; i++.)127         { -              for(intj=0; J<cnt2; j + +)129Add (h[i]-1) *m+c[i], (h2[j]-1) *m+c2[j],1, ABS (H[i]-h2[j]) +abs (c[i]-c2[j])); the         }131printf"%d\n", Mincost ()); the     }133     return 0;134}

HDU 1533 going Home minimum cost max flow

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.