二分图 km算法模板

参考链接:https://blog.csdn.net/sixdaycoder/article/details/47720471

https://www.cnblogs.com/Lanly/p/6291214.html

https://www.cnblogs.com/Mychael/p/8994980.html

 

题目:hdu 2255

#include
#include
#include

using namespace std;

#define INF 0x3f3f3f3f

const int maxn=305;

int match[maxn],lx[maxn],ly[maxn],slack[maxn];
int G[maxn][maxn];

bool visx[maxn],visy[maxn];

int n,nx,ny;

bool findpath(int x)
{
    int tempdelta;

    visx[x]=1;

    for(int y=0;ytempdelta)
            slack[y]=tempdelta;
    }

    return false;
}

void KM()
{
    for(int x=0;x

 

题目:hdu 1533

题意:给幅图,有相同的人和相同的房子,让你把每个人移动到房子里,每移动一个单位,要出一块钱,问你:将所有人移动到房子里,最少移动步数是多少?

题解:直接建个二分图。

///题目要的是最短路,所以我们把值取负,就相当于km算法求了

#include
#include
#include

using namespace std;

#define INF 0x3f3f3f3f

const int maxn=110;

char op[maxn][maxn];

int match[maxn],lx[maxn],ly[maxn],slack[maxn];
bool visx[maxn],visy[maxn];

struct node{
    int x,y;
    node(){}
    node(int _x,int _y){
        x=_x;y=_y;
    }
}house[maxn],people[maxn]; ///存储房子,人的位置

int G[maxn][maxn],nx,ny;

bool findpath(int x)
{
    int tempdelta;

    visx[x]=1;

    for(int y=0;ytempdelta) slack[y]=tempdelta;
    }

    return false;


}

void KM()
{
    for(int x=0;x

 

你可能感兴趣的:(数据结构)