Best match in Bipartite Graph-8616 Auto Rally match

Source: Internet
Author: User

8616 Auto Rally


Time Limit: 500 ms memory limit: 1000 K

Submissions: 3 passes: 3

Question type: Programming Language: Unlimited

Description

The scau team is going to participate in the rally competition. In a rally, each car requires a driver and a navigator. There are n drivers and N navigators in the scau team, each of which has a tacit understanding of each navigator. Now we need to find a certain pair of N pairs, this maximizes the sum of the tacit values of the N pairs.

Input

Multiple cases. The first line of each case is an integer n and the range is (1 <= n <= 16 ). Next there are n rows, each row has n numbers, and the J number in line I represents the tacit understanding of the I driver and the J navigator. There are multiple groups of test data. When n = 0, the data ends and this line does not need to be processed.Output

Output the maximum value of the sum of tacit values after pairing.

Sample Input

1 5 2 10 20 25 5 0  

Sample output

5 45  

Source

CPP

This is obviously the best match for the bipartite graph. Obviously, I won't. I decided to find the information. After understanding and researching the standard process, I have used it as my own. This is the legendary km algorithm, understanding is a bit painful, because I often talk about what the tree is, and I don't know what it is, but I don't know what it is. I can think of the Code directly. below is my code...
#include<string>#include<algorithm>#define N 100using namespace std;int gp[N][N],vx[N],vy[N],fx[N],fy[N],n,m[N];int dfs(int u){vx[u]=1;for(int i=0;i<n;i++){if(vy[i]==0&&fx[u]+fy[i]==gp[u][i]){vy[i]=1;if(m[i]==-1||dfs(m[i])){m[i]=u;return 1;}}}return 0;}int count(){int i,j,k,d,ans;for(i=0;i<n;i++){fx[i]=-0x7fffffff;fy[i]=0;for(j=0;j<n;j++){if(fx[i]<gp[i][j]){fx[i]=gp[i][j];}}}memset(m,-1,sizeof(m));for(int u=0;u<n;u++){memset(vx,0,sizeof(vx));memset(vy,0,sizeof(vy));while(!dfs(u)){d=0x7fffffff;for(i=0;i<n;i++){if(vx[i])for(j=0;j<n;j++){if(!vy[j]){if(d>(fx[i]+fy[j]-gp[i][j]))d=fx[i]+fy[j]-gp[i][j];}}}for(i=0;i<n;i++){if(vx[i])fx[i]-=d,vx[i]=0;if(vy[i])fy[i]+=d,vy[i]=0;}}}ans=0;for(i=0;i<n;i++){ans+=gp[m[i]][i];}return ans;}int main(){int i,j;while(1){scanf("%d",&n);if(n==0)return 0;for(i=0;i<n;i++)for(j=0;j<n;j++){scanf("%d",&gp[i][j]);}printf("%d\n",count());}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.