Dinic solving the problem of the pilot pairing scheme for the maximum matching && network flow of two-dimensional graphs 24 questions

Source: Internet
Author: User

The source S and Sink T are added on the basis of the binary graph.
1, S to the x set of each vertex with a capacity of 1 with a forward edge.
2. Each vertex in the Y set has a forward edge with a capacity of 1 to T.
3. The edges between the XY sets are set to points from the points in the a collection to the point in the B set, with a forward edge of 1.

In order to find the maximum flow of the network, the traffic is the matching number, and all the full flow edge is a feasible solution.

So it was solved.

Pilot Pairing Program Questions:

Topic background

World War II period:

Title Description

The Royal Air Force recruited a large number of foreign pilots from the occupied country. 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 format:

The 1th line 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 (M<=N). 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. End with 2-1.

Output format:

Line 1th is the best pilot matching program. The maximum number of aircraft to be dispatched at a time is M. The next M-line is the best pilot pairing scheme. Each row has 2 positive integers i and j, indicating that pilot I and pilot J are paired in the best pilot pairing scheme. If the optimal pilot pairing scheme is not present, then output ' no solution! '.

Input Sample:

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
1 7
2 9
3 8
5 10

#include <cstdio> #include <queue> #include <algorithm> #include <cstring>using namespace std;# Define N 10000int n,m;int dep[n];int idx=1;int head[n],to[n],nex[n];int val[n];int s,t;int a,b;int ans;void addedge (int A,    int B,int c) {Nex[++idx]=head[a];    Head[a]=idx;    To[idx]=b; Val[idx]=c;}    BOOL BFS (int s,int T) {queue <int> q;    Q.push (S);    memset (dep,-1,sizeof (DEP));    dep[s]=0;        while (!q.empty ()) {int X=q.front ();        Q.pop (); for (int i=head[x];i;i=nex[i]) {if (val[i]&&dep[to[i]]==-1) {Dep[to[i]                ]=dep[x]+1;                Q.push (To[i]);            if (to[i]==t) return 1; }}} return 0;}    int dinic (int x,int flow) {int nowflow=flow;    if (x==t) return nowflow;            For (int. I=head[x];i;i=nex[i]) {if (val[i]>0&&dep[to[i]]==dep[x]+1) {int now; Now=dinic (To[i],min (nowflow,val[i]));            if (now==0) dep[to[i]]=-1;            Val[i]-=now;            Val[i^1]+=now;            Nowflow-=now;        if (nowflow==0) break; }} return flow-nowflow;}    int main () {scanf ("%d%d", &n,&m);    s=0,t=n+m+1;        for (int i=1;i<=n;i++) {Addedge (s,i,1);    Addedge (i,s,0);        } for (int i=n+1;i<=m;i++) {Addedge (i,t,1);    Addedge (t,i,0);        } while (scanf ("%d%d", &a,&b) ==2&&a!=-1&&b!=-1) {Addedge (a,b,1<<30);    Addedge (b,a,0);    } while (BFS (s,t)) ans+=dinic (s,1);        if (ans!=0) {printf ("%d\n", ans); for (int i=2;i<=idx;i+=2) {if (to[i]!=s&&to[i]!=t&&to[i^1]!=s&&to[i^1]!=t&        &AMP;VAL[I]&AMP;&AMP;VAL[I^1]) printf ("%d%d\n", to[i],to[i^1]); }} else puts ("No solution!");}

  

Dinic solving the problem of the pilot pairing scheme for the maximum matching && network flow of two-dimensional graphs 24 questions

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.