[POJ 2536] Gopher ||

[题目链接]

          http://poj.org/problem?id=2536

[算法]

          匈牙利算法解二分图最大匹配

[代码]

        

#include <algorithm>  
#include <bitset>  
#include <cctype>  
#include <cerrno>  
#include <clocale>  
#include <cmath>  
#include <complex>  
#include <cstdio>  
#include <cstdlib>  
#include <cstring>  
#include <ctime>  
#include <deque>  
#include <exception>  
#include <fstream>  
#include <functional>  
#include <limits>  
#include <list>  
#include <map>  
#include <iomanip>  
#include <ios>  
#include <iosfwd>  
#include <iostream>  
#include <istream>  
#include <ostream>  
#include <queue>  
#include <set>  
#include <sstream>  
#include <stdexcept>  
#include <streambuf>  
#include <string>  
#include <utility>  
#include <vector>  
#include <cwchar>  
#include <cwctype>  
#include <stack>  
#include <limits.h>
using namespace std;
#define MAXN 110

int i,j,n,m,s,v,tot,ans;
pair<double,double> a[MAXN],b[MAXN];
int head[MAXN],match[MAXN << 1];
bool visited[MAXN << 1];

struct edge
{
        int to,nxt;
} e[MAXN * MAXN];
inline void addedge(int u,int v)
{
        tot++;
        e[tot] = (edge){v,head[u]};
        head[u] = tot;
}
inline double dist(pair<double,double> a,pair<double,double> b)
{
        return sqrt((a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second));
}
inline bool hungary(int u)
{
        int i,v;
        visited[u] = true;
        for (i = head[u]; i; i = e[i].nxt)
        {
                v = e[i].to;
                if (!visited[v])
                {
                        visited[v] = true;
                        if (!match[v] || hungary(match[v])) 
                        {
                                match[v] = u;
                                return true;
                        }        
                }        
        }        
        return false;
}
 
int main() 
{
        
        while (scanf("%d%d%d%d",&n,&m,&s,&v) != EOF)
        {    
                tot = 0;
                memset(head,0,sizeof(head));
                memset(match,0,sizeof(match));                
                for (i = 1; i <= n; i++) scanf("%lf%lf",&a[i].first,&a[i].second);
                for (i = 1; i <= m; i++) scanf("%lf%lf",&b[i].first,&b[i].second);
                for (i = 1; i <= n; i++)
                {
                        for (j = 1; j <= m; j++)
                        {
                                if (1.0 * dist(a[i],b[j]) / v <= 1.0 * s) 
                                        addedge(i,j + n); 
                        }
                }
                ans = 0;
                for (i = 1; i <= n; i++)
                {
                        memset(visited,false,sizeof(visited));
                        if (hungary(i)) ans++;
                }
                printf("%d\n",n - ans);
        }
        
        return 0;
    
}

 

posted @ 2018-08-02 13:24  evenbao  阅读(95)  评论(0编辑  收藏  举报