zoukankan      html  css  js  c++  java
  • [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 - ans);
    
        }
        return 0;
    }
    View Code
  • 相关阅读:
    pycharm过期后,修改hosts文件?
    三种格式化方式
    virtualenv安装及使用
    二分查找以及单例模式
    目录总览
    SQLAlchemy
    Redis
    linux 安装虚拟机
    shell基本命令
    Linux 命令大全
  • 原文地址:https://www.cnblogs.com/FrozenApple/p/4940264.html
Copyright © 2011-2022 走看看