当前位置: 首页 > news >正文

KM算法模板(二分图的最大权匹配)

又掌握了一个模板~~~其实是差不多理解了,比较菜..

二次元霸主写博客就是强啊: http://www.cnblogs.com/wenruo/p/5264235.html

但...不敢想象博主是男是女。


依旧改成自己喜欢的模板格式~

//模板HDU-2255 
#include <algorithm>
#include <iostream>
#include <string.h>
#include <cstdio>

using namespace std;
const int maxn = 305;
const int inf = 0x3f3f3f3f;

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

int dfs(int cur)
{
	visx[cur] = 1;
	for(int i = 1; i <= n; ++i)
	{
		if(visy[i]) continue;
		int gap = lx[cur] + ly[i] - weight[cur][i];
		if(gap == 0)
		{
			visy[i] = 1;
			if(match[i] == -1 || dfs(match[i]))
			{
				match[i] = cur;
				return 1;
			}
		}
		else
		{
			slack[i] = min(slack[i], gap);
		}
	}
	return 0;
}

int KM()
{
	memset(match, -1, sizeof match);
	memset(ly, 0, sizeof ly);
	
	for(int i = 1; i <= n; ++i)
	{
		lx[i] = weight[i][1];
		for(int j = 2; j <= n; ++j)
		lx[i] = max(lx[i], weight[i][j]);
	}
	
	for(int i = 1; i <= n; ++i)
	{
	//	fill(slack, slack+n, inf);
		memset(slack, 0x3f, sizeof slack);
		while(1)
		{
			memset(visx, 0, sizeof visx);
			memset(visy, 0, sizeof visy);
			
			if(dfs(i)) break; 
			
			int key = inf;
			for(int j = 1; j <= n; ++j)
				if(!visy[j]) key = min(key, slack[j]);
			
			for(int j = 1; j <= n; ++j)
			{
				if(visx[j]) lx[j] -= key;
				
				if(visy[j]) ly[j] += key;
				else slack[j] -= key;
			}
		}
	}
	int ans = 0;
	for(int i = 1; i <= n; ++i)
	{
		ans += weight[match[i]][i];
	}
	return ans;
}
int main()
{
	while(scanf("%d", &n) != EOF)
	{
		for(int i = 1; i <= n; ++i)
			for(int j = 1; j <= n; ++j)
				scanf("%d", &weight[i][j]);	
		printf("%d\n", KM());
	} 
	return 0;
}

当交这个模板题的时候T了若干次,原因是,用到了一个fill(a, a+N, k)的函数,感觉这个函数内部应该就是一个for循环赋值的效率,肯定是不如memset效率高的,唯一优点就是能填充任何值。memset按字节进行填充,只能 填充0, -1, 0x3f(int范围的0x3f3f3f3f), 0x3f3f(long long范围的0x3f3f3f3f3f3f3f3f[实践证明可用]) 其他就不可以了。


fill函数可参见: http://blog.csdn.net/liuchuo/article/details/52296646

memset函数可参见: http://www.cnblogs.com/handsomecui/p/4723949.html


继续加油~

相关文章:

  • 《社交红利》读书总结--如何从微信微博QQ空间等社交网络带走海量用户、流量与收入...
  • 三分法求凸性函数极大极小值
  • CodeForces 612D
  • 【leetcode】Factorial Trailing Zeroes(easy)
  • 数学知识小记
  • [SQL]mysql密码读取
  • CodeForces 616E(数学规律)
  • MongoDB aggregate 运用篇(转)
  • 二分图的概念汇总
  • HDU 1429(状压+bfs)
  • 树莓派系统安装初级教程
  • POJ 2528(线段树+离散化)
  • hadoop问题与解决办法
  • HDU 4725(最短路之建图难点)
  • QDU首届易途杯大赛-kk与cillyb的荣誉之战
  • 【跃迁之路】【519天】程序员高效学习方法论探索系列(实验阶段276-2018.07.09)...
  • Puppeteer:浏览器控制器
  • win10下安装mysql5.7
  • 高性能JavaScript阅读简记(三)
  • AI又要和人类“对打”,Deepmind宣布《星战Ⅱ》即将开始 ...
  • Mac 上flink的安装与启动
  • 继 XDL 之后,阿里妈妈开源大规模分布式图表征学习框架 Euler ...
  • #1015 : KMP算法
  • #define,static,const,三种常量的区别
  • #if #elif #endif
  • $.each()与$(selector).each()
  • (八)Docker网络跨主机通讯vxlan和vlan
  • (解决办法)ASP.NET导出Excel,打开时提示“您尝试打开文件'XXX.xls'的格式与文件扩展名指定文件不一致
  • (九)信息融合方式简介
  • (论文阅读笔记)Network planning with deep reinforcement learning
  • (每日持续更新)jdk api之StringBufferInputStream基础、应用、实战
  • .gitignore
  • .mat 文件的加载与创建 矩阵变图像? ∈ Matlab 使用笔记
  • .NET Core中Emit的使用
  • .NET Framework 和 .NET Core 在默认情况下垃圾回收(GC)机制的不同(局部变量部分)
  • .NET 将多个程序集合并成单一程序集的 4+3 种方法
  • .NET 命令行参数包含应用程序路径吗?
  • .Net 中Partitioner static与dynamic的性能对比
  • .net网站发布-允许更新此预编译站点
  • ??eclipse的安装配置问题!??
  • @Autowired @Resource @Qualifier的区别
  • @Autowired标签与 @Resource标签 的区别
  • @DependsOn:解析 Spring 中的依赖关系之艺术
  • @Transactional 详解
  • [ 环境搭建篇 ] 安装 java 环境并配置环境变量(附 JDK1.8 安装包)
  • [.net 面向对象程序设计进阶] (19) 异步(Asynchronous) 使用异步创建快速响应和可伸缩性的应用程序...
  • [145] 二叉树的后序遍历 js
  • [Arduino学习] ESP8266读取DHT11数字温湿度传感器数据
  • [ASP.NET MVC]如何定制Numeric属性/字段验证消息
  • [BZOJ1053][HAOI2007]反素数ant
  • [C#] 如何调用Python脚本程序
  • [CodeForces-759D]Bacterial Melee
  • [DM复习]关联规则挖掘(下)
  • [Intel Edison开发板] 05、Edison开发基于MRAA实现IO控制,特别是UART通信
  • [ios-必看] IOS调试技巧:当程序崩溃的时候怎么办 iphone IOS