HDU2389Rain on your Parade 二分匹配Hopcroft-Karp

本文介绍了一个名为RainonyourParade的问题背景及其算法解决方案。该问题关注于如何最大效率地分配雨伞以保护尽可能多的人免受大雨侵袭。通过使用Hopcroft-Karp算法实现高效的二分匹配,确保在限定时间内尽可能多的人能够找到遮蔽。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Rain on your Parade
Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 655350/165535 K (Java/Others)
Total Submission(s): 3963    Accepted Submission(s): 1320


Problem Description
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



Source
HDU 2008-10 Public Contest 

裸二分匹配
但是数据量很大,用匈牙利O(VE)的复杂度,肯定会超时
Hopcroft-Karp复杂度为O(sqrt(V)*E),妥妥的


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
#include<queue>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

const int NX = 3005;
const int NY = NX;

struct Edge{
    int to,next;
}edge[NX*NY];
int head[NX];
inline void addEdge(int k,int u,int v){
    edge[k].to=v;
    edge[k].next=head[u];
    head[u]=k;
}

int nx,ny;//左右集合顶点数
int cx[NX],cy[NY];//x/y配对到的点的标号 标号:x:1-nx y:1-ny
int disX[NX],disY[NY];//顶点距离标号
int dis;//可匹配点距离
bool bmask[NY];//寻找增广路时的标志数组

bool searchPath(){//是否存在增广路
    queue<int>que;
    dis=INF;
    fill(disX,disX+nx+1,-1);
    fill(disY,disY+ny+1,-1);
    for(int i=1;i<=nx;++i){
        if(cx[i]==-1){
            que.push(i);
            disX[i]=0;
        }
    }
    while(!que.empty()){
        int u=que.front();
        que.pop();
        if(disX[u]>dis){
            break;
        }
        for(int i=head[u];i!=-1;i=edge[i].next){
            int v=edge[i].to;
            if(disY[v]==-1){
                disY[v]=disX[u]+1;
                if(cy[v]==-1){
                    dis=disY[v];
                }
                else{
                    disX[cy[v]]=disY[v]+1;
                    que.push(cy[v]);
                }
            }
        }
    }
    return INF!=dis;
}

int findPath(int u){
    for(int i=head[u];i!=-1;i=edge[i].next){
        int v=edge[i].to;
        if(!bmask[v]&&disY[v]==disX[u]+1){
            bmask[v]=1;
            if(cy[v]!=-1&&disY[v]==dis){//disY[v]==dis 但是v已经被匹配了
                continue;
            }
            if(cy[v]==-1||findPath(cy[v])){//v未被匹配 或者 v被匹配 但是匹配v的点 又找到新的匹配点
                cy[v]=u;
                cx[u]=v;
                return 1;
            }
        }
    }
    return 0;
}

int maxMatch(){
    int res=0;
    fill(cx,cx+nx+1,-1);
    fill(cy,cy+ny+1,-1);
    while(searchPath()){
        fill(bmask,bmask+ny+1,false);
        for(int i=1;i<=nx;++i){
            if(cx[i]==-1){
                res+=findPath(i);
            }
        }
    }
    return res;
}

int x[NX],y[NX],s[NX];
int x_[NY],y_[NY];

int distance(int x1,int y1,int x2,int y2){
    return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
}

int slove(){
    int t;
    scanf("%d",&t);
    scanf("%d",&nx);
    fill(head,head+nx+1,-1);
    for(int i=1;i<=nx;++i){
        scanf("%d%d%d",x+i,y+i,s+i);
    }
    scanf("%d",&ny);
    int numE=0;
    for(int i=1;i<=ny;++i){
        scanf("%d%d",x_+i,y_+i);
        for(int j=1;j<=nx;++j){
            int dis=distance(x_[i],y_[i],x[j],y[j]);
            if(t*t*s[j]*s[j]>=dis){
                addEdge(numE++,j,i);
            }
        }
    }
    return maxMatch();
}

int main()
{
    //freopen("/home/lu/Documents/r.txt","r",stdin);
    //freopen("/home/lu/Documents/w.txt","w",stdout);
    int T;
    scanf("%d",&T);
    for(int t=1;t<=T;++t){
        int ans=slove();
        printf("Scenario #%d:\n%d\n\n",t,ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值