• 网络流24题1 飞行员配对方案问题


    【问题描述】

     飞行大队有若干个来自各地的驾驶员,专门驾驶一种型号的飞机,这种飞机每架有两个驾驶员,需一个正驾驶员和一个副驾驶员。由于种种原因,例如相互配合的问题,有些驾驶员不能在同一架飞机上飞行,问如何搭配驾驶员才能使出航的飞机最多。
    如图,假设有10个驾驶员,如图中的V1,V2,…,V10就代表达10个驾驶员,其中V1,V2,V3,V4,V5是正驾驶员,V6,V7,V8,V9,V10是副驾驶员。如果一个正驾驶员和一个副驾驶员可以同机飞行,就在代表他们两个之间连一条线,两个人不能同机飞行,就不连。例如V1和V7可以同机飞行,而V1和V8就不行。请搭配飞行员,使出航的飞机最多。注意:因为驾驶工作分工严格,两个正驾驶员或两个副驾驶员都不能同机飞行.
    

    【输入格式】

    输入文件有若干行
    第一行,两个整数n与n1,表示共有n个飞行员(2<=n<=100),其中有n1名飞行员是正驾驶员.
    下面有若干行,每行有2个数字a,b。表示正驾驶员a和副驾驶员b可以同机飞行。
    注:正驾驶员的编号在前,即正驾驶员的编号小于副驾驶员的编号.
    

    【输出格式】

    输出文件有一行
    第一行,1个整数,表示最大起飞的飞机数。
    

    【题解】

    加上源点和汇点,求最大流(刘)。
    

    【代码】

    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <algorithm>
    #include <iostream>
    #include <vector>
    #include <cmath>
    #include <stack>
    #include <queue>
    #define min(x,y) x<y?x:y
    #define max(x,y) x>y?x:y
    const int maxf=0x7fffffff;
    using namespace std;
    
    struct arr{
        int y,w,next;
    }a[1000001];
    int n,m,nm,ans;
    int ls[4001],q[4001],ha[4001];
    
    void add(int u,int v,int z){
        nm++; a[nm].y=v; a[nm].w=z; a[nm].next=ls[u]; ls[u]=nm;
        nm++; a[nm].y=u; a[nm].next=ls[v]; ls[v]=nm;
    }
    
    void init(){
        nm=1; int x,y;
        scanf("%d %d",&m,&n);
        scanf("%d %d",&x,&y);
        while (x!=y and x!=-1){
            add(x,y,1);
            scanf("%d %d",&x,&y);
        }
        for(int i=1;i<=n;++i){
            if (i<=m) add(0,i,1);
            else add(i,n+1,1);
        }
    }
    
    bool bfs(){
        int x,t=0,w=1,p,i;
        memset(ha,-1,sizeof(ha));
        q[t]=ha[0]=0;
        while (t<w){
            x=q[t++];
            i=ls[x];
            while(i){
                if (a[i].w&&ha[a[i].y]<0){
                    q[w++]=a[i].y;
                    ha[a[i].y]=ha[x]+1;
                }
                i=a[i].next;
            }
        }
        if (ha[n+1]==-1) return 0;
        else return 1;
    }
    
    int dfs(int x,int maxf){
        if (x==n+1) return maxf;
        int i=ls[x];
        int w,used=0;
        while(i){
            if(a[i].w&&ha[a[i].y]==ha[x]+1){
                w=maxf-used;
                w=dfs(a[i].y,min(w,a[i].w));
                a[i].w-=w;
                a[i^1].w+=w;
                used+=w;
                if(used==maxf)return maxf;
            }
            i=a[i].next;
        }
        if (used==0) ha[x]=-1;
        return used;
    }
    
    void code(){
        ans=0;
        while (bfs()) ans+=dfs(0,maxf);
        if (ans!=0) printf("%d
    ",ans);
        else printf("No Solution!");
    }
    
    int main(){
        init();
        code();
        return 0;
    }
  • 相关阅读:
    zabbix api
    pymssql
    ssh别名登录,非常适合从跳板机登录其他主机
    apache httpd 不记录head 的请求。
    mysqldump 参数--single-transaction
    Detect operating system [zabbix]
    mysql 审计server_audit 模块
    Execl矩阵如何转化成Pajek的net文件
    4、keepalived高可用nginx负载均衡
    3、使用keepalived高可用LVS实例演示
  • 原文地址:https://www.cnblogs.com/zyx-crying/p/9319534.html
Copyright © 2020-2023  润新知