zoukankan      html  css  js  c++  java
  • 网络流24题 飞行员配对方案问题

    https://www.luogu.org/problem/show?pid=2756

    题目背景

    第二次世界大战时期..

    题目描述

    英国皇家空军从沦陷国征募了大量外籍飞行员。由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 名飞行员,其中1 名是英国飞行员,另1名是外籍飞行员。在众多的飞行员中,每一名外籍飞行员都可以与其他若干名英国飞行员很好地配合。如何选择配对飞行的飞行员才能使一次派出最多的飞机。对于给定的外籍飞行员与英国飞行员的配合情况,试设计一个算法找出最佳飞行员配对方案,使皇家空军一次能派出最多的飞机。

    对于给定的外籍飞行员与英国飞行员的配合情况,编程找出一个最佳飞行员配对方案,使皇家空军一次能派出最多的飞机。

    输入输出格式

    输入格式:

    第 1 行有 2 个正整数 m 和 n。n 是皇家空军的飞行员总数(n<100);m 是外籍飞行员数。外籍飞行员编号为 1~m;英国飞行员编号为 m+1~n。

    接下来每行有 2 个正整数 i 和 j,表示外籍飞行员 i 可以和英国飞行员 j 配合。最后以 2个-1 结束。

    输出格式:

    第 1 行是最佳飞行员配对方案一次能派出的最多的飞机数 M。接下来 M 行是最佳飞行员配对方案。每行有 2个正整数 i 和 j,表示在最佳飞行员配对方案中,飞行员 i 和飞行员 j 配对。如果所求的最佳飞行员配对方案不存在,则输出‘No Solution!’。

    输入输出样例

    输入样例#1:
    5 10
    1 7
    1 8
    2 6
    2 9
    2 10
    3 7
    3 8
    4 7
    4 8
    5 10
    -1 -1
    输出样例#1:
    4
    1 7
    2 9
    3 8
    5 10 
    最大流
    #include<cstdio>
    #include<queue>
    #include<algorithm>
    using namespace std;
    int n,m,tot=1,ans;
    int src,decc;
    int front[210],nextt[20601],to[20601],cap[20601],cnt[210],lev[210]; 
    queue<int>q; 
    void add(int u,int v,int w) 
    { 
        to[++tot]=v;cap[tot]=w;nextt[tot]=front[u];front[u]=tot; 
        to[++tot]=u;cap[tot]=0;nextt[tot]=front[v];front[v]=tot; 
    } 
    bool bfs() 
    { 
        for(int i=0;i<=n+m+3;i++) {cnt[i]=front[i];lev[i]=-1;} 
        while(!q.empty()) q.pop(); 
        q.push(src);lev[src]=0; 
        while(!q.empty()) 
        { 
            int now=q.front();q.pop(); 
            for(int i=front[now];i!=0;i=nextt[i]) 
            { 
                int t=to[i]; 
                if(cap[i]>0&&lev[t]==-1) 
                { 
                    q.push(t); 
                    lev[t]=lev[now]+1; 
                    if(t==decc) return true; 
                } 
            } 
        } 
        return false; 
    } 
    int dinic(int now,int flow) 
    { 
        if(now==decc) return flow; 
        int delta,rest=0; 
        for(int & i=cnt[now];i!=0;i=nextt[i]) 
        { 
            int t=to[i]; 
            if(lev[t]==lev[now]+1&&cap[i]>0) 
            { 
                delta=dinic(t,min(cap[i],flow-rest)); 
                if(delta) 
                { 
                    cap[i]-=delta;cap[i^1]+=delta; 
                    rest+=delta;if(rest==flow) break; 
                } 
            } 
        } 
        if(rest!=flow) lev[now]=-1; 
        return rest; 
    } 
    int main()
    {
        scanf("%d%d",&n,&m);
        decc=n+m+3;
        for(int i=1;i<=n;i++) add(src,i,1);
        for(int i=1;i<=m;i++) add(i+n,decc,1);
        int a,b;
        while(scanf("%d%d",&a,&b)!=EOF)
        {
            if(a==-1) break;
            add(a,b+n,30001);
        }
        while(bfs()) ans+=dinic(src,30001);
        printf("%d
    ",ans);
        if(ans==0)
        {
            printf("No Solution!");
            return 0;
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=front[i];j;j=nextt[j])
            {
                if(cap[j]==30001||to[j]==src) continue;
                printf("%d %d
    ",i,to[j]-n);
            }
        }
    }
  • 相关阅读:
    新浪微盘又是一个给力的产品啊,
    InfoQ: 百度数据库架构演变与设计
    列式数据库——Sybase IQ
    MapR初体验 淘宝共享数据平台 tbdata.org
    IBM正式发布新一代zEnterprise大型机(组图) 大型机,IBM,BladeCenter,美国,纽约 TechWeb News
    1TB is equal to the number of how many GB? 1PB equal to is equal to the number of TB? 1EB PB? | PCfault.com
    Cassandra vs HBase | WhyNosql
    The Hadoop Community Effect
    雅虎剥离开源软件平台 Hadoop ,与风投新建 Hortonworks 公司 品味雅虎
    RowOriented Database 、ColumnOriented Database 、KeyValue Store Database 、DocumentOriented Database
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/6502331.html
Copyright © 2011-2022 走看看