1. 程式人生 > >【COGS 14】 [網絡流24題] 搭配飛行員 網絡流板子題

【COGS 14】 [網絡流24題] 搭配飛行員 網絡流板子題

str main head fin i++ add pri can tail

用網絡流水二分圖的模型(存一下板子)

#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 105
#define R register
#define inf 0x7f7f7f7f
using namespace std;
inline int read()
{
    R int sum=0;
    R char ch=getchar();
    while(ch<0||ch>9)ch=getchar();
    while(ch>=0&&ch<=
9) { sum=(sum<<1)+(sum<<3)+ch-0; ch=getchar(); } return sum; } struct V { int to,next,w; }c[N<<4]; int head[N],t=1; int n,n1; inline void add(int x,int y,int z) { c[++t].to=y; c[t].next=head[x]; c[t].w=z; head[x]=t; } inline void Init() { scanf(
"%d%d",&n,&n1); R int x,y; while(scanf("%d%d",&x,&y)==2) { add(x,y,1); add(y,x,0); } for(int i=1;i<=n1;i++) add(n+1,i,1),add(i,n+1,0); for(int i=n1+1;i<=n;i++) add(i,n+2,1),add(n+2,i,0); } int q[N],top,tail,deep[N]; inline bool bfs() { memset(deep,
0,sizeof(deep)); deep[n+1]=1; q[1]=n+1; top=tail=1; while(top<=tail) { R int x=q[top++]; if(x==n+2)return 1; for(R int i=head[x];i;i=c[i].next) if(c[i].w&&deep[c[i].to]==0) { deep[c[i].to]=deep[x]+1; q[++tail]=c[i].to; } } return 0; } inline int Min(int x,int y) { return x<y?x:y; } int dfs(int x,int v) { if(x==n+2||!v)return v; R int ret=0; for(int i=head[x];i;i=c[i].next) if(c[i].w&&deep[c[i].to]==deep[x]+1) { R int f=Min(c[i].w,v); R int w=dfs(c[i].to,f); v-=w; ret+=w; c[i].w-=w; c[i^1].w+=w; if(v==0)break; } if(!ret)deep[x]=-1; return ret; } inline int dinic() { R int ans=0; while(bfs())ans+=dfs(n+1,inf); return ans; } inline void Work() { printf("%d",dinic()); } int main() { Init(); Work(); return 0; }

【COGS 14】 [網絡流24題] 搭配飛行員 網絡流板子題