【Luogu】P1231教辅的组成(拆点+Dinic+当前弧优化)
妈耶
我的图建反了两次 准确的说是有两个地方建反了,然后反上加反改了一个小时……
知道为什么要拆点吗?

假设这是你的图 左边到右边依次是超级源点 练习册 书 答案 超级汇点
请问这张图的最大流是多少?
如果把中间拆成这样:

Book-in是跟练习册匹配的书的入端,Book-out是跟答案匹配的书的出端。相当于每本书都是一条隧道,有入口有出口,每本书的入口和对应的出口连边。
请问现在这张图的最大流是多少?
所以你看。
代码放上:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype> inline long long read(){
long long num=,f=;
char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') f=-;
ch=getchar();
}
while(isdigit(ch)){
num=num*+ch-'';
ch=getchar();
}
return num*f;
} struct Edge{
int next,to,val;
}edge[];
int head[],num=-;
inline void add(int from,int to,int val){
edge[++num]=(Edge){ head[from],to,val};
head[from]=num;
} bool vis[];
int dfn[];
int list[];
int f[],h,t=;
int n,m,Start,End; bool bfs(){
memset(vis,,sizeof(vis));
f[]=Start;vis[Start]=;dfn[Start]=;h=;t=;
while(h++<t){
int from=f[h];
for(int i=head[from];i!=-;i=edge[i].next){
int to=edge[i].to;
if(vis[to]||(!edge[i].val)) continue;
dfn[to]=dfn[from]+;
vis[to]=;
f[++t]=to;
}
}
return vis[End];
} int dfs(int x,int val){
if(x==End||val==) return val;
int flow=;
vis[x]=;
for(int &i=list[x];i!=-;i=edge[i].next){
int to=edge[i].to;
if(dfn[to]==dfn[x]+&&!vis[to]&&edge[i].val>){
int now=dfs(to,std::min(edge[i].val,val));
if(now>){
edge[i].val-=now;
edge[i^].val+=now;
flow+=now;val-=now;
if(val<=) break;
}
}
}
if(flow!=val) dfn[x]=-;
return flow;
} int ans; int main(){
memset(head,-,sizeof(head));
int n1=read(),n2=read(),n3=read();
int n=n1*+n2;int N=n+n3;End=N+;
int m1=read();
for(int i=;i<=m1;++i){
int book=read(),note=read();
add(note+n1*,book,);
add(book,note+n1*,);
}
int m2=read();
for(int i=;i<=m2;++i){
int book=read(),ansnote=read();
add(book+n1,ansnote+n,);
add(ansnote+n,book+n1,);
}
for(int i=;i<=n1;++i){
add(i,i+n1,);
add(i+n1,i,);
}
for(int i=;i<=n2;++i){
add(Start,i+n1*,);
add(i+n1*,Start,);
}
for(int i=;i<=n3;++i){
add(i+n,End,);
add(End,i+n,);
}
while(bfs()){
memset(vis,,sizeof(vis));
for(int i=;i<=End;++i) list[i]=head[i];
int now=dfs(Start,0x7fffffff);
if(!now) break;
ans+=now;
}
printf("%d",ans);
return ;
}
话说当前弧优化真好用
【Luogu】P1231教辅的组成(拆点+Dinic+当前弧优化)的更多相关文章
- Luogu P1231 教辅的组成
Luogu P1231 教辅的组成 题目背景 滚粗了的HansBug在收拾旧语文书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本语文书里面发现了一本答案,然而他却明明记得这书应该还 ...
- ARC085E(最小割规划【最大流】,Dinic当前弧优化)
#include<bits/stdc++.h>using namespace std;typedef long long ll;const ll inf=0x3f3f3f3f;int cn ...
- Dinic当前弧优化 模板及教程
在阅读本文前,建议先自学最大流的Ek算法. 引入 Ek的核心是执行bfs,一旦找到增广路就停下来进行增广.换言之,执行一遍BFS执行一遍DFS,这使得效率大大降低.于是我们可以考虑优化. 核心思路 在 ...
- [Poj2112][USACO2003 US OPEN] Optimal Milking [网络流,最大流][Dinic+当前弧优化]
题意:有K个挤奶机编号1~K,有C只奶牛编号(K+1)~(C+K),每个挤奶机之多能挤M头牛,现在让奶牛走到挤奶机处,求奶牛所走的最长的一条边至少是多少. 题解:从起点向挤奶机连边,容量为M,从挤奶机 ...
- P3376 网络流-最大流模板题(Dinic+当前弧优化)
(点击此处查看原题) Dinic算法 Dinic算法相对于EK算法,主要区别在于Dinic算法对图实现了分层,使得我们可以用一次bfs,一次dfs使得多条增广路得到增广 普通的Dinic算法已经可以处 ...
- HDU 4280 Island Transport(dinic+当前弧优化)
Island Transport Description In the vast waters far far away, there are many islands. People are liv ...
- 【luogu P1231 教辅的组成】 题解
题目链接:https://www.luogu.org/problemnew/show/P1231 对于每本书只能用一次,所以拆点再建边 #include <queue> #include ...
- P1231 教辅的组成 拆点限流
如果只有两个物品的话 是一个裸的二分图匹配问题 现在变成了三个物品之间的匹配 则只要在中间加一层节点表示书 再把这层的每个点拆成两个点中间连一条边限制流量 使其只能用一次 #include<io ...
- 网络流小记(EK&dinic&当前弧优化&费用流)
欢 迎 来 到 网 络 瘤 的 世 界 什么是网络流? 现在我们有一座水库,周围有n个村庄,每个村庄都需要水,所以会修水管(每个水管都有一定的容量,流过的水量不能超过容量).最终水一定会流向唯一一个废 ...
随机推荐
- PeopleSoft FSCM Production Support 案例分析之一重大紧急事故发生时的应对策略
案例背景: 今天一大早用户打电话来讲昨天上传的银行的forex payment return file好像没有被处理到,我一听就觉得纳闷,因为昨天晚上operator也没有给我打电话啊(如果有job ...
- python中中括号中的负数
>>> a="a,b,c,d,e">>> a.split(",")[0:2]['a', 'b']>>> a ...
- selenium-Python之进行文件的上传和下载文件
在利用Selenium进行批量上传文件时,遇到如下的Windows窗口进行上传.下载操作时,可以通过pywinauto进行操作.上传窗口如下 使用pywinauto,需知Windows窗口控件的cla ...
- VM中python2.7运行skier游戏,shell重启问题!!!!!!
在虚拟机win7系统python2.7,在该python中运行了 父与子中的skier游戏(代码手写), 出现如下问题: ================ RESTART: C:\Python27\S ...
- UIButton zoomin pressed
// Scale up on button press - (void) buttonPress:(UIButton*)button { button.transform = CGAffineTran ...
- UVA 11584 Partitioning by Palindromes 划分回文串 (Manacher算法)
d[i]表示前面i个字符划分成的最小回文串个数, 转移:当第i字符加进来和前面区间j构成回文串,那么d[i] = d[j]+1. 要判断前面的字符j+1到i是不是回文串,可以用Manacher算法预处 ...
- 爆零系列—补题A
http://codeforces.com/contest/615/problem/A 读错题 结果发现是无脑题 直接标记统计 #include<cstdio> #include< ...
- HDU-6035 Colorful Tree(树形DP) 2017多校第一场
题意:给出一棵树,树上的每个节点都有一个颜色,定义一种值为两点之间路径中不同颜色的个数,然后一棵树有n*(n-1)/2条 路径,求所有的路径的值加起来是多少. 思路:比赛的时候感觉是树形DP,但是脑袋 ...
- (五)VMware Harbor 部署之SSL
转自:https://www.cnblogs.com/Rcsec/p/8479728.html 1 .签名证书与自签名证书 签名证书:由权威颁发机构颁发给服务器或者个人用于证明自己身份的东西. 自签名 ...
- Codeforces Round #272 (Div. 2)-B. Dreamoon and WiFi
http://codeforces.com/contest/476/problem/B B. Dreamoon and WiFi time limit per test 1 second memory ...