[Explanations] P2756 | pilot program pairing problems

Topic Link

The pilot program pairing problems

Consider each one of the pilots, most only can drive a plane, a team of relationship can only be used once, so

For foreign pilots \ (i: s \ xrightarrow { 1} i \)

For British pilot \ (j: j \ xrightarrow { 1} t \)

For a set of relationships \ (i, j: i \ xrightarrow {1} j \)

From \ (S \) to \ (T \) Run maximum flow path to the record

With \ (After \) successor array records a point

(Because the problem in input \ (i \) and (j \) \ certainly not equal, so that can be run as a bipartite graph maximum matching, matching point for pilots to record)

\(\mathcal{Code}:\)

#include<queue>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 100010
#define int long long
#define debug cout<<__LINE__<<" "<<__FUNCTION__<<"\n"
inline int read(){
    int x=0,y=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')y=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    return x*y;
}
int n,m,s,t,dep[N];
int head[N],tot=1,front,used[N];
int ans;
int after[N];
struct Node{
    int nxt,to,dis;
}edge[N<<2];
inline void add(int x,int y,int z){
    edge[++tot].nxt=head[x];
    edge[tot].to=y;
    edge[tot].dis=z;
    head[x]=tot;
}
queue<int> q;
inline int bfs(){
    register int i;
    for(i=0;i<=n+1;i++) dep[i]=-1, used[i]=head[i];
    dep[s]=0;
    q.push(s);
    while(!q.empty()){
        front=q.front();q.pop();
//      cout<<front<<" ";debug;
        for(i=head[front];i;i=edge[i].nxt){
            if(edge[i].dis&&dep[edge[i].to]==-1){
                dep[edge[i].to]=dep[front]+1;q.push(edge[i].to);
            }
        }
    }
//  debug;
    return dep[t]!=-1;
}
int dfs(int now,int limit){
    if(!limit||now==t) return limit;
    int flow=0;
    for(int &i=used[now],pro;i;i=edge[i].nxt){
        if(dep[edge[i].to]==dep[now]+1&&edge[i].dis){
            pro=dfs(edge[i].to,min(limit,edge[i].dis));
            if(!pro) continue;
            edge[i].dis-=pro;
            edge[i^1].dis+=pro;
            flow+=pro;
            limit-=pro;
            after[now]=edge[i].to;//记录路径
            if(!limit) return flow;
        }
    }
//  cout<<flow<<" ";debug;
//  system("pause");
    return flow;
}
inline void Dinic(){
    while(bfs()){ans+=(dfs(s,10000000001LL));}
}
signed main(){
//  freopen("a.in","r",stdin);
//  freopen(".out","w",stdout);
    m=read();n=read();
    s=0;t=n+1;
    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);
    int x=read(),y=read();
    while(x!=-1&&y!=-1){
        add(x,y,1);add(y,x,0);
        x=read();y=read();
    }
    Dinic();
    if(ans==0){//无解情况
        puts("No Solution!");return 0;
    }
    cout<<ans<<"\n";
    for(int i=1;i<=m;i++){
        if(after[i]){
            cout<<i<<" "<<after[i]<<"\n";
        }
    }
//  fclose(stdin);
//  fclose(stdout);
    return 0;
}

Guess you like

Origin www.cnblogs.com/end-of-mind/p/11535767.html