记录编号 |
568660 |
评测结果 |
AAAAAAAAAA |
题目名称 |
[网络流24题] 搭配飞行员 |
最终得分 |
100 |
用户昵称 |
yrtiop |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.000 s |
提交时间 |
2022-01-23 12:05:59 |
内存使用 |
0.00 MiB |
显示代码纯文本
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 205;
int cnt,head[maxn],cur[maxn],ver[maxn << 1],nxt[maxn << 1],cap[maxn << 1];
int n,m;
void add(int u,int v,int t) {
ver[++ cnt] = v;
nxt[cnt] = head[u];
head[u] = cnt;
cap[cnt] = t;
return ;
}
int level[maxn];
queue<int> q;
bool bfs(int s,int t) {
memset(level , 0 , sizeof(level));
q.push(s);
level[s] = 1;
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = head[u];~ i;i = nxt[i]) {
int v = ver[i];
if(cap[i]&&!level[v]) {
level[v] = level[u] + 1;
q.push(v);
}
}
}
return level[t] > 0;
}
int dfs(int x,int t,int maxflow) {
if(x == t||!maxflow)return maxflow;
int flow = 0,f;
for(int& i = cur[x];~ i;i = nxt[i]) {
int v = ver[i];
if(level[v] == level[x] + 1&&(f = dfs(v , t , min(maxflow , cap[i])))) {
if(!f) {
level[v] = 0;
break ;
}
flow += f;
maxflow -= f;
cap[i] -= f;
cap[i ^ 1] += f;
}
}
return flow;
}
int Dinic(int s,int t) {
int flow = 0;
while(bfs(s , t)) {
memcpy(cur , head , sizeof(head));
flow += dfs(s , t , 0x3f3f3f3f);
}
return flow;
}
int main() {
freopen("flyer.in","r",stdin);
freopen("flyer.out","w",stdout);
scanf("%d%d",&n,&m);
memset(head , -1 , sizeof(head));
int u,v;
while(~ scanf("%d%d",&u,&v)) {
add(u , v , 1);
add(v , u , 0);
}
int S = n + 1,T = n + 2;
for(int i = 1;i <= m;++ i) {
add(S , i , 1);
add(i , S , 0);
}
for(int i = m + 1;i <= n;++ i) {
add(i , T , 1);
add(T , i , 0);
}
printf("%d\n",Dinic(S , T));
fclose(stdin);
fclose(stdout);
return 0;
}