Problem of pilot pairing scheme for network Flow 24 questions (nefu475)

Source: Internet
Author: User
Tags prepare time limit

Problem of pilot pairing scheme
problem:475

Time limit:1000ms

Memory limit:65536k

Description
During the Second World War, the Royal Air Force recruited a large number of foreign pilots from the occupied countries. Each aircraft dispatched by the Royal Air Force needs to be equipped with 2 pilots, including 1 British pilots and 1 foreign pilots, who can match the skill and language of the craft. Among the many pilots, each of the foreign pilots was able to work well with several other British pilots. How to choose a paired flight pilot to make the most out of one flight. For a given foreign pilot and British pilots, try to design an algorithm to find out the best pilot matching scheme, so that the Royal Air Force can send the most aircraft.
For a given foreign pilot with a British pilot, programming to find the best pilot matching scheme, so that the Royal Air Force can send the most aircraft.
Input
Multiple sets of data input.
The 1th row of each group of inputs has 2 positive integers m and N. n is the total number of pilots of the RAF (N<100); M is the number of foreign pilots. The foreign pilot is numbered 1~m; The British pilot number is m+1~n. Next, there are 2 positive integers i and j for each line, indicating that the foreign pilot I can match the British pilot J. The file ends with 2
A-1 end.
Output
Each group outputs the best pilot pairing scheme can be dispatched at the maximum number of aircraft m.
If the optimal pilot pairing scheme is not present, then output ' no solution! '.
Sample Input
5 10
1 7
1 8
2 6
2 9
2 10
3 7
3 8
4 7
4 8
5 10
-1-1
Sample Output
4
Hint
Source
24 Problems of linear programming and network flow

The problem is to add foreign and the source of the right value of 1, foreign and corresponding to the United Kingdom plus the right value of 1, the United Kingdom and meeting point to build the right value of 1, because it is 1 to 1, so each side of the weight of 1, and then run the maximum flow on the line, in fact, this problem should be used to do

#include <stdio.h> #include <iostream> using namespace std;
const int OO=1E9;
/**oo represents infinity */const int mm=111111111;
/**MM represents the maximum number of edges, remember that if the original twice times, in addition to the side of the time are two-way * * const int mn=999;
/**MN represents the maximum number of points */int node,src,dest,edge;
/**node indicates the number of nodes, SRC represents the source point, dest represents the meeting point, EDGE statistics edge number */int ver[mm],flow[mm],next[mm];
The node that the/**ver edge points to, the capacity of the flow edge, next edge of the next list */int head[mn],work[mn],dis[mn],q[mn];
    void Prepare (int _node, int _src,int _dest) {node=_node,src=_src,dest=_dest;
    for (int i=0; i<node; ++i) head[i]=-1;
edge=0; }/** adds a U to v capacity of C */void Addedge (int u, int v, int c) {ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=
    edge++;
ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
    }/** calculates the shortest distance between each point and the source point, if it cannot reach the end of the meeting point description algorithm */bool Dinic_bfs () {int i,u,v,l,r=0;
    for (i=0; i<node; ++i) dis[i]=-1;
    dis[q[r++]=src]=0;
            For (l=0, l<r; ++l) for (I=head[u=q[l]]; i>=0; i=next[i]) if (flow[i]&&dis[v=ver[i]]<0)
        {        /** This side must have the remaining capacity */dis[q[r++]=v]=dis[u]+1;
            if (v==dest) return 1;
} return 0;
    /** search for an augmented path algorithm for a viable stream, find it by node distance, speed up */int Dinic_dfs (int u, int exp) {if (u==dest) return exp; /**work is a temporary chain header, where I refer to it, so that the searched edges no longer look for */for (int &i=work[u],v,tmp; i>=0; i=next[i]) if (flow[i]&&amp
            ;d is[v=ver[i]]==dis[u]+1&& (Tmp=dinic_dfs (V,min (exp,flow[i))) >0) {flow[i]-=tmp;
            flow[i^1]+=tmp;
        /** positive and negative side capacity change */return TMP;
} return 0;
    } int Dicnic_flow () {int i,ret=0,delta;
        while (DINIC_BFS ()) {for (i=0; i<node; ++i) work[i]=head[i];
    while (Delta=dinic_dfs (Src,oo)) Ret+=delta;
} return ret;
    } int main () {int n,m,x,y;
    Ios::sync_with_stdio (FALSE);
        while (Cin>>m>>n) {prepare (n+2,0,n+1); while (cin>>x>>y) {if (x==-1&&y==-1) break;
        Addedge (x,y,1);
        } for (int i=1;i<=m;i++) Addedge (0,i,1);
        for (int i=m+1;i<=n;i++) Addedge (i,n+1,1);
        int Sum=dicnic_flow (); if (sum==0) cout<< "No solution!"
        <<endl;
    else cout<<sum<<endl;
} return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.