HDU 2255 Ben-off make a lot of money (the maximum stripe weight of a binary graph matching bare km algorithm)

Source: Internet
Author: User
Tags first row
Ben-off make a lot of moneyClick to open the topic link Time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3979 Accepted Submission (s): 1729


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
2 100 10) 15 23
Sample Output
123
Source hdoj Summer Exercise (4)-Buffet Dinner km The algorithm can be done directly, make the right, but the process of attention to the small standard, the details are very important, this problem contributed 13 times tle, because the small mark was wrong, Σ (° °| | |) ︴ the template, but better than the beginning; code:


#include <string.h> #include <stdio.h> #define N 310 #define INF (1<<30) int w[n][n];
int lx[n],ly[n];
int slack[n];
int lef[n];
int sx[n],sy[n];
int n;
    bool Dfs (int x) {int i;
    Sx[x]=1;
            for (I=1; i<=n; i++) {if (sy[i]) continue;
            int d=lx[x]+ly[i]-w[x][i];
                if (!d) {sy[i]=1; if (lef[i]==0| |
                    DFS (Lef[i])) {lef[i]=x;
                return true;
            }} else if (slack[i]>d) {slack[i]=d;
}} return false;
    } void init () {int i,j;
    memset (ly,0,sizeof (ly));
    memset (lef,0,sizeof (lef));
                For (I=1, i<=n; i++) {for (j=1,lx[i]=0; j<=n; J + +) {if (Lx[i]>w[i][j])
            Continue
        LX[I]=W[I][J];
    }}} int KM () {int i,j;
    Init ();
    for (I=1; i<=n; i++) {    for (j=1; j<=n; J + +) {Slack[j]=inf;
            } while (1) {memset (sx,0,sizeof (SX));
            memset (sy,0,sizeof (SY));
            if (Dfs (i)) break;
            int d=inf;
            for (j=1; j<=n; J + +) {if (!sy[j]&&d>slack[j]) d=slack[j];
            } for (j=1; j<=n; J + +) {if (sx[j]) Lx[j]-=d;
                } for (j=1; j<=n; J + +) {if (!sy[j]) Slack[j]-=d;

            else Ly[j]+=d;
    }}} int result=0;
    for (I=1; i<=n; i++) {if (lef[i]>0) result+=w[lef[i]][i];
} return result;
    } int main () {int i,j;
    int ans; 
while (~SCANF ("%d", &n)) {for (i=1; i<=n; i++) {for (j=1; j<=n; J + +) {                scanf ("%d", &w[i][j]);
        }} ans=km ();
    printf ("%d\n", ans);
} return 0;
 }


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.