POJ2536_Gopher II (maximum matching of bipartite graphs) and poj2536_gopherii

Source: Internet
Author: User

POJ2536_Gopher II (maximum matching of bipartite graphs) and poj2536_gopherii

Solution report

Http://blog.csdn.net/juncoder/article/details/38156509

Question Portal

Question:

N hamster, m hole, the time when the e arrived at the ground s, the speed at which the hamster moved v, and how many hamster will be eaten by the e.

Ideas:

The hamster and the hole are regarded as two sets, and a bipartite graph is created. Only the local rat to the hole time is less than the time when the e to the ground to connect the edge.

#include <cmath>#include <cstdio>#include <cstring>#include <iostream>using namespace std;int n,m,s,v,mmap[500][500],vis[500],pre[500];struct point{    double x,y;}G[200],H[200];double dis(point p1,point p2){    return  sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));}int dfs(int x){    for(int i=n+1;i<=n+m;i++){        if(!vis[i]&&mmap[x][i]){            vis[i]=1;            if(pre[i]==-1||dfs(pre[i])){                pre[i]=x;                return 1;            }        }    }    return 0;}int main(){    //std::ios::sync_with_stdio(false);    int i,j,a,b,t;    while(~scanf("%d%d%d%d",&n,&m,&s,&v)){        memset(pre,-1,sizeof(pre));        memset(mmap,0,sizeof(mmap));        for(i=1;i<=n;i++){            scanf("%lf%lf",&G[i].x,&G[i].y);        }        for(i=1;i<=m;i++){            scanf("%lf%lf",&H[i].x,&H[i].y);        }        for(i=1;i<=n;i++){            for(j=1;j<=m;j++){                double d=dis(G[i],H[j]);                if(d/v<=(double)s){                    mmap[i][n+j]=1;                }            }        }        int ans=0;        for(i=1;i<=n;i++){            memset(vis,0,sizeof(vis));            ans+=dfs(i);        }        printf("%d\n",n-ans);    }    return 0;}

Gopher II
Time Limit:2000 MS   Memory Limit:65536 K
Total Submissions:6438   Accepted:2640

Description

The gopher family, having averted the canine threat, must face a new predator.

The are n gophers and m gopher holes, each at distinct (x, y) coordinates. A hawk arrives and if a gopher does not reach a hole in s seconds it is vulnerable to being eaten. A hole can save at most one gopher. all the gophers run at the same velocity v. the gopher family needs an escape strategy that minimizes the number of vulnerable gophers.

Input

The input contains several cases. the first line of each case contains four positive integers less than 100: n, m, s, and v. the next n lines give the coordinates of the gophers; the following m lines give the coordinates of the gopher holes. all distances are in metres; all times are in seconds; all velocities are in metres per second.

Output

Output consists of a single line for each case, giving the number of vulnerable gophers.

Sample Input

2 2 5 101.0 1.02.0 2.0100.0 100.020.0 20.0

Sample Output

1



What is the matching of a bipartite graph? maximum matching: maximum weighted matching

Given a bipartite graph G, In a subgraph M of G, any two edges in the edge set of M are not attached to the same vertex. M is a matching.
Selecting the largest subset of the number of edges is called the maximal matching problem)
If in a match, each vertex in the graph is associated with an edge in the graph, it is called a full match or a complete match.
The maximum stream or Hungary algorithm can be used to obtain the maximum matching of a bipartite graph.
Reference: bk.baidu.com/view/503667.htm

Matlab program for maximum matching of bipartite graphs

[Num h] = maxnum (g); % g is the bipartite graph ing matrix %. A self-written maxnum function is called. The return value num is the maximum value, and h is the hij (not unique) maxnum. m content, which uses the Hungary algorithm also uses a recursive incpath function to find the augmented path function [num h] = maxnum (g) s = size (g ); global G_h; % matrix hij record selected global G_g; % matrix gij record matched global G_v; % record current path accessed node G_h = false (s ); % matrix hij is initially blank G_g = g; % matrix gij is the passed parameter gfor I = 1: s (1) G_v = false (1, s (2 )); % The Node accessed by each initialization path is empty incpath (I); % search for the augmented path endh = G_h from Ai; num = sum (h (:)); % The maximum number of matches output, and the matching matrix hclear global 'G _ H'; clear Global 'G _ G'; endfunction OK = incpath (I) % starting from Ai global G_h; global G_g; global G_v; OK = false; j = find (~ G_h (I, :) & G_g (I ,:)&~ G_v, 1); % find the condition Bjif isempty (j), return; end % cannot find return falseG_v (j) = true; % found, mark Bj as Access Node ii = find (G_h (:, j); % find Bj in original match if isempty (ii) % if not in original match G_h (I, j) = true; OK = true; return; end % find the end of the augmented path and return trueok = incpath (ii); % if the original match is in progress, call incpath recursively to find if OK % based on the matching Aii. if the recursive search returns G_h (I, j) = ~ G_h (I, j); G_h (ii, j) = ~ G_h (ii, j); OK = true; return; end % PATH Reversed return trueend
 

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.