Bipartite Graph km algorithm HDU 2255

Source: Internet
Author: User

I learned the KM Algorithm in Baidu encyclopedia ..

The KM algorithm is the maximum weight for a complete match.

KM algorithm flow:

1. initialize two vertices of a bipartite graph.

2. Search for complete matching using the Hungary algorithm using the KM Algorithm

3. Modify the vertex value if not found.

4. Repeat (2) (3) until a complete matching of an equal subgraph is found;

HDU 2255 is a bare question ..

Unoptimized version:

View code

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int match[1000];
int mp[400][400];
bool visitx[400], visity[400];
int xx[400], yy[400];
const int inf = 0x7f7f7f7f;
int minx, N;

void init( )
{
memset(match, -1, sizeof(match));
memset(mp, 0, sizeof(mp));
memset(xx, 0, sizeof(xx));
memset(yy, 0, sizeof(yy));
}

bool Hungary( int x)
{
visitx[x] = true;
for( int i = 1; i <= N; i++)
{
if( visity[i] ) continue;
int temp = xx[x] + yy[i] - mp[x][i];
if( temp == 0 )
{
visity[i] = true;
if( match[i] == -1 || Hungary(match[i]))
{
match[i] = x;
return true;
}
}
else
{
if( temp < minx )
minx = temp;
}
}
return false;
}

void KM( )
{
for( int i = 1; i <= N; i++)
{
for( int j = 1; j <= N; j++)
{
if( mp[i][j] > xx[i] )
xx[i] = mp[i][j];
yy[j] = 0;
}
}
for( int i = 1; i <= N; i++)
{
while( 1 )
{
memset(visitx, 0, sizeof(visitx));
memset(visity, 0, sizeof(visity));
minx = inf;
if( Hungary( i ) )
break;
else
{
for(int j = 1; j <= N; j++)
{
if( visitx[j] )
xx[j] -= minx;
if( visity[j] )
yy[j] += minx;

}
}
}
}
int sum = 0;
for( int i = 1; i <= N; i++)
sum += mp[match[i]][i];
printf("%d\n", sum);

}

int main( )
{

while( scanf("%d",&N) != EOF )
{
init( );
for( int i = 1; i <= N; i++)
for( int j = 1; j <= N; j++)
scanf("%d",&mp[i][j]);
KM( );
}
}

Optimized version, more time to run, optimized

 

View code

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;

int match[310];
int xx[310], yy[310];
int visitx[310], visity[310];
int mp[310][310];
int slack[310];
const int inf = 0x7f7f7f7f;
int N;

void init( )
{
memset(match, -1, sizeof(match));
memset(xx, 0, sizeof(xx));
memset(yy, 0, sizeof(yy));
memset(mp, 0, sizeof(mp));
}

bool Hungary( int x)
{
visitx[x] = true;
for( int i = 1; i <= N; i++)
{
if( visity[i] )
continue;
int temp = xx[x] + yy[i] - mp[x][i];
if( temp == 0)
{
visity[i] = 1;
if( match[i] == -1 || Hungary(match[i]))
{
match[i] = x;
return true;
}
}
else
slack[i] = min(slack[i], temp);
}
return false;
}

void KM( )
{
for( int i = 1; i <= N; i++)
for( int j = 1; j <= N; j++)
{
if( mp[i][j] > xx[i] )
xx[i] = mp[i][j];
yy[j] = 0;
}
for( int i = 1; i <= N; i++)
{
for( int j = 1; j <= N; j++)
slack[j] = inf;
while( 1 )
{
memset(visitx, 0, sizeof(visitx));
memset(visity, 0, sizeof(visity));
if( Hungary(i) )
break;
else
{
int temp = inf;
for( int j = 1; j <= N; j++)
{
if( !visity[j] && slack[j] < temp )
temp = slack[j];
}
for( int k = 1; k <= N; k++)
{
if(visitx[k])
xx[k] -= temp;
if(visity[k])
yy[k] += temp;
else
slack[k] -= temp;
}
}
}
}
int sum = 0;
for( int i = 1; i <= N; i++)
sum += mp[match[i]][i];
printf("%d\n", sum);

}
int main( )
{
while( scanf("%d",&N) != EOF )
{
init( );
for( int i = 1; i <= N; i++)
for( int j = 1; j <= N; j++)
scanf("%d",&mp[i][j]);
KM( );
}
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.