[NOI题库][POJ2536][匈牙利算法][二分图最大匹配]Gopher II

先上一个匈牙利算法的学习博客,通俗易懂,值得一看。 趣写算法系列之--匈牙利算法 

点击跳转原题

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7707   Accepted: 3146

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 10
1.0 1.0
2.0 2.0
100.0 100.0
20.0 20.0

Sample Output

1

Source


很裸的一个匈牙利。

代码

 
#include <stdio.h>
#include <math.h>
#include <string.h>
struct location{
    double x, y;
    int belong;
}hole[ 110 ], gopher[ 110 ];

bool line[ 110 ][ 110 ]; // line[ gopher ][ hole ];
int n, m, s, v;
double maxd;
bool used[ 110 ];
int ans = 0;

double dis( double x1, double y1, double x2, double y2)
{
    return ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
}

bool find( int x )
{
    int i;
    for( i = 1; i <= m; i++ ){
        if( line[ x ][ i ] && !used[ i ] ){
            used[ i ] = true;
            if( hole[ i ].belong == 0 || find( hole[ i ].belong ) ){
                hole[ i ].belong = x;
                return true;
            }
        }
    }
    return false;
}

int main()
{


    while( scanf("%d%d%d%d", &n, &m, &s, &v) != EOF ){
        memset( hole, 0, sizeof( hole ));
        memset( gopher, 0, sizeof( gopher ));
        memset( line, 0, sizeof( line ));
        ans = 0;
        int i, j, k;

        maxd = v * s * v * s;
        /* gopher */
        for( i = 1; i <= n; i++ )
            scanf("%lf%lf", &gopher[ i ].x, &gopher[ i ].y);

        /* hole */
        for( i = 1; i <= m; i++ ){
            scanf("%lf%lf", &hole[ i ].x, &hole[ i ].y);
            for( j = 1; j <= n; j++ ){
                if( dis( hole[ i ].x, hole[ i ].y, gopher[ j ].x, gopher[ j ].y) <= maxd ){
                    /* AddEdge */
                    line[ j ][ i ] = true;
                }
            }
        }

        for( i = 1; i <= n; i++ ){
            memset( used, 0, sizeof( used ));
            if( find( i ) ) ans++;
        }

        printf("%d\n", n - ans);

    }
    return 0;
}
View Code

 

posted @ 2015-11-05 17:34  FrozenApple  阅读(204)  评论(0编辑  收藏  举报