HDU 1533 going Home minimum cost max inflow door question

Source: Internet
Author: User

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


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


Test instructions: Pair the H and M one by one with the minimum number of steps they need to take.


Practice: Use BFS first to find any H and M direct minimum number of steps. Record them. Then build the diagram like a binary chart. Beginning to all home traffic 1, costs 0. The cost between home and man is distance, traffic between 1,man and endpoint EE flows between 1 and 0. After the building, and then with the minimum cost of the maximum flow run one side is OK.


#include <cstdio> #include <iostream> #include <cmath> #include <cstring> #include <vector > #include <stdlib.h> #include <queue> #include <map> #include <algorithm>using namespace std ; const int MAXN = 10100;const int MAXM = 30000000;const int INF = 0x3f3f3f3f;struct edge{int to,next,cap,flow,cost;} Edge[maxm];int head[maxn],tol;int pre[maxn],dis[maxn];bool vis[maxn];int n;//node total number, node number from 0~n-1void init (int N) {n = n; Tol = 0;memset (head,-1,sizeof (Head));} void Addedge (int u,int v,int cap,int cost) {edge[tol].to = V;edge[tol].cap = Cap;edge[tol].cost = Cost;edge[tol].flow = 0;e Dge[tol].next = Head[u];head[u] = tol++; edge[tol].to = U;edge[tol].cap = 0;edge[tol].cost =-cost;edge[tol].flow = 0;edge[tol].next = Head[v];head[v] = tol++;} BOOL SPFA (int s,int t) {queue<int>q;for (int i = 0;i < n;i++) {Dis[i] = inf;vis[i] = false;pre[i] =-1;} Dis[s] = 0;vis[s] = True;q.push (s), while (!q.empty ()) {int u = q.front (); Q.pop (); Vis[u] = false;for (int i = heAd[u]; I! = -1;i = edge[i].next) {int v = edge[i].to;if (Edge[i].cap > Edge[i].flow &&dis[v] > Dis[u] + edge[i].cost {Dis[v] = Dis[u] + edge[i].cost;pre[v] = i;if (!vis[v]) {Vis[v] = True;q.push (v);}}} if (pre[t] = =-1) return false;else return true; The maximum flow is returned, the cost is the minimum charge int mincostmaxflow (int s,int t,int &cost) {int flow = 0;cost = 0;while (SPFA (s,t)) {int Min = Inf;f or (int i = pre[t];i! = -1;i = Pre[edge[i^1].to]) {if (min > Edge[i].cap-edge[i].flow) Min = Edge[i].cap-edge[i].flow;} for (int i = pre[t];i! = -1;i = Pre[edge[i^1].to]) {Edge[i].flow + = min;edge[i^1].flow-= min;cost + = Edge[i].cost * Min;} Flow + = Min;} return flow;} int n,m;struct Node{int x,y,bu;};/  /addedge (int u,int v,int cap,int cost) Char mp[110][110];//map char num[110][110];//point Search int mp_dis[110][110];//number distance ago Home After Menint mp_vis[110][110];int dir[4][2]={1,0,0,1,0,-1,-1,0};void bfs (int x,int y) {memset (mp_vis,-1,sizeof Mp_vis); queue<node> q;node Sta,nw,tem;sta.bu=0;sta.x=x;sta.y=y;q.push (STA); whilE (!q.empty ()) {Nw=q.front (); Q.pop (); if (mp[nw.x][nw.y]== ' m ') mp_dis[num[x][y]][num[nw.x][nw.y]]=nw.bu;for (int i=0; i<4;i++) {int xx=nw.x+dir[i][0];int yy=nw.y+dir[i][1];if (xx<0| | xx>=n| | yy<0| | yy>=m) continue;if (Mp_vis[xx][yy]!=-1&&nw.bu+1>=mp_vis[xx][yy]) continue;tem.x=xx;tem.y=yy;mp_vis[  xx][yy]=tem.bu=nw.bu+1; Q.push (TEM); }}}int Main () {while (cin>>n>>m,n| | m) {for (int i=0;i<n;i++) {scanf ("%s", Mp[i]);} int home,man;home=man=0;for (int i=0;i<n;i++) {for (int j=0;j<m;j++) {if (mp[i][j]== ' H ') num[i][j]=home++;if (mp[i ][j]== ' m ') num[i][j]=man++;}} for (int i=0;i<n;i++) {for (int j=0;j<m;j++) {if (mp[i][j]== ' H ') BFS (I,J);}} int Ss=home+man;int ee=home+man+1;init (home+man+2), for (int. i=0;i








HDU 1533 going Home minimum cost max inflow door question

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.