LibreOJ #6000. 「网络流 24 题」搭配飞行员

二次联通门 : LibreOJ #6000. 「网络流 24 题」搭配飞行员

/*
LibreOJ #6000. 「网络流 24 题」搭配飞行员 二分图最大匹配
Dinic最大流 + 当前弧优化 */
#include <cstring>
#include <cstdio>
#include <queue> #define Max 10000
#define INF 1e5 int read (int &now)
{
now = ;
register char word = getchar ();
while (word < '' || word > '')
word = getchar ();
while (word >= '' && word <= '')
{
now = now * + word - '';
word = getchar ();
}
if (now >= )
return ;
} inline int min (int a, int b)
{
return a < b ? a : b;
} class Net_Flow_Type
{ private : int __to[Max << ], __next[Max << ]; int __flow[Max << ]; int edge_list[Max];
int Edge_Count; int deep[Max], __tech_[Max];
int T; int Answer; public : Net_Flow_Type ()
{
Edge_Count = ;
} inline void Insert_edge (int from, int to)
{
Edge_Count ++; __to[Edge_Count] = to;
__next[Edge_Count] = edge_list[from];
edge_list[from] = Edge_Count; Edge_Count ++; __to[Edge_Count] = from;
__next[Edge_Count] = edge_list[to];
edge_list[to] = Edge_Count; __flow[Edge_Count - ] = ;
__flow[Edge_Count] = ;
} bool Bfs (int Start, int End)
{
std :: queue <int> Queue; Queue.push (Start);
memset (deep, -, sizeof deep); int now;
for (deep[Start] = ; !Queue.empty (); Queue.pop ())
{
now = Queue.front (); for (int i = edge_list[now]; i; i = __next[i])
if (__flow[i] && deep[__to[i]] == -)
{
deep[__to[i]] = deep[now] + ;
if (__to[i] == End)
return true;
Queue.push (__to[i]);
}
} return deep[End] != -;
} int Flowing (int now, int flow)
{
if (now == T || flow <= )
return flow; int res = , pos = ;
for (int i = __tech_[now]; i; i = __next[i])
{
if (deep[__to[i]] != deep[now] + || __flow[i] <= )
continue;
res = Flowing (__to[i], min (flow, __flow[i])); if (res > )
{
flow -= res;
pos += res; __flow[i] -= res;
__flow[i ^ ] += res;
if (__flow[i])
__tech_[now] = i; if (flow == )
return pos;
}
}
return pos;
} int Dinic (int Start, int End)
{
for (T = End; Bfs (Start, End); )
{
memcpy (__tech_, edge_list, sizeof edge_list); Answer += Flowing (Start, INF);
} return Answer;
} }; int N, M;
Net_Flow_Type Make; int main (int argc, char *argv[])
{
read (N);
read (M); int S = N + , T = N + ; for (int i = ; i <= M; i ++)
Make.Insert_edge (S, i); for (int i = M + ; i <= N; i ++)
Make.Insert_edge (i, T); for (int x, y; scanf ("%d %d", &x, &y) == ; Make.Insert_edge (x, y)); printf ("%d", Make.Dinic (S, T)); return ;
}
上一篇:LibreOJ #6007. 「网络流 24 题」方格取数 最小割 最大点权独立集 最大流


下一篇:在jsp里调用out.flush()和response.flushBuffer()有什么区别