大白书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. Egret容器的鼠标默认事件

    容器的鼠标默认事件   touchEnabled touchChildren touchThrough DisplayObject false \ \ DisplayObjectContainer f ...

  2. 【CF875F】Royal Questions 最小生成基环树森林

    [CF875F]Royal Questions 题意:国王的n个王子该结婚了!现在从外国来了m位公主,第i位公主的嫁妆是wi.由于进步思想的传播,每个公主在选择配偶的事情上是有自主权的,具体地,每个公 ...

  3. Xcode - LLDB调试技巧

    LLDB是Xcode默认的调试器,它与LLVM编译器一起,带给我们更丰富的流程控制和数据检测的调试功能.平时用Xcode运行程序,实际走的都是LLDB.熟练使用LLDB,可以让你debug事半功倍. ...

  4. mysqladmin 命令详解

    mysqladmin是一个执行管理操作的客户端程序.它可以用来检查服务器的配置和当前状态.创建和删除数据库等. mysqladmin 工具的使用格式: mysqladmin [option] comm ...

  5. Python通过正则表达式去除(过滤)HTML标签,提取文字

    # -*- coding: utf-8-*- import re ##过滤HTML中的标签 #将HTML中标签等信息去掉 #@param htmlstr HTML字符串. def filter_tag ...

  6. 2018牛客网暑期ACM多校训练营(第二场) J - farm - [随机数哈希+二维树状数组]

    题目链接:https://www.nowcoder.com/acm/contest/140/J 时间限制:C/C++ 4秒,其他语言8秒 空间限制:C/C++ 262144K,其他语言524288K ...

  7. POJ_3616_Milking Time

    Milking Time Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10841   Accepted: 4564 Des ...

  8. UESTC - 1057 秋实大哥与花 线段树模板题

    http://acm.uestc.edu.cn/#/problem/show/1057 题意:给你n个数,q次操作,每次在l,r上加上x并输出此区间的sum 题解:线段树模板, #define _CR ...

  9. 如何删除word中多余的空格和空行

    去除word中多余的空格及空行 一.去掉表格和格式 为了版面的整齐,网页文档都是以表格的形式存在的,只是一般情况下表格的颜色被设为无色或表格宽度被设为0,所以我们在网页上看不到表格.另外,网 页文档中 ...

  10. LightOJ 1027 - A Dangerous Maze(求期望)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1027 题意:又一个迷宫,有n个门,每个门又一个值num,如果num>0 说明在n ...