HDU 1533 -- Going Home [maximum minimum fee stream & amp; TEMPLATE], hdu1533 -- going

Source: Internet
Author: User

HDU 1533 -- Going Home [maximum minimum fee & template], hdu1533 -- going

Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 3452 Accepted Submission (s): 1771


Problem DescriptionOn 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.
InputThere 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.
 
OutputFor 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
 
Question:

An N * M map has the same number of characters H and m, m represents a person, and H Represents a house. The cost of people to the House is the distance between them in the Manhattan figure. Ask you the minimum cost for getting everyone back to the House (a house can only accommodate one person ).


Solution:

I did not dare to write the first question about the minimum cost flow. I chose A bare question. I would like to thank bin Shen for his template and Xiao B for his analysis.

Because it is a bare question, the idea is quite good. Let's briefly explain the idea of creating a picture:

(1) create a super source point outset and a super sink point inset.

(2) The source node builds edges for each user. The capacity is 1 and the cost is 0.

(3) Build the edge at the settlement point. The capacity is 1 and the cost is 0.

(4) Each person and house are built along with a capacity of 1 and the cost is the distance from Manhattan.

From outset to inset, you can run the maximum flow with the minimum fee.

# Include <cstdio> # include <cstring> # include <algorithm> # include <queue> # define INF 0x3f3f3f3f # define maxn 220 # define maxm 88000 using namespace std; int n, m; int outset; // super source point int inset; // super sink point struct node {int u, v, cap, flow, cost, next ;}; node edge [maxm]; int head [maxn], cnt; int per [maxn]; // records the int dist [maxn], vis [maxn], and the number of the edge that reaches point I in the augmented path. void init () {cnt = 0; memset (head,-1, sizeof (head);} void add (int u, Int v, int w, int c) {edge [cnt] = {u, v, w, 0, c, head [u]}; head [u] = cnt ++; edge [cnt] = {v, u, 0, 0,-c, head [v]}; head [v] = cnt ++;} int dir (int x1, int y1, int x2, int y2) {return abs (x1-x2) + abs (y1-y2);} struct NODE {int x, y;}; NODE maph [maxn], mapm [maxn]; int ans_h, ans_m; void getmap () {char str [120] [120]; ans_h = ans_m = 0; for (int I = 0; I <n ;++ I) {scanf ("% s ", str [I]); for (int j = 0; j <M; ++ j) {if (str [I] [j] = 'M') {ans_m ++; mapm [ans_m]. x = I; mapm [ans_m]. y = j;} if (str [I] [j] = 'H') {ans_h ++; maph [ans_h]. x = I; maph [ans_h]. y = j ;}}int t = ans_h; // Number of outsets = 0; // source point inset = t * 2 + 1; // sink point for (int I = 1; I <= t; ++ I) {add (outset, I, 1, 0); add (I + t, inset, 1, 0 ); for (int j = 1; j <= t; ++ j) {int d = dir (maph [I]. x, maph [I]. y, mapm [j]. x, mapm [j]. y); add (I, j + t, 1, d );}} // Find the path with the minimum cost // run SPFA once to find the path with the minimum cost of st -- ed, and each side of the path cannot be full. // If yes, the path can be extended., otherwise, it cannot be bool SPFA (int st, int ed) {queue <int> q; memset (dist, INF, sizeof (dist); memset (vis, 0, sizeof (vis); memset (per,-1, sizeof (per); dist [st] = 0; vis [st] = 1; q. push (st); while (! Q. empty () {int u = q. front (); q. pop (); vis [u] = 0; for (int I = head [u]; I! =-1; I = edge [I]. next) {node E = edge [I]; if (dist [E. v]> dist [u] + E. cost & E. cap> E. flow) {// can be relaxed and not full stream dist [E. v] = dist [u] + E. cost; per [E. v] = I; // the ID of the edge that records the arrival of this vertex if (! Vis [E. v]) {vis [E. v] = 1; q. push (E. v) ;}}} return per [ed]! =-1;} void MCMF (int st, int ed, int & cost, int & flow) {flow = 0; // total traffic cost = 0; // total cost while (SPFA (st, ed) {// find the path int mins = INF with the minimum cost each time; // find the smallest augmented flow through the least-cost path from the reverse arc to the sink point (int I = per [ed]; I! =-1; I = per [edge [I ^ 1]. v]) {mins = min (mins, edge [I]. cap-edge [I]. flow);} // augmented for (int I = per [ed]; I! =-1; I = per [edge [I ^ 1]. v]) {edge [I]. flow + = mins; edge [I ^ 1]. flow-= mins; cost + = edge [I]. cost * mins; // overhead of the augmented stream} flow + = mins // total traffic accumulation} int main () {while (scanf ("% d", & n, & m), n | m) {init (); getmap (); int cost, flow; // maximum minimum fee stream MCMF (outset, inset, cost, flow ); printf ("% d \ n", cost);} return 0 ;}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.