/*  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匹配板子的更多相关文章

  1. 【转】KM匹配题集

    转自:http://blog.csdn.net/shahdza/article/details/7779324 [HDU]2255 奔小康赚大钱 模板题★1533 Going Home 模板题★242 ...

  2. HDU5740 Glorious Brilliance【最短路 KM匹配】

    HDU5740 Glorious Brilliance 题意: 给出一张不一定合法的染色图,每次可以交换相邻两点的颜色,问最少多少次能使染色图合法 合法的染色图相邻点的颜色不能相同 题解: 首先要确定 ...

  3. hdu_2255_奔小康赚大钱(KM带权二分匹配板子)

    题目连接:hdu_2255_奔小康赚大钱 存个板子 /* 其实在求最大 最小的时候只要用一个模板就行了, 把边的权值去相反数即可得到另外一个.求结果的时候再去 相反数即可,最大最小有一些地方不同.. ...

  4. Assignment HDU - 2853(二分图匹配 KM 新边旧边)

    传送门: Assignment HDU - 2853 题意:题意直接那松神的题意了.给了你n个公司和m个任务,然后给你了每个公司处理每个任务的效率.然后他已经给你了每个公司的分配方案,让你求出最多能增 ...

  5. POJ 2516 Minimum Cost (KM最优匹配)

    题意:有N家家店,每家店都对K种货物有需求:同时有M家仓库,对K钟货物有供应.对于每种货物,每个仓库送至每家店都有自己的单位费用.求满足所有店所有货物的最小费用 分析:对于每一种货物,如果总需求大于总 ...

  6. HDU 2255.奔小康赚大钱 最大权匹配

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

  7. UOJ80 二分图最大权匹配

    草,学了一下午假板子,sb博客害人 题目大意: 一个教室有\(n\)个男生和\(m\)个女生,某些男女之间愿意早恋(雾),其早恋好感度为\(w_i\),问怎样让男女配对使得班里好感度之和最大 \(n\ ...

  8. 【HDU 2853】 KM算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2853 题意:有n个公司,m个任务,每个公司做每个任务都有一个效率值,最开始每个公司都指派了一个任务,现 ...

  9. 【HDU 2853】Assignment (KM)

    Assignment Problem Description Last year a terrible earthquake attacked Sichuan province. About 300, ...

随机推荐

  1. java swing示例

    该范例主要是JFrame(框架)和Jpanel(画板),在Jpanel容器上添加控件,然后再把Jpanel放进JFrame的容器里面. FrameDemo.java import java.awt.D ...

  2. 完整性约束&外键变种三种关系&数据的增删改

    完整性约束 本节重点: not null 与 default unique primary auto_increment foreign key 一.介绍 约束条件与数据类型的宽度一样,都是可选参数 ...

  3. js回调函数,检测这个值是否重复

    //校验提交的数据是否重复 /** * url:后端的查询地址 * filedVal: 要传到后台的值 * ele:要绑定显示的元素,一般就是当前的input就可以,直接在其后边追加显示 * fn:回 ...

  4. Gym - 101911B Glider(前缀和+二分)

    传送门:点我 A plane is flying at a constant height of hh meters above the ground surface. Let's consider ...

  5. 图片Bitmap在本地的存储与读取 File

    将Bitmap存储到本地: public void SaveImage(Bitmap image, String user_id){ //照片通常存在DCIM文件夹中 String sdCardDir ...

  6. 【nginx】配置详解

    nginx作为web server,是最常用的网络服务器.来看下它的配置文件. 实验1:最基础的配置(可访问静态资源) 包括listen,server_name,root,index,access_l ...

  7. Java03-Java语法基础(二)运算符

    Java语法基础(二)运算符 一.运算符 1.算数运算符:+.-.*./.% 1)双目运算符:二元运算符,参加运算的对象有两个(+.-.*./.%) 2)单目运算符:一元运算符,参加运算的对象只有一个 ...

  8. linux命令学习之:cd

    cd命令用来切换工作目录至dirname. 其中dirName表示法可为绝对路径或相对路径.若目录名称省略,则变换至使用者的home directory(也就是刚login时所在的目录).另外,~也表 ...

  9. certificate verify failed (https://gems.ruby-china.org/specs.4.8.gz)

    redis集群配置中 >gem sources -a https://ruby.taobao.org/ Error fetching https://gems.ruby-china.org/: ...

  10. ret和retf

    ret指令用栈中的数据,修改IP的内容,从而实现近转移; retf指令用栈中的数据,修改CS和IP的内容,从而实现远转移. CPU执行ret指令时,进行下面两步操作: (IP) = ((ss)*16+ ...