"Network flow 24 questions" paired with pilot/pilot matching scheme

Source: Internet
Author: User
Tags bool printf stdin time limit
[Network flow 24 questions] paired with pilots
★★☆ input file: flyer.in output file: flyer.out simple comparison
Time limit: 1 s memory limit: MB
"Problem description"
The Flight brigade has a number of pilots from all over the world who specialize in one type of aircraft, each of which has two drivers and requires a pilot and a co-pilot. For a variety of reasons, such as co-ordination, some pilots cannot fly on the same plane and ask how to match the driver to make the most of the flight.

As shown in the figure, suppose there are 10 drivers, as shown in Figure v1,v2,...,v10 represents up to 10 drivers, of which V1,V2,V3,V4,V5 is the pilot, V6,V7,V8,V9,V10 is the co-pilot. If a pilot and a co-pilot can fly on the same plane, on behalf of both of them a line between the two, two people can not fly with the aircraft, it is not connected. For example, V1 and V7 can fly on the same plane, while V1 and V8 do not. Please match the pilot to make the most of the aircraft sailing. Note: Because of the strict division of the driving work, two or two copilot pilots can not fly on the plane.
"Input Format"
Input file has several lines
The first line, two integers n and N1, indicates a total of n pilots (2<=N<=100), including N1 pilots.
There are several lines below, each with 2 numbers, A, B. Indicates that both driver A and copilot B can fly on the same machine.
Note: The pilot's number is before the driver's number is less than the pilot's number.
"Output Format"
Output file has a row
The first line, 1 integers, indicates the maximum number of aircraft to take off.
"Input and Output sample"
Input file name: flyer.in
10 5
1 7
2 6
2 10
3 7
4 8
5 9
Output file name: Flyer.out
4
Paired pilots between the connecting side, if you want to use the maximum flow, the source point 0 and the driver connected side, the meeting point N+1 and the copilot connected side, the flow is 1, if you want to use a binary map matching, directly connected to the end of a run the Hungarian algorithm on the line.
Note: while ((X=read (), Y=read ())!=eof) do not know why the loop is dead.
Network Stream Dinic:
#include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> using namespace std;
int m,n,i,x,y,ans,sum,cnt=1,h[100001]={0},head,tail,q[100001],dis[100001];
struct node {int to,next,v;};

Node edge[100001];
    int read () {int w=0,c=1;
    Char Ch=getchar ();
        while (ch< ' 0 ' | | ch> ' 9 ') {if (ch== '-') c=-1;
      Ch=getchar ();
        } while (ch>= ' 0 ' && ch<= ' 9 ') {w=w*10+ch-' 0 ';
      Ch=getchar ();
} return w*c;
    } void Add (int u,int v,int W) {cnt++;
    Edge[cnt].next=h[u];
    h[u]=cnt;
    Edge[cnt].to=v;
Edge[cnt].v=w;
    } bool BFs () {int j,p;
    memset (dis,-1,sizeof (dis));
    q[0]=0;
    dis[0]=0;
    Head=0;
    tail=1;
        while (Head<tail) {head++;
        J=q[head];
        P=H[J]; while (p) {if (dis[edge[p].to]<0 && edge[p].v>0) {dis[ Edge[p].to]=dis[j]+1;
                tail++;
              q[tail]=edge[p].to;
          } P=edge[p].next;
    }} if (dis[n+1]>0) return true;
else return false;
    } int dfs (int x,int f) {int w,used=0,i=h[x];
    if (x==n+1) return F;
            while (i) {if (edge[i].v && dis[edge[i].to]==dis[x]+1) {w=f-used;
            W=dfs (Edge[i].to,min (W,EDGE[I].V));
            Edge[i].v-=w;
            Edge[i^1].v+=w;
            Used+=w;
          if (used==f) return F;
      } I=edge[i].next;
    } if (!used) dis[x]=-1;
return used;
    } int main () {freopen ("flyer.in", "R", stdin);
    Freopen ("Flyer.out", "w", stdout);
    N=read ();
    M=read ();
        for (i=1;i<=m;i++) {Add (0,i,1);
      Add (i,0,1);
        } for (i=m+1;i<=n;i++) {Add (i,n+1,1);
      Add (n+1,i,1);
     } while ((scanf ("%d%d", &x,&y))!=eof) {Add (x,y,1);
      Add (y,x,1);
    } ans=0;
    while (BFS ()) while (Sum=dfs (0,0X7FFFFFFF)) ans+=sum;  
    printf ("%d", ans);
    Fclose (stdin);
    Fclose (stdout);
return 0; }

Binary graph matching:

#include <cstdio> #include <cstring> using namespace std;
int Cnt=1,head[100001]={0},part[100001]={0},n,m,i,x,y,ans;
struct node {int to,next;};
Node edge[100001]={0};

BOOL used[100001];
    int read () {int w=0,c=1;
    Char Ch=getchar ();
        while (ch< ' 0 ' | | ch> ' 9 ') {if (ch== '-') c=-1;
      Ch=getchar ();
        } while (ch>= ' 0 ' && ch<= ' 9 ') {w=w*10+ch-' 0 ';
      Ch=getchar ();
} return w*c;
    } void Add (int u,int v) {cnt++;
    Edge[cnt].next=head[u];
    head[u]=cnt;
Edge[cnt].to=v;
    } bool Find (int s) {int k;
            for (K=head[s];k;k=edge[k].next) if (!used[edge[k].to]) {used[edge[k].to]=true;
                if (!part[edge[k].to] | | | find (PART[EDGE[K].TO)) {part[edge[k].to]=s;
              return true;
}} return false;
    } int main () {freopen ("flyer.in", "R", stdin); Freopen ("Flyer.out", "W", stdout);
    N=read ();
    M=read ();
        while ((scanf ("%d%d", &x,&y)!=eof) {Add (x, y);
      Add (y,x);
    } ans=0;
        for (i=1;i<=m;i++) {memset (used,false,sizeof (used));
      if (find (i)) ans++;
    } printf ("%d", ans);
    Fclose (stdin);
    Fclose (stdout);
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.