[kuangbin带你飞]专题十 匹配问题 二分图最大权匹配
二分图最大权匹配有km算法和网络流算法
km算法模板默认解决最大权匹配的问题 而使用最小费用最大流 是解决最小权匹配问题
这两种办法都可以求最大最小权 需要两次取反
TAT 感觉讲km会很难的样子...
P hdu2255
km的模板题
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<map>
#include<string>
#include<vector>
#include<queue>
#include<iostream>
using namespace std;
#define L long long
bool visx[305] , visy[305] ;
int sla[305] ;
int linker[305];
int lx[305] , ly[305];
int n ;
int c[305][305]; bool fin(int u ){
visx[u] = false ;
for(int v = 1; v <= n ; v ++ ){
if(visy[v]){
int tmp = lx[u] + ly[v] - c[u][v] ;
if(tmp == 0){
visy[v] = false ;
if(linker[v] == -1 || fin(linker[v])){
linker[v] = u ;
return true ;
}
}
else {
sla[v] = min (sla[v] , tmp ) ;
}
}
}
return false ;
}
int km(){
memset(linker, -1 ,sizeof(linker));
memset(ly , 0 ,sizeof(ly)) ;
for(int i = 1; i <= n ; i ++ ){
lx[i] = 0 ;
for(int j = 1; j <= n; j ++ ){
lx[i] = max(lx[i] , c[i][j]) ;
}
}
for(int i = 1; i<= n ; i ++ ){
for(int j = 1 ; j <= n ; j ++ )sla[j] = 999999999;
while(true){
memset(visx , true , sizeof(visx)) ;
memset(visy , true , sizeof(visy)) ;
if(fin(i))break ;
int d = 999999999 ;
for(int j = 1; j<= n ; j ++ ){
if(visy[j]){
d = min(d , sla[j]) ;
}
}
for(int j = 1; j<= n ; j ++ ){
if(visx[j] == false){
lx[j] -= d;
}
if(visy[j] == false){
ly[j] += d ;
}
else {
sla[j] -= d;
}
}
}
}
int res = 0 ;
for(int j = 1; j <= n ; j ++ ){
if(linker[j] != -1){
res += c[linker[j]][j];
}
}
return res ;
}
int main(){
while(scanf("%d",&n)!=EOF){
for(int i = 1; i <= n ;i ++) {
for(int j = 1; j <= n ; j ++ )
scanf("%d",&c[i][j]) ;
}
int ans = km() ;
printf("%d\n",ans );
}
}
Q hdu3488
是一个要求将所有的点连为一些环 并且边权的加和最小
环的合并可以用最小费用最大流来做
可以转化为 每个点上都连着两条边 使总边和最小 即 求最小权匹配
建图 是左边n个点 右边n个点 那么 最后可以求出一个完备匹配 左边的x点和右边的x点上都有一条边相连
km算法和网络列解法都是可以的 km100+ms 费用流900+ms
km :
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<map>
#include<string>
#include<vector>
#include<queue>
#include<iostream>
using namespace std;
#define L long long
bool visx[305] , visy[305] ;
int sla[305] ;
int linker[305];
int lx[305] , ly[305];
int n , m ;
int c[305][305];
bool fin(int u ){
visx[u] = false ;
for(int v = 1; v <= n ; v ++ ){
if(visy[v]){
int tmp = lx[u] + ly[v] - c[u][v] ;
if(tmp == 0){
visy[v] = false ;
if(linker[v] == -1 || fin(linker[v])){
linker[v] = u ;
return true ;
}
}
else {
sla[v] = min (sla[v] , tmp ) ;
}
}
}
return false ;
}
int km(){
memset(linker, -1 ,sizeof(linker));
memset(ly , 0 ,sizeof(ly)) ;
for(int i = 1; i <= n ; i ++ ){
lx[i] = -999999999 ;
for(int j = 1; j <= n; j ++ ){
lx[i] = max(lx[i] , c[i][j]) ;
}
}
for(int i = 1; i<= n ; i ++ ){
for(int j = 1 ; j <= n ; j ++ )sla[j] = 999999999;
while(true){
memset(visx , true , sizeof(visx)) ;
memset(visy , true , sizeof(visy)) ;
if(fin(i))break ;
int d = 999999999 ;
for(int j = 1; j<= n ; j ++ ){
if(visy[j]){
d = min(d , sla[j]) ;
}
}
for(int j = 1; j<= n ; j ++ ){
if(visx[j] == false){
lx[j] -= d;
}
if(visy[j] == false){
ly[j] += d ;
}
else {
sla[j] -= d;
}
}
}
}
int res = 0 ;
for(int j = 1; j <= n ; j ++ ){
if(linker[j] != -1){
res += c[linker[j]][j];
}
}
return res ;
}
int main(){
int t ;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m) ;
for(int i = 1; i<= n ; i ++ ){
for(int j = 1; j <= n ; j ++)
c[i][j] = - 999999999;
}
for(int i = 1 ; i <= m ; i ++ ){
int u , v, w;
scanf("%d%d%d",&u,&v,&w);
c[u][v] = max(c[u][v] , -w );
}
int ans = km() ;
printf("%d\n", -ans);
}
}
费用流 :
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<map>
#include<string>
#include<vector>
#include<queue>
#include<iostream>
using namespace std;
#define L long long
int n ;
int m ;
int cnt ;
struct ed{
int v , nex ,cap , flow ,cost ;
}e[405 * 405] ;
int head[405];
int tol;
int pre[405];
int dis[405];
bool vis[405];
void init(){
cnt = 0;
memset(head, -1 ,sizeof(head)) ;
}
void add(int u , int v ,int cap ,int cost){
e[cnt].v = v;
e[cnt].nex = head[u];
e[cnt].cap = cap ;
e[cnt].cost = cost ;
e[cnt].flow = 0 ;
head[u] = cnt ;
cnt ++ ;
e[cnt].v = u ;
e[cnt].nex = head[v] ;
e[cnt].cap = 0 ;
e[cnt].cost = -cost ;
e[cnt].flow = 0 ;
head[v] = cnt ;
cnt ++ ;
}
bool spfa(int s, int t){
queue<int >que;
for(int i = 0; i<= 2*n + 1; i ++ ){
dis[i] = 999999999 ;
vis[i] = false ;
pre[i] = -1 ;
}
dis[s] = 0;
vis[s] = true ;
que.push(s) ;
while(!que.empty()){
int u = que.front() ; que.pop () ;
vis[u] = false ;
for(int i = head[u] ; i != -1 ; i = e[i].nex) {
int v = e[i].v ;
if(e[i].cap > e[i].flow && dis[v] > dis[u] + e[i].cost ){
dis[v] = dis[u] + e[i].cost ;
pre[v] = i ;
if(vis[v] == false ){
vis[v] = true ;
que.push(v) ;
}
}
}
}
if(pre[t] == -1)return false ;
else return true ;
}
int fyl(int s , int t , int &cost){
int flow = 0;
cost = 0;
while(spfa(s,t)){
int minn = 999999999;
for(int i = pre[t] ; i != -1 ; i = pre[e[i^1].v]){
if(minn > e[i].cap - e[i].flow){
minn = e[i].cap - e[i].flow ;
}
}
for(int i = pre[t] ; i != -1 ; i = pre[e[i^1].v]){
e[i].flow += minn ;
e[i^1].flow -= minn ;
cost += e[i].cost * minn ;
}
flow += minn ;
}
return flow ;
}
int main(){
int t ;
scanf("%d",&t);
while(t--){
init();
scanf("%d%d",&n,&m);
for(int i = 1; i <= m ; i ++ ){
int u , v , w;
scanf("%d%d%d",&u,&v,&w);
add(u,v+n,1,w);
}
for(int i = 1; i <= n ; i ++ ){
add(0,i,1,0);
add(i+n, n*2 + 1 , 1 , 0) ;
}
int cost ;
int flow = fyl(0,n*2+1,cost);
printf("%d\n",cost);
}
}
[kuangbin带你飞]专题十 匹配问题 二分图最大权匹配的更多相关文章
- [kuangbin带你飞]专题十 匹配问题
A-L 二分匹配 M-O 二分图多重匹配 P-Q 二分图最大权匹配 R-S 一般图匹配带花树 模板请自己找 ID Origin Title 61 / 72 Problem A HD ...
- [kuangbin带你飞]专题十 匹配问题 一般图匹配
过去做的都是二分图匹配 即 同一个集合里的点 互相不联通 但是如果延伸到一般图上去 求一个一般图的最大匹配 就要用带花树来解决 带花树模板 用来处理一个无向图上的最大匹配 看了一会还是不懂 抄了一遍 ...
- [kuangbin带你飞]专题十 匹配问题 二分图多重匹配
二分图的多重匹配问题不同于普通的最大匹配中的"每个点只能有最多一条边" 而是"每个点连接的边数不超过自己的限定数量" 最大匹配所解决的问题一般是"每个 ...
- [kuangbin带你飞]专题十 匹配问题 二分匹配部分
刚回到家 开了二分匹配专题 手握xyl模板 奋力写写写 终于写完了一群模板题 A hdu1045 对这个图进行 行列的重写 给每个位置赋予新的行列 使不能相互打到的位置 拥有不同的行与列 然后左行右列 ...
- [kuangbin带你飞]专题十五 数位DP
ID Origin Title 62 / 175 Problem A CodeForces 55D Beautiful numbers 30 / 84 Problem B HD ...
- [kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher 题解报告
来刷kuangbin字符串了,字符串处理在ACM中是很重要的,一般比赛都会都1——2道有关字符串处理的题目,而且不会很难的那种,大多数时候都是用到一些KMP的性质或者找规律. 点击标题可跳转至VJ比赛 ...
- [kuangbin带你飞]专题十四 数论基础
ID Origin Title 111 / 423 Problem A LightOJ 1370 Bi-shoe and Phi-shoe 21 / 74 Problem B ...
- 【算法系列学习】DP和滚动数组 [kuangbin带你飞]专题十二 基础DP1 A - Max Sum Plus Plus
A - Max Sum Plus Plus https://vjudge.net/contest/68966#problem/A http://www.cnblogs.com/kuangbin/arc ...
- [kuangbin带你飞]专题十二 基础DP1
ID Origin Title 167 / 465 Problem A HDU 1024 Max Sum Plus Plus 234 / 372 Problem B HDU 1 ...
随机推荐
- Delphi自定义消息应用及delphi托盘实现
Delphi自定义消息应用及delphi托盘实现interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Co ...
- InstallShield: cannot extract icon with index 0错误解决方案
在VS2012打包过程中,遇到这个错误,困扰我好几天,后来通过谷歌找到解决方案,如下: Expand the deploy solution (+) on the right panel (Solut ...
- (1.1.9)UVA 10930 A-Sequence(模拟)
/* * UVA_10930_1.cpp * * Created on: 2013年10月7日 * Author: Administrator */ #include <iostream> ...
- java并发ThreadLocal
ThreadLocal 实际就是一个map,一个线程对应一个local对象,线程创建时候,threadlocal随着创建,线程死亡ThreadLocal对象随着消失. runnable可以共享数据,要 ...
- Linux编程之自定义消息队列
我这里要讲的并不是IPC中的消息队列,我要讲的是在进程内部实现自定义的消息队列,让各个线程的消息来推动整个进程的运动.进程间的消息队列用于进程与进程之间的通信,而我将要实现的进程内的消息队列是用于有序 ...
- HTML5 Canvas渐进填充与透明
详细解释HTML5 Canvas中渐进填充的参数设置与使用,Canvas中透明度的设置与使 用,结合渐进填充与透明度支持,实现图像的Mask效果. 一:渐进填充(Gradient Fill) Canv ...
- 微软下一代云环境Web开发框架ASP.NET vNext预览
微软在2014年5月12日的TechEd大会上宣布将会公布下一代ASP.NET框架ASP.NET vNext的预览.此次公布的ASP.NET框架与曾经相比发生了根本性的变化,凸显了微软"云优 ...
- C# 自己定义 implicit和explicit转换
explicit 和 implicit 属于转换运算符,如用这两者能够让我们自己定义的类型支持相互交换explicti 表示显式转换.如从 A -> B 必须进行强制类型转换(B = (B)A) ...
- [Typescript] Introduction to Generics in Typescript
If Typescript is the first language in which you've encountered generics, the concept can be quite d ...
- sae storage 使用uploadify插件进行文件批量上传
uploadify插件在文件上传方面还是很不错的,这不我需要往sae 的storage上上传文件,就用了它.下面我就分享一下如何实现的吧.我们先到官网下载最新的uploadify最新的插件包.在页面中 ...