Rain on your Parade HDU - 2389 【Hopcroft-karp 算法】

You’re giving a party in the garden of your villa by the sea. The party is a huge success, and everyone is here. It’s a warm, sunny evening, and a soothing wind sends fresh, salty air from the sea. The evening is progressing just as you had imagined. It could be the perfect end of a beautiful day.
But nothing ever is perfect. One of your guests works in weather forecasting. He suddenly yells, “I know that breeze! It means its going to rain heavily in just a few minutes!” Your guests all wear their best dresses and really would not like to get wet, hence they stand terrified when hearing the bad news.
You have prepared a few umbrellas which can protect a few of your guests. The umbrellas are small, and since your guests are all slightly snobbish, no guest will share an umbrella with other guests. The umbrellas are spread across your (gigantic) garden, just like your guests. To complicate matters even more, some of your guests can’t run as fast as the others.
Can you help your guests so that as many as possible find an umbrella before it starts to pour?

Given the positions and speeds of all your guests, the positions of the umbrellas, and the time until it starts to rain, find out how many of your guests can at most reach an umbrella. Two guests do not want to share an umbrella, however.
Input
The input starts with a line containing a single integer, the number of test cases.
Each test case starts with a line containing the time t in minutes until it will start to rain (1 <=t <= 5). The next line contains the number of guests m (1 <= m <= 3000), followed by m lines containing x- and y-coordinates as well as the speed si in units per minute (1 <= si <= 3000) of the guest as integers, separated by spaces. After the guests, a single line contains n (1 <= n <= 3000), the number of umbrellas, followed by n lines containing the integer coordinates of each umbrella, separated by a space.
The absolute value of all coordinates is less than 10000.
Output
For each test case, write a line containing “Scenario #i:”, where i is the number of the test case starting at 1. Then, write a single line that contains the number of guests that can at most reach an umbrella before it starts to rain. Terminate every test case with a blank line.
Sample Input

2
1
2
1 0 3
3 0 3
2
4 0
6 0
1
2
1 1 2
3 3 2
2
2 2
4 4

Sample Output
Scenario #1:
2

Scenario #2:
2
题意:宴会时下雨,客人们要避雨,两个客人不能同用一把伞,输入第一行给出测试用例的数目,每个测试用例第一行给出一个数t(min)代表还有多长时间将要下雨,下一行给出一个数n,代表有n个客人,下面
n行,给出三个数,分别是客人所在的横坐标和纵坐标以及客人的跑步速度(m/min)。
下一行再输入一个数m,代表伞的数目,下面m行,每行两个数,代表伞的横坐标和纵坐标。
求出最多有多少客人可以拿到伞。
题解:二分图最大匹配,Hopcroft-karp 算法模板题时间复杂度为O(sqrt(n)m),匈牙利算法时间复杂度O(nm)超时。
Hopcroft-karp 算法模板(来源https://blog.csdn.net/discreeter/article/details/51649155):

//hopcroft_karp算法,复杂度O(sqrt(n)*m)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
 
const int N = 320;
const int INF = 0x3f3f3f3f;
struct edge
{
    int to, next;
}g[N*N];
int match[N], head[N];
bool used[N];
int p, n;
int nx, ny, cnt, dis; //nx,ny分别是左点集和右点集的点数
int dx[N], dy[N], cx[N], cy[N]; //dx,dy分别维护左点集和右点集的标号
//cx表示左点集中的点匹配的右点集中的点,cy正好相反
void add_edge(int v, int u)
{
    g[cnt].to = u, g[cnt].next = head[v], head[v] = cnt++;
}
bool bfs() //寻找增广路径集,每次只寻找当前最短的增广路
{
    queue<int> que;
    dis = INF;
    memset(dx, -1, sizeof dx);
    memset(dy, -1, sizeof dy);
    for(int i = 1; i <= nx; i++)
        if(cx[i] == -1) //将未遍历的节点入队,并初始化次节点距离为0
            que.push(i), dx[i] = 0;
    while(! que.empty())
    {
        int v = que.front(); que.pop();
        if(dx[v] > dis) break;
        for(int i = head[v]; i != -1; i = g[i].next)
        {
            int u = g[i].to;
            if(dy[u] == -1)
            {
                dy[u] = dx[v] + 1;
                if(cy[u] == -1) dis = dy[u]; //找到了一条增广路,dis为增广路终点的标号
                else
                    dx[cy[u]] = dy[u] + 1, que.push(cy[u]);
            }
        }
    }
    return dis != INF;
}
int dfs(int v)
{
    for(int i = head[v]; i != -1; i = g[i].next)
    {
        int u = g[i].to;
        if(! used[u] && dy[u] == dx[v] + 1) //如果该点没有被遍历过并且距离为上一节点+1
        {
            used[u] = true;
            if(cy[u] != -1 && dy[u] == dis) continue; //u已被匹配且已到所有存在的增广路终点的标号,再递归寻找也必无增广路,直接跳过
            if(cy[u] == -1 || dfs(cy[u]))
            {
                cy[u] = v, cx[v] = u;
                return 1;
            }
        }
    }
    return 0;
}
int hopcroft_karp()
{
    int res = 0;
    memset(cx, -1, sizeof cx);
    memset(cy, -1, sizeof cy);
    while(bfs())
    {
        memset(used, 0, sizeof used);
        for(int i = 1; i <= nx; i++)
            if(cx[i] == -1)
                res += dfs(i);
    }
    return res;
}
int main()
{
    int t, a, b;
    scanf("%d", &t);
    while(t--)
    {
        cnt = 0;
        memset(head, -1, sizeof head);
        scanf("%d%d", &p, &n);
        for(int i = 1; i <= p; i++)
        {
            scanf("%d", &a);
            for(int j = 0; j < a; j++)
            {
                scanf("%d", &b);
                add_edge(i, b);
            }
        }
        nx = p, ny = n;
        if(hopcroft_karp() == p) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值