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

时间:2023-03-08 22:20:04

解题思路

考虑如何建模。

既然是网络流,那么肯定要有源点和汇点。而这个题目并没有什么明显的源点和汇点。

想一想,如果一个飞机能够起飞的话,那么必定有一对可以配对的正副驾驶员。也就是说一条曾广路能够上必定有且只有一对配对的管理员。

这里引入一个超级源点和超级汇点。超级源点就和正驾驶相连,流量为$1$。副驾驶员和汇点相连。模型就建好了。

之后就是跑$Dinic$,网络流模板不难,难的是建模QAQ

附上代码

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int maxnode = , maxedge = , INF = ;
int n, m, s, t, head[maxnode], cnt = , Depth[maxnode], Ans;
struct Edge {
int nxt, v, w;
}ed[maxedge];
inline void addedge(int u, int v, int w) {
ed[++cnt].nxt = head[u];
ed[cnt].v = v, ed[cnt].w = w;
head[u] = cnt;
}
inline bool BFS() {
queue <int> Q;
memset(Depth, , sizeof(Depth));
Depth[s] = , Q.push(s);
int u;
while (!Q.empty()) {
u = Q.front();
Q.pop();
for(int i=head[u]; i; i=ed[i].nxt) {
if(Depth[ed[i].v] == && ed[i].w > ) {
Depth[ed[i].v] = Depth[u] + ;
Q.push(ed[i].v);
if(ed[i].v == t) return true;
}
}
}
return false;
}
inline int Dinic(int u, int cap) {
if(u == t) return cap;
int delta;
for(int i=head[u]; i; i=ed[i].nxt) {
if(ed[i].w > && Depth[ed[i].v] == Depth[u] + ) {
delta = Dinic(ed[i].v, min(cap, ed[i].w));
if(delta > ) {
ed[i].w -= delta;
ed[i^].w += delta;
return delta;
}
}
}
return ;
}
int main() {
scanf("%d%d", &n, &m);
s = , t = n+;
for(int i=; i<=m; i++) addedge(s, i, ), addedge(i, s, );
for(int i=m+; i<=n; i++) addedge(i, t, ), addedge(t, i, );
static int x, y;
while (scanf("%d%d", &x, &y) == ) addedge(x, y, ), addedge(y, x, );
while (BFS()) Ans += Dinic(s, INF);
printf("%d", Ans);
}