Optimal minimum weight matching

Source: Internet
Author: User

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


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 through this question, I have deepened the understanding of the enlarged equal sub-graph in the binary matching maximum weight matching. It's a really good question. The problem is the opposite ...
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <climits > #include <cmath>using namespace std;const int N = 105;struct node{int x, y;}; Vector<node>man;vector<node>house;int Weight[n][n];int Lx[n], ly[n];int slack[N];int match[N];bool visx[    N], visy[n];int N, m;bool dfs (const int& x) {visx[x] = 1;        for (int i = 0; i < m; i + +) {if (visy[i]) continue;        int t = weight[x][i]-lx[x]-ly[i];            if (t = = 0) {Visy[i] = 1;                if (match[i] = = 1 | | DFS (MATCH[I])) {match[i] = x;            return true;        }} else {slack[i] = min (slack[i], T); }} return false;}    void KM () {int temp;    memset (ly, 0, sizeof (ly));        for (int i = 0; i < n; i + +) {Lx[i] = Int_max;        for (int j = 0; J < m; J + +) {Lx[i] = min (Lx[i], weight[i][j]);    }} for (int i = 0; i < n; i + +) {    for (int j = 0; J < m; j + +) slack[j] = Int_max;            while (1) {memset (visx, 0, sizeof (VISX));            memset (Visy, 0, sizeof (Visy));            if (Dfs (i)) break;            temp = Int_max;            for (int j = 0; J < m; J + +) {if (!visy[j]) temp = min (temp, slack[j]);            } for (int j = 0; J < N; j + +) {if (Visx[j]) lx[j] + = temp;                } for (int j = 0; J < m; J + +) {if (Visy[j]) ly[j]-= temp;            else Slack[j]-= temp;        }}}}int Main () {while (scanf ("%d%d", &n, &m), n+m) {man.clear (), house.clear ();        Node temp;            for (int i = 1; I <= n; i + +) {GetChar ();                for (int j = 1; j <= M; j + +) {Char ch = getchar ();                if (ch = = ' m ') {temp.x = i, temp.y = j, Man.push_back (temp); } else if (ch = = ' H ') {                    temp.x = i, temp.y = j, House.push_back (temp);        }}} n = Man.size ();        m = House.size (); for (int i = 0; i < n; i + +) {for (int j = 0; J < m; J + +) {Weight[i][j] = ABS (man[i].x-            house[j].x) +abs (MAN[I].Y-HOUSE[J].Y);        }} memset (Match,-1, sizeof (match));        KM ();        int ans = 0;        for (int i = 0; i < m; i + +) {if (Match[i] >-1) ans + = weight[match[i]][i];    } printf ("%d\n", ans); } return 0;}



Optimal minimum weight matching

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.