HDU2255 Ben well-off make a lot of money "binary chart best match · KM algorithm "

Source: Internet
Author: User
Tags stdin

Ben-off make a lot of moneyTime limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) Total Submission (s): 3898 Accepted Submission (s): 1691

Problem description legend in a faraway place there is a very wealthy village, one day the village chief decided to reform the system: redistribute the House.
This is a big event, related to people's housing problems ah. There are n rooms in the village, just there are n common people, considering every house to have a room to live (if there are people do not live, easy to cause instability factors), each family must be assigned to a house and can only get a house.
On the other hand, village chiefs and other village leaders hope to get the most benefit, so that the village's institutions will be rich. Because people are richer, they can have a certain price for each house in their economy, for example, there are 3 houses, and a common man can give 100,000 to the first, 20,000 to the 2nd. 200,000 for the 3rd room. (in their economy, of course). The question now is how village leaders can allocate their homes to make the most of their income. (Villagers who have the money to buy a house but not necessarily can buy it depends on what the village leader assigns).

Input data contains multiple sets of test cases, the first row of each set of data input n, indicating the number of houses (also the number of people home), followed by n rows, the number of n per row represents the price of the room of the second village name (n<=300).

Output make the maximum revenue value for each set of data, one row for each set of outputs.


Sample Input
2100 1015 23

Sample Output
123

Sourcehdoj Summer Exercise (4)-Buffet DinnerTest Instructions: A template problem for finding the best match of a binary graph. The puzzle : Start using the template on the book, see the four-layer for loop immediately sweating-_-| | | The result is knocked down and found unable to run. Then found a template on the Internet, the original link.


#include <stdio.h> #include <string.h> #define MAXN 305#define inf 0x3f3f3f3fint g[maxn][maxn];int LX[MAXN],    Ly[maxn];int Match[maxn];bool VISX[MAXN], Visy[maxn];int SLACK[MAXN], n;bool DFS (int cur) {int T, y;    Visx[cur] = true;        for (y = 1; y <= N; ++y) {if (visy[y]) continue;        t = Lx[cur] + ly[y]-g[cur][y];            if (t = = 0) {Visy[y] = true; if (match[y] = =-1 | |            DFS (Match[y])) {match[y] = cur; return true;     }} else if (Slack[y] > t) slack[y] = t; } return false;}    int KM () {int I, j, X, D, ret;    memset (Match,-1, sizeof (int) * (n + 1));    memset (Ly, 0, sizeof (int) * (n + 1));        for (i = 1; I <= n; ++i) {lx[i] =-inf;    for (j = 1; j <= N; ++j) if (G[i][j] > Lx[i]) lx[i] = G[i][j];        } for (x = 1; x <= n; ++x) {memset (slack, 0x3f, sizeof (int) * (n + 1));           while (true) {memset (visx, 0, sizeof (BOOL) * (n + 1)); memset (Visy, 0, sizeof (BOOL) * (n + 1));            if (DFS (x)) break;            d = inf;            for (i = 1; I <= n; ++i) if (!visy[i] && d > slack[i]) d = slack[i];            for (i = 1; I <= n; ++i) if (Visx[i]) lx[i]-= D;                for (i = 1; I <= n; ++i) if (Visy[i]) ly[i] + = D;        else Slack[i]-= D;    }} ret = 0;    for (i = 1; I <= n; ++i) if (Match[i] >-1) ret + = G[match[i]][i]; return ret;}    int main () {//Freopen ("Stdin.txt", "R", stdin);    int I, J; while (scanf ("%d", &n) = = 1) {for (i = 1; I <= n; ++i) for (j = 1; j <= N; ++j) s        CANF ("%d", &g[i][j]);    printf ("%d\n", KM ()); } return 0;}


HDU2255 Ben well-off make a lot of money "binary chart best match · KM algorithm "

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.