KM匹配板子
/* gyt
Live up to every day */
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<cstring>
#include<queue>
#include<set>
#include<string>
#include<map>
#include <time.h>
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = ;
const ll maxm = 1e7;
const int mod = 1e9+;
const int INF = 0x3f3f3f;
const ll inf = 1e14 + ;
const db eps = 1e-;
const ll Max=1e19;
int mapp[maxn][maxn], visx[maxn], visy[maxn];
int lx[maxn], ly[maxn];
int match[maxn];
int n; int hungry(int u) {
visx[u] = true;
for(int i = ; i < n; ++i)
{
if(!visy[i] && lx[u] + ly[i] == mapp[u][i])
{
visy[i] = true;
if(match[i] == - || hungry(match[i]))
{
match[i] = u;
return true;
}
}
}
return false;
}
void KM() {
int temp;
memset(lx, , sizeof(lx)); //初始化顶标
memset(ly, , sizeof(ly)); //ly[i]为0
for(int i = ; i < n; ++i) //lx[i]为权值最大的边
for(int j = ; j < n; ++j)
lx[i] = max(lx[i], mapp[i][j]);
for(int i = ; i < n; ++i) //对n个点匹配
{
while()
{
memset(visx, false, sizeof(visx));
memset(visy, false, sizeof(visy));
if(hungry(i)) //匹配成功
break;
else //匹配失败,找最小值
{
temp = INF;
for(int j = ; j < n; ++j) //x在交错树中
if(visx[j])
for(int k = ; k < n; ++k) //y在交错树外
if(!visy[k] && temp > lx[j] + ly[k] - mapp[j][k])
temp = lx[j] + ly[k] - mapp[j][k];
for(int j = ; j < n; ++j) //更新顶标
{
if(visx[j])
lx[j] -= temp;
if(visy[j])
ly[j] += temp;
}
}
}
}
}
void solve() {
int ans;
while(scanf("%d", &n) != EOF)
{
ans = ;
memset(match, -, sizeof(match));
for(int i = ; i < n; i++)
for(int j = ; j < n; j++)
scanf("%d", &mapp[i][j]);
KM();
for(int i = ; i < n; i++) //权值相加
ans += mapp[match[i]][i];
printf("%d\n", ans);
}
}
int main() {
int t=;
//freopen("in.txt", "r", stdin);
//scanf("%d", &t); while(t--)
solve();
}
KM匹配板子的更多相关文章
- 【转】KM匹配题集
转自:http://blog.csdn.net/shahdza/article/details/7779324 [HDU]2255 奔小康赚大钱 模板题★1533 Going Home 模板题★242 ...
- HDU5740 Glorious Brilliance【最短路 KM匹配】
HDU5740 Glorious Brilliance 题意: 给出一张不一定合法的染色图,每次可以交换相邻两点的颜色,问最少多少次能使染色图合法 合法的染色图相邻点的颜色不能相同 题解: 首先要确定 ...
- hdu_2255_奔小康赚大钱(KM带权二分匹配板子)
题目连接:hdu_2255_奔小康赚大钱 存个板子 /* 其实在求最大 最小的时候只要用一个模板就行了, 把边的权值去相反数即可得到另外一个.求结果的时候再去 相反数即可,最大最小有一些地方不同.. ...
- Assignment HDU - 2853(二分图匹配 KM 新边旧边)
传送门: Assignment HDU - 2853 题意:题意直接那松神的题意了.给了你n个公司和m个任务,然后给你了每个公司处理每个任务的效率.然后他已经给你了每个公司的分配方案,让你求出最多能增 ...
- POJ 2516 Minimum Cost (KM最优匹配)
题意:有N家家店,每家店都对K种货物有需求:同时有M家仓库,对K钟货物有供应.对于每种货物,每个仓库送至每家店都有自己的单位费用.求满足所有店所有货物的最小费用 分析:对于每一种货物,如果总需求大于总 ...
- HDU 2255.奔小康赚大钱 最大权匹配
奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- UOJ80 二分图最大权匹配
草,学了一下午假板子,sb博客害人 题目大意: 一个教室有\(n\)个男生和\(m\)个女生,某些男女之间愿意早恋(雾),其早恋好感度为\(w_i\),问怎样让男女配对使得班里好感度之和最大 \(n\ ...
- 【HDU 2853】 KM算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2853 题意:有n个公司,m个任务,每个公司做每个任务都有一个效率值,最开始每个公司都指派了一个任务,现 ...
- 【HDU 2853】Assignment (KM)
Assignment Problem Description Last year a terrible earthquake attacked Sichuan province. About 300, ...
随机推荐
- pandas中关于DataFrame 去除省略号
#显示所有列 pd.set_option('display.max_columns', None) #显示所有行 pd.set_option('display.max_rows', None) #设置 ...
- Python hash() 函数
Python hash() 函数 Python 内置函数 描述 hash() 用于获取取一个对象(字符串或者数值等)的哈希值. 语法 hash 语法: hash(object) 参数说明: obje ...
- python字典设置初始值setdefault()与get()
L = ['you','me','you','me','you','me','you'] D = {} for i in L: D[i] += 1 print(D) 执行以下代码会发生错误 Trace ...
- Chrome控制台格式化输出
一 格式化输出文字 console.log('%c你好','color:green;'); console.log('%c你好%c啊','color:green;','color:blue;'); 二 ...
- 如何在比较1.5 len的次数下,找到整型数组最大最小值
2016-11-11 #include <iostream> #include<stdlib.h> #include<stdio.h> using namespac ...
- macOS 升级后重装命令行工具的问题
问题背景 最近升级个人macbook 从 10.13 到 10.14 在终端输入 git 不能用了,发现是重装操作系统后原来的 Command Line Tools 被自动卸载了, 采用 xcode- ...
- python:在for遍历list时使用remove出现的问题以及解析(转)
原文地址:http://blog.csdn.net/circle2015/article/details/64444300最近面试时,面试官在纸上写了几行代码,说是很简单的问题,我看后直接懵逼...如 ...
- ROS launch总结
1 运行Launch文件2 新建Launch文件3 在namespace中启动nodes 4 remapping names 5 其他的launch元素 1 运行Launch文件 Launch文 ...
- Query to find the eligible indexes for rebuilding
Query to find the eligible indexes for rebuilding The following script can be used to determine whic ...
- Java并发集合(三)-ConcurrentHashMap分析和使用
1 http://ifeve.com/hashmap-concurrenthashmap-%E7%9B%B8%E4%BF%A1%E7%9C%8B%E5%AE%8C%E8%BF%99%E7%AF%87% ...