Max flow of minimum cost for poj 2195 Map

Source: Internet
Author: User

Idea: At the beginning, I knew it was the maximum flow of the minimum cost, because I had to find the best, and m and H had to be matched one by one, therefore, if it is not a Bipartite Graph Match, It is the maximum flow with the minimum cost.

However, at the beginning, I was still wondering how to find the minimum cost between m and H. Do I need to use DFS to search? After thinking about this, I read the question for 1000 ms and then excluded the shortest distance between the DFS search m and H. Then I thought about it. Actually Nima is too simple, because the question says that we can only walk vertically and vertically, so the shortest distance is not the sum of the two abscissa subtraction and the two ordinate subtraction!

Then, every pair of M and H are connected, and the traffic is 1 (because each pair of matching cannot be repeated), the cost is the distance between them, and then the super source and super sink are created, the source point is connected to each m, and the traffic is the same as above (single match). The cost is 0, because there is no cost between them; the sink point and the source point build edge are the same.

# Pragma comment (linker, "/Stack: 1024000000,1024000000 ") # include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <map> # include <queue> # include <set> # include <cmath> # include <bitset> # define MEM (, b) memset (a, B, sizeof (A) # define lson I <1, L, mid # define rson I <1 | 1, Mid + 1, R # define llson j <1, L, mid # define rrson j <1 | 1, Mid + 1, R # define INF 0x7ffffffftypedef long ll; typedef unsigned long Long ull; using namespace STD; # define maxn 20005 struct {int V, W, C, next, RE; // re records the subscript of the inverse edge, C is the cost, W is traffic} e [maxn]; int N, CNT; int head [maxn], que [maxn * 8], pre [maxn], DIS [maxn]; bool vis [maxn]; void add (int u, int V, int W, int c) {e [CNT]. V = V, E [CNT]. W = W, E [CNT]. C = C; E [CNT]. next = head [u]; E [CNT]. re = CNT + 1, head [u] = CNT ++; E [CNT]. V = u, E [CNT]. W = 0, E [CNT]. C =-C; E [CNT]. next = head [v]; E [CNT]. re = cnt-1, head [v] = CNT ++;} bool spfa () {Int I, L = 0, r = 1; for (I = 0; I <= N; I ++) dis [I] = inf, vis [I] = false; DIS [0] = 0, que [0] = 0, vis [0] = true; while (L <R) {int u = que [L ++]; for (I = head [u]; I! =-1; I = E [I]. next) {int v = E [I]. v; If (E [I]. W & dis [v]> dis [u] + E [I]. c) {dis [v] = dis [u] + E [I]. c; Pre [v] = I; If (! Vis [v]) {vis [v] = true; que [R ++] = V ;}} vis [u] = false;} return dis [N]! = Inf;} int change () {int I, P, sum = inf, ANS = 0; for (I = N; I! = 0; I = E [E [p]. re]. v) {// E [E [p]. re]. V is the forward node. If not, see add and spfa P = pre [I]; // P is the forward node number sum = min (sum, E [p]. w) ;}for (I = N; I! = 0; I = E [E [p]. re]. v) {P = pre [I]; E [p]. w-= sum; E [E [p]. re]. W + = sum; ans + = sum * E [p]. c; // The unit traffic fee recorded in C must be multiplied by the traffic fee.} Return ans;} int EK () {int sum = 0; while (spfa () sum + = change (); Return sum;} void Init () {MEM (Head,-1), Mem (PRE, 0), CNT = 0;} Char s [102] [102]; struct mm {int X, Y ;} mm [101], HH [101]; int main () {// freopen ("1.txt"," r ", stdin); int n, m; while (~ Scanf ("% d", & N, & M) {If (! N &&! M) break; int I, j, TOT = 0, TMP = 0, DIST; Init (); for (I = 0; I <n; I ++) {scanf ("% s", s [I]); For (j = 0; j <m; j ++) {If (s [I] [J] = 'M') mm [tot]. X = I, Mm [tot ++]. y = J; If (s [I] [J] = 'H') HH [TMP]. X = I, HH [TMP ++]. y = J ;}n = tot + TMP + 1; for (I = 1; I <= tot; I ++) add (0, I ); // because each edge can only be used once, the traffic is 1 for (I = tot + 1; I <= tot + TMP; I ++) add (I, n, 1, 0); // because each side can only be used once, the traffic is 1 for (I = 1; I <= tot; I ++) for (j = tot + 1; j <= tot + TMP; j ++) {Dist = ABS (Mm [I-1]. x-hh[ j-tot-1]. x) + ABS (Mm [I-1]. y-hh[ j-tot-1]. y); add (I, j, 1, DIST); // the minimum cost of M and H is the minimum cost} printf ("% d \ n ", ek ();} return 0 ;}


Max flow of minimum cost for poj 2195 Map

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.