P1231 教辅的组成
传送门:https://www.luogu.org/problemnew/show/P1231
这是一道很不错的网络流入门题,关键在于如何建图。
首先,我们将练习册和源点连一条边权为1的边,然后若书 i 和练习册 j 可以配套,就将连一条从练习册 j 到书 i 边,当然边权还是1。同理,答案和书也是如此,最后再将答案和汇点连一条边权为1的边。
但是这么写还是会有点问题,因为经过一本书的路径可能与很多条,书就被使用了多次,显然不符合题意。这时候我们可以将书 i 拆成书 i1 和 i2,i1 和练习册连边,i2 和答案连边,这样就保证没一本书之用过一次了。
建好图后跑最大流就行了。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
#define enter printf("\n")
#define space printf(" ")
#define Mem(a) memset(a, 0, sizeof(a))
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = 1e4 + ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch))
{
ans = ans * + ch - ''; ch = getchar();
}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) x = -x, putchar('-');
if(x >= ) write(x / );
putchar(x % + '');
} int t, n1, n2, n3; struct Edge
{
int from, to, cap, flow;
};
vector<Edge> edges;
vector<int> G[maxn << ];
void addEdge(int from, int to)
{
edges.push_back((Edge){from, to, , });
edges.push_back((Edge){to, from, , });
int sz = edges.size();
G[from].push_back(sz - );
G[to].push_back(sz - );
} int dis[maxn << ];
bool vis[maxn << ];
bool bfs()
{
Mem(vis);
queue<int> q;
q.push(); vis[] = ;
dis[] = ;
while(!q.empty())
{
int now = q.front(); q.pop();
for(int i = ; i < (int)G[now].size(); ++i)
{
Edge& e = edges[G[now][i]];
if(!vis[e.to] && e.cap > e.flow)
{
vis[e.to] = ;
dis[e.to] = dis[now] + ;
q.push(e.to);
}
}
}
return vis[t];
}
int cur[maxn << ];
int dfs(int now, int a)
{
if(now == t || !a) return a;
int flow = , f;
for(int& i = cur[now]; i < (int)G[now].size(); ++i)
{
Edge& e = edges[G[now][i]];
if(dis[e.to] == dis[now] + && (f = dfs(e.to, min(a, e.cap - e.flow))) > )
{
e.flow += f;
edges[G[now][i] ^ ].flow -= f;
flow += f; a -= f;
if(!a) break;
}
}
return flow;
} int maxflow()
{
int flow = ;
while(bfs())
{
Mem(cur);
flow += dfs(, INF);
}
return flow;
} //bool vis2[maxn]; int main()
{
n1 = read(); n2 = read(); n3 = read();
t = (n1 << )+ n2 + n3 + ; //为了防止编号重复
/*按注释掉的写法会WA掉,因为如果一本书多次输入,那么这个书
就多次拆点,这一本书就可以使用多次了 */
/* int m = read();
while(m--)
{
int x = read(), y = read();
addEdge(0, y + (n1 << 1));
addEdge(y + (n1 << 1), x);
addEdge(x, x + n1);
vis2[x] = 1;
}
m = read();
while(m--)
{
int x = read(), y = read();
if(!vis2[x]) addEdge(x, x + n1), vis2[x] = 1;
addEdge(x + n1, y + (n1 << 1) + n2);
addEdge(y + (n1 << 1) + n2, t);
}*/
int m = read();
while(m--)
{
int x = read(), y = read();
addEdge(y + (n1 << ), x);
}
m = read();
while(m--)
{
int x = read(), y = read();
addEdge(x + n1, y + (n1 << ) + n2);
}
for(int i = ; i <= n1; ++i) addEdge(i, i + n1);
for(int i = ; i <= n2; ++i) addEdge(, i + (n1 << ));
for(int i = ; i <= n3; ++i) addEdge(i + (n1 << ) + n2, t);
write(maxflow()); enter;
}
P1231 教辅的组成的更多相关文章
- Luogu P1231 教辅的组成
Luogu P1231 教辅的组成 题目背景 滚粗了的HansBug在收拾旧语文书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本语文书里面发现了一本答案,然而他却明明记得这书应该还 ...
- 洛谷 P1231 教辅的组成
P1231 教辅的组成 题目背景 滚粗了的HansBug在收拾旧语文书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本语文书里面发现了一本答案,然而他却明明记得这书应该还包含一份练习 ...
- 洛谷——P1231 教辅的组成
P1231 教辅的组成 题目背景 滚粗了的HansBug在收拾旧语文书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本语文书里面发现了一本答案,然而他却明明记得这书应该还包含一份练习 ...
- 【解题报告】洛谷 P1231 教辅的组成
[解题报告]洛谷 P1231 教辅的组成 题目链接 CSDN链接 这道题就只是一道普通的最大流问题,但是关键所在就是如何构图.要不是我看了题解,真的想不到这个构图方法呢 题目大意我就不写了,自己看好了 ...
- P1231 教辅的组成(最大流)
P1231 教辅的组成 这个题一看便知是网络流量,(三分图??滑稽..) 就一个小细节,如果我们仅仅将所有的点分成三部分跑网络流的话会有点小问题.. 因为这可能导致一本书被重复利用,就是有两条流经过同 ...
- 【luogu P1231 教辅的组成】 题解
题目链接:https://www.luogu.org/problemnew/show/P1231 对于每本书只能用一次,所以拆点再建边 #include <queue> #include ...
- 洛谷 P1231 教辅的组成(网络最大流+拆点加源加汇)
题目背景 滚粗了的HansBug在收拾旧语文书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本语文书里面发现了一本答案,然而他却明明记得这书应该还包含一份练习题.然而出现在他眼前的书 ...
- [洛谷P1231] 教辅的组成
题目大意:有n1本书,n2本练习册和n3个答案,然后又一些条件,说明某本答案可能和某本书对应,某本练习册可能和某本书对应,求最多有多少本完整的书(有书,练习册,答案) 题解:网络流,对应就连边,然后考 ...
- 【Luogu】P1231教辅的组成(拆点+Dinic+当前弧优化)
题目链接 妈耶 我的图建反了两次 准确的说是有两个地方建反了,然后反上加反改了一个小时…… 知道为什么要拆点吗? 假设这是你的图 左边到右边依次是超级源点 练习册 书 答案 ...
随机推荐
- [日常] Go语言圣经-匿名函数习题2
练习5.13: 修改crawl,使其能保存发现的页面,必要时,可以创建目录来保存这些页面.只保存来自原始域名下的页面.假设初始页面在golang.org下,就不 要保存vimeo.com下的页面. p ...
- [javaSE] 看知乎学习工厂模式
factory的“本质”就是根据不同的输入创建出不同类型的对象. 引入factory的原因就是你需要根据不同的输入创建不同类型的对象. 简单工厂模式相当于是一个工厂中有各种产品,创建在一个类中,客户无 ...
- 大数据之 Hadoop学习笔记
1 hadoop生态系统 hdfs 分布式文件系统 hadoop-hdfs-2.7.2.jar mapreduce 分布式计算框架 hadoop-mapreduce-client-app-2.7.2. ...
- find the most comfortable road(hdu1598)不错的并查集
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- linux系统编程:用truncate调整文件大小
truncate的使用非常简单: int truncate(const char *path, off_t length); 参数1:文件名 参数2: 文件需要被调整的大小 length 大于 文件 ...
- JAVA 并发:CLH 锁 与 AbstractQueuedSynchronizer
首先向Doug Lea致敬. CLH 以下是CLH锁的一个简单实现: class SimpleCLHLock { /** * initialized with a dummy node */ priv ...
- 【 js 基础 】【读书笔记】关于this
this 关键字是 Javascript 中很特别的一个关键字,被自动定义在所有函数的作用域中.this提供了一种更优雅的方式隐式“传递”一个对象的引用.今天就来说说 this 的指向问题. this ...
- js同时获取多个同name的input框的值
demo代码 <!doctype html> <html ng-app="a3_4"> <head> <title>表头排序< ...
- git 报错:error: failed to push some refs to 'https://github.com/Anderson-An/******.git'(已解决)
提交push 报错: $ git push origin masterTo https://github.com/Anderson-An/******.git ! [rejected] master ...
- npm 安装指定模块版本
npm list 查看具体模块 如: npm list @antv/g6 如需要安装指定的模块和版本 保存时 - --save-dev 是你开发时候依赖的东西,--save 是你发布之后还 ...