hdu 2389 Rain on your Parade【最大匹配】

题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=2389

用 Hopcroft-Karp 算法
匈牙利算法会超时

代码:

#include <stdio.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <algorithm>
#include <vector>
#include <string.h>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <sstream>
#include <time.h>

using namespace std;

const int N = 3005;
const int INF = 1 << 28;

int g[N][N];
int Mx[N];
int My[N];
int dx[N];
int dy[N];
bool used[N];

int Nx, Ny, dis;

bool searchP()
{
    dis = INF;
    int i, v, u;
    std::queue<int> Q;

    memset(dx, -1, sizeof(dx));
    memset(dy, -1, sizeof(dy));
    for (i = 0; i<Nx; i++)
    {
        if (Mx[i] == -1)
        {
            Q.push(i);
            dx[i] = 0;
        }
    }
    while (!Q.empty())
    {
        u = Q.front();
        Q.pop();
        if (dx[u]>dis) break;
        for (v = 0; v<Ny; v++)
        {
            if (g[u][v] && dy[v] == -1)
            {
                dy[v] = dx[u] + 1;
                if (My[v] == -1) dis = dy[v];
                else
                {
                    dx[My[v]] = dy[v] + 1;
                    Q.push(My[v]);
                }
            }
        }
    }
    return dis != INF;
}

bool DFS(int u)
{
    int v;
    for (v = 0; v<Ny; v++)
    {
        if (g[u][v] && !used[v] && dy[v] == dx[u] + 1)
        {
            used[v] = true;
            if (My[v] != -1 && dy[v] == dis) continue;
            if (My[v] == -1 || DFS(My[v]))
            {
                My[v] = u;
                Mx[u] = v;
                return true;
            }
        }
    }
    return false;
}

int Hungary()
{
    int u;
    int ret = 0;
    memset(Mx, -1, sizeof(Mx));
    memset(My, -1, sizeof(My));
    while (searchP())
    {
        memset(used, false, sizeof(used));
        for (u = 0; u<Nx; u++)
            if (Mx[u] == -1 && DFS(u))  ret++;
    }
    return ret;
}

struct Peo
{
    int x;
    int y;
    int s;
}peo[3010];

int n, m;
int main()
{
    int t, cases = 1;
    scanf("%d", &t);
    while (t--)
    {
        memset(g, 0, sizeof(g));
        int T, x, y;
        scanf("%d", &T);
        scanf("%d", &m);
        for (int i = 1;i <= m;i++)
            scanf("%d%d%d", &peo[i].x, &peo[i].y, &peo[i].s);
        scanf("%d", &n);
        for (int i = 1;i <= n;i++)
        {
            scanf("%d%d", &x, &y);
            for (int j = 1;j <= m;j++)
            {
                int Dis = ceil(sqrt((peo[j].x - x) * (peo[j].x - x) + (peo[j].y - y) * (peo[j].y - y)) * 1.0 / peo[j].s);
                if (Dis <= T)
                    g[i-1][j-1] = 1;
            }
        }

        Nx = n;
        Ny = m;
        int ans = Hungary();

        printf("Scenario #%d:\n%d\n\n", cases++, ans);
    }
    return 0;
}

你可能感兴趣的:(hdu 2389 Rain on your Parade【最大匹配】)