传送门: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 教辅的组成的更多相关文章

  1. Luogu P1231 教辅的组成

    Luogu P1231 教辅的组成 题目背景 滚粗了的HansBug在收拾旧语文书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本语文书里面发现了一本答案,然而他却明明记得这书应该还 ...

  2. 洛谷 P1231 教辅的组成

    P1231 教辅的组成 题目背景 滚粗了的HansBug在收拾旧语文书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本语文书里面发现了一本答案,然而他却明明记得这书应该还包含一份练习 ...

  3. 洛谷——P1231 教辅的组成

    P1231 教辅的组成 题目背景 滚粗了的HansBug在收拾旧语文书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本语文书里面发现了一本答案,然而他却明明记得这书应该还包含一份练习 ...

  4. 【解题报告】洛谷 P1231 教辅的组成

    [解题报告]洛谷 P1231 教辅的组成 题目链接 CSDN链接 这道题就只是一道普通的最大流问题,但是关键所在就是如何构图.要不是我看了题解,真的想不到这个构图方法呢 题目大意我就不写了,自己看好了 ...

  5. P1231 教辅的组成(最大流)

    P1231 教辅的组成 这个题一看便知是网络流量,(三分图??滑稽..) 就一个小细节,如果我们仅仅将所有的点分成三部分跑网络流的话会有点小问题.. 因为这可能导致一本书被重复利用,就是有两条流经过同 ...

  6. 【luogu P1231 教辅的组成】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1231 对于每本书只能用一次,所以拆点再建边 #include <queue> #include ...

  7. 洛谷 P1231 教辅的组成(网络最大流+拆点加源加汇)

    题目背景 滚粗了的HansBug在收拾旧语文书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本语文书里面发现了一本答案,然而他却明明记得这书应该还包含一份练习题.然而出现在他眼前的书 ...

  8. [洛谷P1231] 教辅的组成

    题目大意:有n1本书,n2本练习册和n3个答案,然后又一些条件,说明某本答案可能和某本书对应,某本练习册可能和某本书对应,求最多有多少本完整的书(有书,练习册,答案) 题解:网络流,对应就连边,然后考 ...

  9. 【Luogu】P1231教辅的组成(拆点+Dinic+当前弧优化)

    题目链接 妈耶 我的图建反了两次 准确的说是有两个地方建反了,然后反上加反改了一个小时…… 知道为什么要拆点吗? 假设这是你的图   左边到右边依次是超级源点    练习册     书     答案 ...

随机推荐

  1. VS2017 IIS 部署.net core web项目

    1.点击IIS,查看模块 查看是否安装了 AspNetCoreModule 模块,如果没有安装可下载:https://dotnet.microsoft.com/download 下载安装后,即可部署项 ...

  2. IDEA@Data注释使用

    @Data注解主要是帮助解决Setter 和 Getter以及 toString这种重复的无脑工作 加入@Data注解可以直接帮助我们添加实体类相应的Setter 和 Getter以及 toStrin ...

  3. Contemplation! Algebra(矩阵快速幂,uva10655)

    Problem EContemplation! AlgebraInput: Standard Input Output: Standard Output Time Limit: 1 Second Gi ...

  4. Can you find it?(数组+二分hdu2141)

    Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others ...

  5. c# IEnumerable和IEnumerator枚举器

    一 : IEnumerable 公开枚举数,该枚举数支持在非泛型集合上进行简单迭代. IEnumerable是可以枚举的所有非泛型集合的基接口,IEnumerable包含单个方法GetEnumerat ...

  6. django-xss攻击原理与防范

    一.跨站脚本攻击(XSS) XSS是一种经常出现在web应用中的计算机安全漏洞,它允许恶意web用户将代码植入到提供给其它用户使用的页面中.也属一种注入攻击,注入本质上就是把输入的数据变成可执行的程序 ...

  7. 前端了解即可:postman(接口测试)的使用

    postman的使用,前端了解即可.因为都是一些工具的应用,截图比较多,之前在有道云笔记上总结记录过,在这不在写了,展示一张笔记截图. 此文仅做参考一下,有不足之处,欢迎指正:

  8. 【代码笔记】iOS-字体从右向左滚动

    一,效果图. 二,代码. ViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup ...

  9. JS中String与Array的一些常用方法

    真是恨透了这些类似于substring substr slice 要么长得像,要么就功能相近的方法... 1⃣️string 1.substring(start开始位置的索引,end结束位置索引) 截 ...

  10. 《Spring实战》第4章--面向切面的Spring--处理通知中的参数(经验总结)

    今天学习<Spring实战>第4章<面向切面的Spring>,根据4.3.3小节写出如下切面类: package proxy; import java.util.HashMap ...