[ACM] HDU 2255 Ben-off make a lot of money (binary graph maximum weight matching, km algorithm)

Source: Internet
Author: User

Ben-off make a lot of money

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 Dinner


Problem Solving Ideas:

The maximum weight matching problem of bare binary graphs is compared.

Read a lot of information, feel or the KM algorithm mastery is not very thorough ...

Resources:

Http://www.cppblog.com/MatoNo1/archive/2012/04/26/151724.html

http://blog.csdn.net/liguanxing/article/details/5665646

http://cuitianyi.com/blog/%E6%B1%82%E6%9C%80%E5%A4%A7%E6%9D%83%E4%BA%8C%E5%88%86%E5%8C%B9%E9%85%8D%E7%9A%84km% e7%ae%97%e6%b3%95/

http://blog.csdn.net/niushuai666/article/details/7171880

Code:

#include <iostream> #include <stdio.h> #include <algorithm> #include <string.h>using namespace std;const int Maxn=302;const int Inf=0x3f3f3f;int nx,ny;//both left and right points int g[maxn][maxn];//adjacency Matrix int linked[maxn];// The right point and the left point connect int lx[maxn],ly[maxn];//the label int slack[maxn];//slack[j] for the right point J for all the edges that are not on the exported sub-graph Lx[i]+ly[j]-w[i][j]    The minimum value of bool Visx[maxn],visy[maxn];bool DFS (int x)//hungary seeking augmented path {visx[x]=true;        for (int y=0;y<ny;y++) {if (visy[y]) continue;        int tmp=lx[x]+ly[y]-g[x][y];            if (tmp==0) {visy[y]=true; if (linked[y]==-1| |                DFS (Linked[y])) {linked[y]=x;            return true;    }} else if (slack[y]>tmp) slack[y]=tmp; } return false;}    int KM () {memset (linked,-1,sizeof (linked));    memset (ly,0,sizeof (ly));        for (int i=0;i<nx;i++) {lx[i]=-inf; for (int j=0;j<ny;j++) if (G[i][j]>lx[i]) lx[i]=g[i][j];        } for (int x=0;x<nx;x++) {for (int y=0;y<ny;y++) Slack[y]=inf;            while (true) {memset (visx,0,sizeof (VISX));            memset (visy,0,sizeof (Visy));            if (DFS (x)) break;            int d=inf;            for (int y=0;y<ny;y++) if (!visy[y]&&d>slack[y]) d=slack[y];            for (int i=0;i<nx;i++) if (visx[i]) Lx[i]-=d;                for (int i=0;i<ny;i++) {if (visy[i]) Ly[i]+=d;            else Slack[i]-=d;    }}} int ans=0;    for (int y=0;y<ny;y++) {if (linked[y]!=-1) ans+=g[linked[y]][y]; } return ans;    int main () {int n; while (scanf ("%d", &n)!=eof) {for (Int. i=0;i<n;i++) for (int j=0;j<n;j++) scanf ("%d        ", &g[i][j]);        Nx=ny=n;    printf ("%d\n", KM ()); } rEturn 0;} 



[ACM] HDU 2255 Ben-off make a lot of money (binary graph maximum weight matching, 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.