Binary graph matching (with algorithm introduction) (Example: Hamster)

Source: Internet
Author: User

This is a magical algorithm.

The problem that it solves is:

For a graph, there are only black dots and white dots, and there are edges between the black and white dots, and the maximum number of matches to ask for black and white points.

To finish the problem, let's talk about algorithms.

First we have to find the first black point that is not currently matched (this algorithm can only do one side (black or white))

Then we find the point with which he is connected and mark the two points as a match.

Then we look down until we find a point where the point is connected to a point that has been matched (recorded as V).

So we're going to try to replace it with the point where the V-node matches the original (recursion is needed, because the points that are likely to be found are matched too.) )

Then if the point can be matched, then modify it, otherwise it will not match this point.

As we know, if this point match fails, then the next match to this point, you can skip directly (optimization)

Let's put an example here.

———————————————— I'm a split line ————————————————

Title Description

There were a total of n sacs in the hamster family, and they dug a hole in the ground to escape predators. When predators arrive, they must run into the hole in s time. But because the size of the hole is limited, only 1 hamsters can be hidden in each hole. All hamsters run at a speed of V. The hamster that stays outside the hole will be eaten. The Sac family plans an escape plan to allow as many hamsters as possible to escape.

Input

The input file contains multiple data, and the first line of each data consists of 4 integers less than 100: n,m,s,v. The following n lines give the position coordinates of each sac, and the M-line gives the hole coordinates.

Output

For each data output cannot escape the number of cystic hamsters.

———————————————— I'm a split line ————————————————

This problem is obviously a binary map match, just to determine if the first mouse can run to the J Pit, then from I to J connected Edge

Paste the following code

#include <cstdio>#include<cstring>#include<cmath>#defineEPS 1e-9#defineMN 101#defineM 10001using namespacestd;intN,m,s,v,ans,num;structedge{intTo,next;} G[M]; intHEAD[MN],LKY[MN];BOOLVIS[MN];Doublemaxdis=0; DoubleX[MN],Y[MN];DoubleDistDoubleADoubleBDoubleCDoubleD) {return(Double) sqrt ((b-a) * (b-a) + (d-c) * (dc));} voidInsintUintV) {g[++num].next=head[u];head[u]=num;g[num].to=v;} BOOLMatchintu) {      for(intI=head[u];i;i=g[i].next)if(!Vis[g[i].to]) {Vis[g[i].to]=1; if(!lky[g[i].to]| |match (lky[g[i].to])) {Lky[g[i].to]=u;return true; }         }     return false; } intMain () { while(SCANF ("%d%d%d%d", &n,&m,&s,&v)! =EOF) {Maxdis=1.0*s*v; Ans=num=0; Doubleqx,qy; memset (Head,0,sizeof(head));  for(intI=1; i<=n;i++) scanf ("%LF%LF",&x[i],&Y[i]);  for(intI=1; i<=m;i++) {scanf ("%LF%LF",&qx,&qy);  for(intj=1; j<=n;j++)                 if(Dist (X[J],QX,Y[J],QY) +eps<maxdis) ins (j,i); } memset (Vis,0,sizeof(VIS)); memset (Lky,0,sizeof(LKY));  for(intI=1; i<=n;i++) {memset (Vis,0,sizeof(VIS)); if(Match (i)) ans++; } printf ("%d\n", N-ans); } }

Binary graph matching (with algorithm introduction) (Example: Hamster)

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.