Problem of the "solution" pilot pairing scheme

Source: Internet
Author: User

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 with a British pilot, programming to find the best pilot matching scheme, so that the Royal Air Force can send the most aircraft.

Input Description

The input data is provided by the file Input.txt. The 1th line of the file 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-1.

Output Description
The best pilot pairing scheme is output to file Output.txt at the end of the program run. Line 1th is the best pilot matching program. The maximum number of aircraft to be dispatched at a time is M.

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

5 10


The maximum matching problem of binary graph can be solved by the Hungarian algorithm, and can be converted to multi-source point multi-meeting point problem directly with the maximum flow.

Modeling methods:

1. Set a super source point S, set a super sink point T.

2. From S to each foreign pilot with an edge, capacity of 1.

3. One edge from each royal pilot to T, with a capacity of 1.

4. According to the input, a 1 edge is attached to the royal pilot from each foreign pilot.

The maximum flow from S to T is obtained for the network.

/*name:wxjlzbcdmemory:6340kbtime:11ms*/#include <cstdio> #include <climits> #include <cstring># include<algorithm>const int Maxn=20010;const int maxm=500010;using namespace std;struct edge{int v,next,val;} E[maxm];int p[maxn],eid;int n,m;int h[maxn];int gap[maxn];int s,t;inline void Add (int from,int to,int val) {e[eid].v=to    ; e[eid].val=val;    E[eid].next=p[from]; p[from]=eid++;}    inline int dfs (int pos,int cost) {if (pos==t) return cost;    int j,minh=n-1,lv=cost,d;        for (j=p[pos];j!=-1;j=e[j].next) {int v=e[j].v,val=e[j].val;                if (val>0) {if (H[v]+1==h[pos]) {if (lv<e[j].val) d=lv;                else D=e[j].val;                D=dfs (V,D);                E[j].val-=d;                E[j^1].val+=d;                Lv-=d;                if (h[s]>=n) return cost-lv;            if (lv==0) break;        } if (H[v]<minh) minh=h[v]; }} if (Lv==cost) {--gap[h[pOS]];        if (gap[h[pos]]==0) h[s]=n;        h[pos]=minh+1;    ++gap[h[pos]]; } return COST-LV;} int isap (int st,int ed) {s=st;    t=ed;    int ret=0;    memset (Gap,0,sizeof (GAP));    memset (h,0,sizeof (h));    Gap[st]=n;    while (h[st]<n) Ret+=dfs (St,int_max); return ret;}    int main () {freopen ("air.in", "r+", stdin), Freopen ("Air.out", "w+", stdout), scanf ("%d%d", &m,&n); memset (P,-1,sizeof (P)); Eid=0;int i;int tnum=m+n+2;for (i=2;i<=m+1;++i) {Add (1,i,1); add (i,1,0);}    for (i=m+2;i<=tnum;++i) {Add (i,tnum,1); add (tnum,i,0);}        while (1) {int u,v,c;        scanf ("%d%d", &u,&v);        if (u==-1&&v==-1) break;        ++u;        ++v;        V+=m;        Add (u,v,1);    Add (v,u,0);    } printf ("%d\n", Isap (1,tnum)); return 0;}




Problem of the "solution" pilot pairing scheme

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.