大白书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. docker link 过时不再用了?那容器互联、服务发现怎么办?

    在 1-2 年前,Docker 所有容器都连接于默认的桥接网络上,也就是很多老文章鼓捣的 docker0 桥接网卡.因此实际上默认情况下所有容器都是可以互联的,没有隔离,当然这样安全性不好.而服务发现 ...

  2. 阿里云服务器如何设置IPV6通过appstore的审核

    苹果上架要求:要求支持IPV6only(因为阿里云主机没有IPV6only) 确认IPV6是否开启: 方式1:使用ifconfig查看自己的IP地址是否含有IPv6地址. 方式2.查看服务监听的IP中 ...

  3. HDU 3903 Trigonometric Function(数学定理)

    Trigonometric Function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Oth ...

  4. POJ--1936 All in All(水题,暴力即可)

    All in All Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 30543 Accepted: 12723 Descript ...

  5. matplotlib的下载和安装方法

    官网:http://matplotlib.org/ Installation节 Visit the Matplotlib installation instructions. Installing节 ...

  6. Oracle数据库中的优化方案

    来自: http://woainichenxueming.iteye.com/blog/726541 一. 优化oracle中的sql语句,提高运行效率 1. 选择最有效率的表名顺序(只在基于规则的优 ...

  7. linux系统下top命令参数详解

    简介 top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. top显示系统当前的进程和其他状况,是一个动态显示过程,即可以通过用户按 ...

  8. Java中子类是否可以继承父类的static变量和方法而呈现多态特性

    静态方法 通常,在一个类中定义一个方法为static,那就是说,无需本类的对象即可调用此方法,关于static方法,声明为static的方法有以下几条限制: 它们仅能调用其他的static 方法. 它 ...

  9. 币安Binance API

    本文介绍币安Binance API General API Information The base endpoint is: https://api.binance.com All endpoint ...

  10. vue - vue-cli脚手架项目中组件的使用

    在webpack-simple模板中,包括webpck模板.一个.vue文件就是一个组件. 为什么会这样呢?因为webpack干活了!webpack的将我们所有的资源文件进行打包.同时webpack还 ...