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
  • 相关阅读:
    扩展JSON
    字符串格式化---- String.prototype.format
    HigntChats应用举例--报表
    django Form组件之解决数据无法动态显示之BUG----以博客园添加新随笔页为主
    报错:jquery3.1.1报错Uncaught TypeError: a.indexOf is not a function
    HDU 6166 Senior Pan(多校第九场 二进制分组最短路)
    HDU 6069 Counting Divisors(区间素数筛法)
    hdu 6058 Kanade's sum (计算贡献,思维)
    HDU 6052 To my boyfriend(容斥+单调栈)
    HDU 6041 I Curse Myself(点双联通加集合合并求前K大) 2017多校第一场
  • 原文地址:https://www.cnblogs.com/FrozenApple/p/4940264.html
Copyright © 2011-2022 走看看