luogu P1231 教辅的组成
发布日期:2021-08-19 21:36:36 浏览次数:10 分类:技术文章

本文共 2648 字,大约阅读时间需要 8 分钟。

二次联通门 : 

 

 

 

 

/*    luogu P1231 教辅的组成        拆点 + 最大流        把书拆点后与答案与资料连边    跑最大流 */#include 
#include
#include
#include
#include
#define Max 50005#define INF 1e7using namespace std;void read (int &now){ now = 0; char word = getchar (); while (word > '9' || word < '0') word = getchar (); while (word >= '0' && word <= '9') { now = now * 10 + word - '0'; word = getchar (); }}struct Edge{ int to; int next; int flow;}edge[Max << 3];int Edge_Count = 1;int edge_list[Max];inline void AddEdge (int from, int to){ Edge_Count++; edge[Edge_Count].flow = 1; edge[Edge_Count].to = to; edge[Edge_Count].next = edge_list[from]; edge_list[from] = Edge_Count; Edge_Count++; edge[Edge_Count].to = from; edge[Edge_Count].flow = 0; edge[Edge_Count].next = edge_list[to]; edge_list[to] = Edge_Count;}int N, M, Q;int E;int S, T;int deep[Max];int Answer;int Flowing (int now, int flow){ if (flow <= 0 || now == T) return flow; int pos = 0, res = 0; for (int i = edge_list[now]; i; i = edge[i].next) { if (deep[edge[i].to] != deep[now] + 1 || edge[i].flow <= 0) continue; pos = Flowing (edge[i].to, min (edge[i].flow, flow)); flow -= pos; res += pos; edge[i].flow -= pos; edge[i ^ 1].flow += pos; if (flow <= 0) return res; } return res;}int main (int argc, char *argv[]){ read (N); read (M); read (Q); read (E); M += N; Q += M; S = Max - 2; T = Max - 3; int x, y; for (int i = 1; i <= N; i++) AddEdge (i, i + Q); for (int i = N + 1; i <= M; i++) AddEdge (S, i); for (int i = M + 1; i <= Q; i++) AddEdge (i, T); for (int i = 1; i <= E; i++) { read (x); read (y); AddEdge (y + N, x); } read (E); for (int i = 1; i <= E; i++) { read (x); read (y); AddEdge (x + Q, y + M); } while (true) { bool flag = false; memset (deep, -1, sizeof deep); queue
Queue; Queue.push (S); deep[S] = 0; int now; while (!Queue.empty ()) { now = Queue.front (); Queue.pop (); for (int i = edge_list[now]; i; i = edge[i].next) if (deep[edge[i].to] < 0 && edge[i].flow) { deep[edge[i].to] = deep[now] + 1; if (edge[i].to == T) { flag = true; break; } Queue.push (edge[i].to); } if (flag) break; } if (deep[T] < 0) break; Answer += Flowing (S, INF); } printf ("%d", Answer); return 0;}

 

转载于:https://www.cnblogs.com/ZlycerQan/p/6882689.html

转载地址:https://blog.csdn.net/weixin_30955341/article/details/94822928 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:git三剑客笔记
下一篇:hdoj4864 Task (贪心)

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2025年01月11日 12时58分19秒