网络流24题-2 飞行员配对方案问题

网络流24题-2  飞行员配对方案问题

 

 题目链接:https://www.luogu.com.cn/problem/P2756

 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N=211,M=40011,inf=1<<29;
 5 int m,n,st,ed,cnt;
 6 int to[M],nxt[M],c[M],edge,head[N];
 7 int dep[N],cur[N];
 8 bool vis[N];
 9 
10 inline int re_ad() {
11     char ch=getchar(); int x=0,f=1;
12     while(ch<'0' || ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
13     while('0'<=ch && ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
14     return x*f;
15 }
16 
17 inline void addedge(int x,int y,int cc) {
18     ++edge,to[edge]=y,nxt[edge]=head[x],c[edge]=cc,head[x]=edge;
19     ++edge,to[edge]=x,nxt[edge]=head[y],c[edge]=0,head[y]=edge;
20 }
21 
22 inline bool bfs() {
23     memset(dep,0,sizeof(dep));
24     for(int i=0;i<=n+1;++i) cur[i]=head[i];
25     queue<int> q;
26     dep[st]=1,q.push(st);
27     while(!q.empty()) {
28         int u=q.front(); q.pop();
29         for(int i=head[u],v;i;i=nxt[i]) {
30             v=to[i];
31             if(!c[i] || dep[v]) continue;
32             dep[v]=dep[u]+1,q.push(v);
33             if(v==ed) return true;
34         }
35     }
36     return false;
37 }
38 
39 int Dinic(int d,int flow) {
40     if(d==ed) return flow;
41     int rst=flow;
42     for(int &i=cur[d],u,tmp;i;i=nxt[i]) {
43         u=to[i];
44         if(!c[i] || dep[u]!=dep[d]+1) continue;
45         tmp=Dinic(u,min(c[i],rst));
46         if(!tmp) dep[u]=0;
47         else c[i]-=tmp,c[i^1]+=tmp,rst-=tmp;
48         if(!rst) break;
49     }
50     return flow-rst;
51 }
52 
53 int main()
54 {
55     m=re_ad(),n=re_ad();
56     edge=1;
57     for(int x,y;x!=-1 || y!=-1;) x=re_ad(),y=re_ad(),++cnt,addedge(x,y,1);
58     st=0,ed=n+1;
59     for(int i=1;i<=m;++i) addedge(st,i,1);
60     for(int i=m+1;i<=n;++i) addedge(i,ed,1);
61     ll maxflow=0;
62     while(bfs()) {
63         int tmp=Dinic(st,inf);
64         if(tmp) maxflow+=1ll*tmp;
65         else break;
66     }
67     printf("%lld\n",maxflow);
68     for(int i=2;i<=cnt*2;i+=2) if(!c[i]) printf("%d %d\n",to[i^1],to[i]);
69     return 0;
70 }

 

上一篇:2021-07-25


下一篇:数字asic流程实验(三) Verilog编写&前仿真