[Cogs728] [网络流24题#3] 最小路径覆盖 [网络流,最大流,二分图匹配]
建图:源点—>边的起点(集合1中的)—>边的终点(集合2中的)—>汇点,所有边权均为1,
计算最大流,最后枚举起点的出边,边权为0的即为匹配上的,
可以这样理解:每条边表示起点和终点形成一组可选匹配,所以每个点只能被匹配1次(做起点和终点分别1次),所以可以看成是二分图匹配。
代码略丑:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <queue> using namespace std; template<const int _n>
struct Edge
{
struct Edge_base { int to,next,w; }e[_n];
int cnt,p[_n];
Edge() { clear(); }
int start(const int x) { return p[x]; }
void insert(const int x,const int y,const int z)
{ e[++cnt].to=y; e[cnt].next=p[x]; e[cnt].w=z; p[x]=cnt; return ; }
Edge_base& operator[](const int x) { return e[x]; }
void clear() { cnt=,memset(p,,sizeof(p)); }
}; int n,m,SSS,TTT,Ans;
int level[],cur[],Out[],to[],from[];
bool visited[];
Edge<> e; bool Bfs(const int S)
{
int i,t;
queue<int> Q;
memset(level,,sizeof(level));
level[S]=;
Q.push(S);
while(!Q.empty())
{
t=Q.front();Q.pop();
for(i=e.start(t);i;i=e[i].next)
{
if(!level[e[i].to] && e[i].w)
{
level[e[i].to]=level[t]+;
Q.push(e[i].to);
}
}
}
return level[TTT];
} int Dfs(const int S,const int bk)
{
if(S==TTT)return bk;
int rest=bk;
for(int &i=cur[S];i;i=e[i].next)
{
if(level[e[i].to]==level[S]+ && e[i].w)
{
int flow=Dfs(e[i].to,min(e[i].w,rest));
e[i].w-=flow;
e[i^].w+=flow;
if((rest-=flow)<=)break;
}
}
if(bk==rest)level[S]=;
return bk-rest;
} int Dinic()
{
int flow=;
while(Bfs(SSS))
{
memcpy(cur,e.p,sizeof(int)*(n+n+));
flow+=Dfs(SSS,0x3f3f3f3f);
}
return flow;
} int main()
{
freopen("path3.in","r",stdin);
freopen("path3.out","w",stdout);
int i,j,x,y; scanf("%d%d",&n,&m);
for(i=;i<=m;++i)
{
scanf("%d%d",&x,&y);
e.insert(x,y+n,);
e.insert(y+n,x,);
Out[x]++;
} SSS=n+n+,TTT=n+n+;
for(i=;i<=n;++i)
{
e.insert(SSS,i,);
e.insert(i,SSS,);
e.insert(i+n,TTT,);
e.insert(TTT,i+n,);
} Dinic(); for(i=e.start(SSS);i;i=e[i].next)
{
if(e[i].w)continue;
for(j=e.start(e[i].to);j;j=e[j].next)
{
if(e[j].to!=SSS && !e[j].w)
{
to[e[i].to]=e[j].to-n;
from[e[j].to-n]=e[i].to;
break;
}
}
} for(i=;i<=n;++i)
{
if(!from[i])
{
int t=i;
while(t)
{
printf("%d ",t);
t=to[t];
}
printf("\n");
Ans++;
}
} printf("%d\n",Ans); return ;
}
匈牙利写法详见:http://www.cnblogs.com/Ngshily/p/4988909.html
[Cogs728] [网络流24题#3] 最小路径覆盖 [网络流,最大流,二分图匹配]的更多相关文章
- LOJ6002 - 「网络流 24 题」最小路径覆盖
原题链接 Description 求一个DAG的最小路径覆盖,并输出一种方案. Solution 模板题啦~ Code //「网络流 24 题」最小路径覆盖 #include <cstdio&g ...
- LibreOJ #6002. 「网络流 24 题」最小路径覆盖
#6002. 「网络流 24 题」最小路径覆盖 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:Special Judge 上传者: 匿名 提交提交记录统计讨论测 ...
- Luogu 2764 最小路径覆盖问题 / Libre 6002 「网络流 24 题」最小路径覆盖 (网络流,最大流)
Luogu 2764 最小路径覆盖问题 / Libre 6002 「网络流 24 题」最小路径覆盖 (网络流,最大流) Description 给定有向图G=(V,E).设P是G的一个简单路(顶点不相 ...
- [LOJ#6002]「网络流 24 题」最小路径覆盖
[LOJ#6002]「网络流 24 题」最小路径覆盖 试题描述 给定有向图 G=(V,E).设 P 是 G 的一个简单路(顶点不相交)的集合.如果 V 中每个顶点恰好在 P 的一条路上,则称 P 是 ...
- 【PowerOJ1738&网络流24题】最小路径覆盖问题 (最大流)
题意: 思路: [问题分析] 有向无环图最小路径覆盖,可以转化成二分图最大匹配问题,从而用最大流解决. [建模方法] 构造二分图,把原图每个顶点i拆分成二分图X,Y集合中的两个顶点Xi和Yi.对于原图 ...
- 【刷题】LOJ 6002 「网络流 24 题」最小路径覆盖
题目描述 给定有向图 \(G = (V, E)\) .设 \(P\) 是 \(G\) 的一个简单路(顶点不相交)的集合.如果 \(V\) 中每个顶点恰好在 \(P\) 的一条路上,则称 \(P\) 是 ...
- Libre 6013 「网络流 24 题」负载平衡 (网络流,最小费用最大流)
Libre 6013 「网络流 24 题」负载平衡 (网络流,最小费用最大流) Description G 公司有n 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使n ...
- Libre 6008 「网络流 24 题」餐巾计划 (网络流,最小费用最大流)
Libre 6008 「网络流 24 题」餐巾计划 (网络流,最小费用最大流) Description 一个餐厅在相继的N天里,第i天需要Ri块餐巾(i=l,2,-,N).餐厅可以从三种途径获得餐巾. ...
- Libre 6003 「网络流 24 题」魔术球 (网络流,最大流)
Libre 6003 「网络流 24 题」魔术球 (网络流,最大流) Description 假设有n根柱子,现要按下述规则在这n根柱子中依次放入编号为 1,2,3,4......的球. (1)每次只 ...
随机推荐
- 我们的微信小程序开发
基于微信小程序的系统开发准备工作 腾讯推出微信小程序也有一段时间了,在各种行业里面也都掀起一阵阵的热潮,很多APP应用被简化为小程序的功能迅速推出,同时也根据小程序的特性推出各种独具匠心的应用,相对传 ...
- Java Socket编程 深入讲解?你之前真的学懂了吗
很多人学习Socket往往会碰到这样哪样的问题,可以看看下面,加深理解.能看懂多少看懂多少. Socket是Java网络编程的基础,了解还是有好处的,这篇文章主要讲解Socket的基础编程.Socke ...
- .ignore配置问题1:配置后所忽略的文件不起作用
gitignore可以指定git忽略指定文件. 问题: 想忽略一些文件在.gitignore文件中会配置对应的文件,但是有时候配置后还是没有起作用: 我在element-ui封装个性化组件时,需要更换 ...
- D3.js 力导向图(小气泡围绕中心气泡)
html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3 ...
- 努比亚(nubia) Z18 mini NX611J 解锁BootLoader 并刷入recovery ROOT
努比亚(nubia) Z18 mini NX611J解锁BootLoader 并刷入recovery ROOT 工具下载链接:https://pan.baidu.com/s/1toU-mTR9FNE ...
- Android生命周期回顾
先回顾生命周期 Activity一共有3中状态 运行中 Activity位于前台,并具有用户焦点 暂停 另一个Activity位于屏幕前台并具有用户焦点,但此Activity仍可见.也就是说,另一个A ...
- SQLSERVER SQL性能优化技巧
这篇文章主要介绍了SQLSERVER SQL性能优化技巧,需要的朋友可以参考下 1.选择最有效率的表名顺序(只在基于规则的优化器中有效) SQLSERVER的解析器按照从右到左的顺序处理F ...
- dnn 添加图片
public string fileUpload() { if (fuPhoto.PostedFile != null && fuPhoto.P ...
- jstree CHECKBOX PLUGIN
The checkbox plugin makes multiselection possible using three-state checkboxes. Configuration overri ...
- js 滚动条
<script type="text/javascript"> //文档高度1016 包含隐藏的margin和padding 实际1000 //文档1000 //窗口高 ...