[网络流24题]飞行员配对方案问题
裸的二分图最大匹配,也可以用匈牙利算法解决。
#include<complex> #include<cstdio> using namespace std; const int INF=0x3f3f3f3f; const int N=1e3+7; struct node{ int v,f,nxt; }e[N<<1]; int n,m,Enum=1,ans,s,t; int front[N],cur[N],deep[N]; int q[N]; int qread() { int x=0; char ch=getchar(); while(ch<'0' || ch>'9')ch=getchar(); while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();} return x; } void Insert(int u,int v) { e[++Enum].v=v;e[Enum].f=1;e[Enum].nxt=front[u];front[u]=Enum; e[++Enum].v=u;e[Enum].nxt=front[v];front[v]=Enum; } bool bfs() { for(int i=0;i<=t;i++) { cur[i]=front[i]; deep[i]=0; } int head=1,tail=0,u; deep[s]=1; q[++tail]=s; while(head<=tail) { u=q[head++]; for(int i=front[u];i;i=e[i].nxt) if(e[i].f && !deep[e[i].v]) { deep[e[i].v]=deep[u]+1; if(e[i].v==t)return 1; q[++tail]=e[i].v; } } return 0; } int dfs(int x,int cur_flow) { if(x==t)return cur_flow; int rest=cur_flow,v; for(int &i=cur[x];i;i=e[i].nxt) { v=e[i].v; if(e[i].f && deep[v]==deep[x]+1 && rest) { int new_flow=dfs(v,min(e[i].f,rest)); e[i].f-=new_flow; e[i^1].f+=new_flow; rest-=new_flow; if(!rest)return cur_flow; } } deep[x]=0; return cur_flow-rest; } void Dinic() { while(bfs()) ans+=dfs(s,INF); if(!ans)printf("No Solution!\n"); else printf("%d\n",ans); } int main() { scanf("%d%d",&n,&m); s=0;t=n+1; for(int i=1;i<=m;i++) Insert(s,i); for(int i=m+1;i<=n;i++) Insert(i,t); int u,v; while(scanf("%d%d",&u,&v)!=EOF) Insert(u,v); Dinic(); return 0; }