[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 There is a very wealthy village in a faraway place, and one day the village chief decided to reform the system: Another allocation of the house.


It's a big deal, it's about the people's housing problem.

The village co-owned N rooms, just have n common people, considering every home to have a room to live (if there are people without houses to live.) Easy causes unrest), each family must be assigned to a house and can only get a house.
On the one hand, village chiefs and other village leaders hope to get the most benefit, so that the village's institutions will be rich. Because the people are richer, they can have a certain price for each house in their economic range, for example, there are 3 houses, one people can 100,000 for the first room, 20,000 for the 2nd, 200,000 for the 3rd room. (in their economy, of course). The problem now is how village leaders allocate their homes to the highest 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 includes multiple sets of test examples. The first row of each group of data entered N, indicating the number of houses (also the number of people home), followed by n rows, the number of n per row indicates the price of the first village name of the J Room (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

Source

Field=problem&key=hdoj+2008+summer+exercise%a3%a84%a3%a9-+buffet+dinner&source=1&searchmode=source "Style=" Color:rgb (26,92,200); Text-decoration:none ">hdoj Summer Exercise (4)-Buffet Dinner


Problem Solving Ideas:

The problem of the maximum weight matching of the bare binary graph.

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

。。

References:

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 of the connection int lx[maxn],ly[maxn];//the right point of the label int slack[maxn];//slack[j] means that all of the points of Point J on the same side are not on the edges of 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.