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
  • 相关阅读:
    时间已经到了再一次发了博客
    java排错
    毕业设计
    做生意这件事
    性能优化的原理和理解
    【Linux】-NO.87.Assembly.1.滴水逆向.1.001-【介绍】-
    【Java】NO.80.Note.1.Java.1.001-【Java 各种特性概念】
    【Common】NO.81.Note.1.Common.1.001-【各种英文符号的表示及念法】
    【UML】NO.55.EBook.8.UML.3.001-【UML和模式应用 第3版】
    【UML】NO.54.EBook.6.UML.2.002-【Thinking In UML 大象 第二版】- UML 核心元素
  • 原文地址:https://www.cnblogs.com/FrozenApple/p/4940264.html
Copyright © 2011-2022 走看看