【网络流24题】飞行员配对方案问题
题面
这些题都用Cogs交算了
因为cogs有SPJ
题面
题解
很简单的二分图匹配
匈牙利算法就能够解决
求最大流的话
再加上一个源点一个汇点即可
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define MAXL 100000
#define MAX 500
#define INF 1000000000
inline int read()
{
int x=0,t=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
int n1,n2;
struct Line
{
int v,next,w;
}e[MAXL];
int h[MAX],cnt;
inline void Add(int u,int v,int w)
{
e[cnt]=(Line){v,h[u],w};h[u]=cnt++;
e[cnt]=(Line){u,h[v],0};h[v]=cnt++;
}
int n,S,T;
int level[MAX];
bool BFS()
{
memset(level,0,sizeof(level));
queue<int> Q;
Q.push(S);level[S]=1;
while(!Q.empty())
{
int u=Q.front();Q.pop();
for(int i=h[u];i!=-1;i=e[i].next)
{
int v=e[i].v;
if(e[i].w&&!level[v])
level[v]=level[u]+1,Q.push(v);
}
}
return level[T];
}
int cur[MAX];
int DFS(int u,int flow)
{
if(u==T||!flow)return flow;
int ret=0;
for(int &i=cur[u];i!=-1;i=e[i].next)
{
int v=e[i].v;
if(e[i].w&&level[v]==level[u]+1)
{
int d=DFS(v,min(flow,e[i].w));
ret+=d;flow-=d;
e[i].w-=d;e[i^1].w+=d;
}
}
if(!ret)level[u]=0;
return ret;
}
int Dinic()
{
int ret=0;
while(BFS())
{
for(int i=S;i<=T;++i)cur[i]=h[i];
ret+=DFS(S,INF);
}
return ret;
}
int main()
{
freopen("flyer.in","r",stdin);
freopen("flyer.out","w",stdout);
n=read();n1=read();n2=n-n1;
S=0;T=n+1;
memset(h,-1,sizeof(h));
for(int i=1;i<=n1;++i)Add(S,i,1);
for(int i=n1+1;i<=n;++i)Add(i,T,1);
int u,v;
while(scanf("%d%d",&u,&v)!=EOF)
Add(u,v,1);
printf("%d\n",Dinic());
return 0;
}