POJ 2536题

//类型:二分图的最大匹配(匈牙利算法实现)
//建图:如果 gopher和hole最s时间内可达,则建立一条边,剩下的则是利用匈牙利算法求最大匹配
#include <stdio.h>
#include <string.h>
#include <math.h>
//#include <conio.h>
#define arraysize 101
typedef struct gopherxy
{
   double x;
   double y;
}gopherxy;
typedef struct holexy
{
    double x;
    double y;
}holexy;
int n,m,s,v;
gopherxy gophers[arraysize];
holexy holes[arraysize];
int match[arraysize];
bool final[arraysize];
int map[arraysize][arraysize];
double distance(double x1,double y1,double x2,double y2)     //计算gopher和hole的距离
{
    return sqrt((double)(x1-x2)*(x1-x2)+(double)(y1-y2)*(y1-y2));
}
bool DFS(int p)
{
     int i,j;
     int temp;
     for(i=1;i<m+1;++i)
     {
         if(map[p][i] && !final[i])
         {
            final[i] = true;
            temp = match[i];
            match[i] = p;
            if(temp==0 || DFS(temp)) return true;
            match[i] = temp;
         }
     }
     return false;
}
int mat()
{
    int i,j;
    int maxmatch = 0;
    for(i=1;i<n+1;++i)
    {
       memset(final,0,sizeof(final));
       if(DFS(i)) maxmatch++;
    }
    return maxmatch;
}
int main()
{
    int i,j;
    double x,y;
    //freopen("1.txt","r",stdin);
    while(scanf("%d%d%d%d",&n,&m,&s,&v)!=EOF)
    {
        memset(map,0,sizeof(map));
        memset(match,0,sizeof(match));
        for(i=1;i<n+1;++i)
        {
           scanf("%lf%lf",&x,&y);
           gophers[i].x = x;
           gophers[i].y = y;
        }
        for(i=1;i<m+1;++i)
        {
           scanf("%lf%lf",&x,&y);
           holes[i].x = x;
           holes[i].y = y;
        }
        for(i=1;i<n+1;++i)
        {
            for(j=1;j<m+1;++j)
            {
                if(distance(gophers[i].x,gophers[i].y,holes[j].x,holes[j].y)-v*s<0) //如果gopher和hole在s时间内可达,则建立一条边
                   map[i][j] = 1;
            }
        }       
        printf("%d\n",n-mat());
    }
    //getch();
    return 0;
}

你可能感兴趣的:(poj)