HDU 1533 going Home (minimum charge flow)

Source: Internet
Author: User

Going HomeTime limit:10000/5000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 3278 Accepted Submission (s): 1661


Problem Descriptionon 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.
Inputthere 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.

Outputfor each test case, output one line with the single integer, which are the minimum amount, in dollars, your need to PA Y.

Sample Input
2 2.mh.5 5HH.. m...............mm. H7 8...H ..... H....... H....mmmhmmmm ... H....... H....... H.... 0 0

Sample Output
21028

Sourcepacific Northwest 2004

Test instructions: To a map, marked the location of man M and room H, the number of people and the number of houses equal, now everyone to go back to their homes, a room can only one person. What is the minimum number of steps everyone has to take? Each step can only go adjacent to the lattice.

Problem solving: Minimum cost flow.

3 Types of points: 1: Source point S, meeting point T. 2: Man M. 3: Room H.

Map: (U, V, cap, cost): U-->v contents as Cap

1): (S, M, 1, 0)

2): (M, H, 1, Mindis): Mindis indicates the shortest distance to the room

3: (H, T, 1, 0)

#include <stdio.h> #include <string.h> #include <queue>using namespace std;const int maxn = 10010;const    int MAXM = 100100;const int INF = 1<<30;struct edg{int to,next,cap,flow;  int cost; Unit price}edg[maxm];int Head[maxn],eid;int PRE[MAXN], COST[MAXN];    Point 0~ (n-1) void init () {eid=0; memset (head,-1,sizeof (Head));}    void addedg (int u,int v,int cap,int cst) {edg[eid].to=v; edg[eid].next=head[u]; edg[eid].cost = CST; Edg[eid].cap=cap; edg[eid].flow=0;    head[u]=eid++; Edg[eid].to=u; EDG[EID].NEXT=HEAD[V];    Edg[eid].cost =-CST; Edg[eid].cap=0; edg[eid].flow=0; head[v]=eid++;}    BOOL Inq[maxn];bool SPFA (int snode,int enode,int n) {queue<int>q;    for (int i=0; i<n; i++) {inq[i]=false; cost[i]= INF; } cost[snode]=0; Inq[snode]=1;    Pre[snode]=-1;    Q.push (SNode);        while (!q.empty ()) {int U=q.front (); Q.pop ();        inq[u]=0;            for (int i=head[u]; i!=-1; i=edg[i].next) {int v=edg[i].to; if (edg[i].cap-edG[i].flow>0 && cost[v]>cost[u]+edg[i].cost) {//In the case of satisfying an additive flow, the minimum cost cost[v] = Cost[u]+edg[i].cost;   Pre[v]=i;            Record the edge on the path if (!inq[v]) Q.push (v), inq[v]=1;    }}} return cost[enode]!=inf;    Determine if there is no augmented path}//reverse is the maximum flow, the minimum cost is mincostint mincost_maxflow (int snode,int enode,int& mincost,int n) {int ans=0;        while (SPFA (snode,enode,n)) {int mint=inf; for (int i=pre[enode]; i!=-1; i=pre[edg[i^1].to]) {if (Mint>edg[i].cap-edg[i].flow) mint=edg[i]        . Cap-edg[i].flow;        } Ans+=mint;            for (int i=pre[enode]; i!=-1; i=pre[edg[i^1].to]) {edg[i].flow+=mint; edg[i^1].flow-=mint;        Mincost+=mint*edg[i].cost; }} return ans; int abs (int a) {return a>0?a:-a;}    int Buildgraph (char mapt[105][105],int n,int m) {int id[105][105], k=1; for (int i=0, i<n; i++) for (int j=0; j<m; J + +) if (mapt[i][j]== ' H ' ||mapt[i][j]== ' m ') id[i][j]=k++;    int s=0, t = k;        for (int i=0, i<n; i++) for (int j=0; j<m; J + +) if (mapt[i][j]== ' m ') {int u,v;        U=ID[I][J];        ADDEDG (s,u,1,0); for (int ti=0, ti<n; ti++) for (int tj=0; tj<m; tj++) if (mapt[ti][tj]== ' H ') {V=id [Ti]                [TJ];            ADDEDG (U,v,1,abs (ti-i) +abs (tj-j));    }} else if (mapt[i][j]== ' H ') addedg (id[i][j],t,1,0); return k;}      int main () {int n,m;      Char mapt[105][105]; while (scanf ("%d%d", &n,&m) >0&& (n| |            m) {for (int i=0; i<n; i++) scanf ("%s", Mapt[i]);            Init ();                        int S=0,t=buildgraph (mapt,n,m), mincost=0;                        Mincost_maxflow (s,t,mincost,t+1);      printf ("%d\n", mincost); }}


HDU 1533 going Home (minimum charge 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.