大白书P248有证明,此处贴出两种复杂度的方案,

n^4

大白书P350

n^3

 #include <algorithm>
#include <string.h>
#include <iostream>
#include <cstdio>
using namespace std;
/* KM算法
* 复杂度O(nx*nx*ny)
* 求最大权匹配
* 若求最小权匹配,可将权值取相反数,结果取相反数
* 点的编号从0开始
*/
const int N = ;
const int INF = 0x3f3f3f3f;
int nx,ny; //两边的点数
int W[N][N]; //二分图描述
int Left[N],Lx[N],Ly[N]; //y中各点匹配状态, x,y中的点标号
int slack[N];
bool S[N],T[N];
bool DFS(int x) {
S[x] = true;
for(int y = ; y < ny; y++){
if(T[y]) continue;
int tmp = Lx[x] + Ly[y] - W[x][y];
if(tmp == ){
T[y] = true;
if(Left[y] == - || DFS(Left[y])){
Left[y] = x;
return true;
}
}
else if(slack[y] > tmp)
slack[y] = tmp;
}
return false;
}
int KM(){
memset(Left, -, sizeof(Left));
memset(Ly,, sizeof(Ly));
for(int i = ;i < nx;i++){
Lx[i] = -INF;
for(int j = ;j < ny;j++)
if(W[i][j] > Lx[i])
Lx[i] = W[i][j];
}
for(int x = ;x < nx;x++){
for(int i = ;i < ny;i++)
slack[i] = INF;
while(true){
memset(S, false, sizeof(S));
memset(T, false, sizeof(T));
if(DFS(x)) break;
int d = INF;
for(int i = ;i < ny;i++)
if(!T[i] && d > slack[i])
d = slack[i];
for(int i = ;i < nx;i++)
if(S[i])
Lx[i] -= d;
for(int i = ;i < ny;i++){
if(T[i])Ly[i] += d;
else slack[i] -= d;
}
}
}
int res = ;
for(int i = ;i < ny;i++)
if(Left[i] != -)
res += W[Left[i]][i];
return res;
}
//HDU 2255
int main()
{
int n;
while(scanf("%d",&n) == ){
for(int i = ;i < n;i++)
for(int j = ;j < n;j++)
scanf("%d",&W[i][j]);
nx = ny = n;
printf("%d\n",KM());
}
return ;
}

KM算法模板的更多相关文章

  1. hdu 2255 奔小康赚大钱--KM算法模板

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2255 题意:有N个人跟N个房子,每个人跟房子都有一定的距离,现在要让这N个人全部回到N个房子里面去,要 ...

  2. HDU 2255 奔小康赚大钱 (KM算法 模板题)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  3. 二分图最大权值匹配 KM算法 模板

    KM算法详解+模板 大佬讲的太好了!!!太好了!!! 转载自:http://www.cnblogs.com/wenruo/p/5264235.html KM算法用来求二分图最大权完美匹配. 本文配合该 ...

  4. hdu 2255奔小康赚大钱 KM算法模板

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=2255 一,KM算法:(借助这个题写一下个人对km的理解与km模板) KM算法主要是用来求解图的最优匹 ...

  5. HDU 2255 奔小康,赚钱(KM算法模板)

    解决问题的思路: 二部图,正确的匹配,卡费用流,使用KM算法. #include <cstring> #include <algorithm> #include <cst ...

  6. HDU:2255-奔小康赚大钱(KM算法模板)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2255 奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Mem ...

  7. hdoj--2255--奔小康赚大钱(KM算法模板)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  8. HDU - 2255 奔小康赚大钱 KM算法 模板题

    HDU - 2255 题意: 分配n所房子给n个家庭,不同家庭对一所房子所需缴纳的钱是不一样的,问你应当怎么分配房子,使得最后收到的钱最多. 思路: KM算法裸题.上模板 #include <i ...

  9. UVA1349(带权二分图最大匹配 --> KM算法模板)

    UVA1349 题意:给定一些有向带权边,求出把这些边构造成一个个环,总权值最小 解法: 对于带权的二分图的匹配问题可以用通过KM算法求解. 要求最大权匹配就是初始化g[i][j]为0,直接跑就可以: ...

随机推荐

  1. Python中的str与bytes之间的转换的三种方法

    # bytes object b = b"example" # str object s = "example" # str to bytes sb = byt ...

  2. linux系统下邮件的发送

    在linux系统下发送邮件一般都要要求本地的机器必须安装和启动Sendmail服务,配置非常麻烦,而且会带来不必要的资源占用. 其实我还可以安装mailx软件,通过修改配置文件可以使用外部SMTP服务 ...

  3. css---媒体查询

    简单示例: <style type="text/css"> /* 小于 300的时候 */ @media screen and (max-width: 400px){ ...

  4. stat命令的实现-mysate

    任务详情 学习使用stat(1),并用C语言实现 提交学习stat(1)的截图 man -k,grep -r的使用 伪代码 产品代码mystate.c,提交码云链接 测试代码,mysate与stat( ...

  5. URAL 1277 - Cops and Thieves - [无向图点带权的最小点割]

    题目链接:https://cn.vjudge.net/problem/URAL-1277 The Galaxy Police (Galaxpol) found out that a notorious ...

  6. 【Python算法】遍历(Traversal)、深度优先(DFS)、广度优先(BFS)

    图结构: 非常强大的结构化思维(或数学)模型.如果您能用图的处理方式来规范化某个问题,即使这个问题本身看上去并不像个图问题,也能使您离解决问题更进一步. 在众多图算法中,我们常会用到一种非常实用的思维 ...

  7. centos7.2 源码编译安装php7.2.4 apache2.4.37 https证书安装

    一.php7.2.11源码安装 1.下载php7.2.11 wget http://cn2.php.net/downloads.php/php-7.2.11.tar.gz#### 2.安装依赖 yum ...

  8. JS中"属性"的用法

    JS的属性和C#有相似之处  ! 使用get和set来进行属性的获取和设置 var obj={ a:"1", get age(){ return obj.a; }, set age ...

  9. Python开发【笔记】:获取mp3文件获取信息

    import eyed3 def main(): mp3 = '8200031001_13429822982_1_00163e086216990b11e82403f00d3d9a.mp3' xx = ...

  10. CH0201 费解的开关 枚举

    正解:枚举 解题报告: 入门傻逼题,思维难度不高代码量极小,非常适合上手 然后傻逼的我第二次看这道题的时候依然没想到解法:D 没有办法,就想着写个笔记好歹记录一下以后多复习几次就记着了趴qwq 就是, ...