23 HK 最快的最大匹配算法;一个比匈牙利匹配算法快的算法;o(sqrt(n)*E);

F - Rain on your Parade

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 <= s i <= 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

HK算法分为三步 :假设我们将集合分为两个,一个是左边的x集合,右面的y集合;

1) :首先我们x集合中没有匹配的元素入队列;

2) :我们以队列中的每一个元素作为起始点然后向他们相邻的点进行BFS就是在寻找增广路或者说是将其他的点根据我们的起始点来分层,如果与队列中的元素(x集合)相连的元素(y集合)没有匹配的话我们就找到了一条增广路(就是一个可能的匹配)

3)如果这个元素与x集合中的元素匹配了那么我么就要将x集合中的元素入队列;

这里有四个数组:

linkx[]:表示的是左面x集合中的元素和谁相连;

linky[]:表示的是右面y集合中的元素和谁相连;

dx[]   : 表示在分层的过程中x集合中元素所在的层数

dy[]   :表示在分层的过程中y集合中的元素所在的层数;

 假设我们有 1--2  1--3 1--4  2--5  2--6 3--5这六条边那么我们的HK算法过程是这样

集合   x : 1    2     3

集合   y :  2   3    4    5   6  

1)在入队了的时候是  1   2    3  

2)  u=1    to:  2  3  4  因为y集合中的2,3,4在x集中没有匹配的元素所以找到了一条增广路

                           dy :  1  1  1 

     u=2         to :        5         6                  同上;

                            dy:        1         1    

             u=3        to :         5

                           dy:         1  //                         这里的深度是在2的时候就更新好了

3)

最后就是朴素的匈牙利匹配算法了:首先1-2成功,2匹配5成功,3匹配5的时候不成功,但是我们我们可以将集合y中5的x集合中

的匹配元素2在找一个匹配正好找到y集合中的6,所以问题中的最大匹配是3

这一到题目就是有n个人m个伞,伞和人有一定的距离人跑步有一定的速度,距离下雨还t分钟,我们要让尽可能多的人不淋雨,最多可以由多少个人不淋雨;

#include <bits/stdc++.h>
using namespace std;
const int Max  = 3e3+10;
typedef long long ll;
#define rep(i,s,n) for(int i=s;i<=n;i++)
#define per(i,n,s) for(int i=n;i>=s;i--)
vector <int> ma[Max];
struct node {
    ll x,y,s;
}p[Max],p1[Max];
int n,m,linkx[Max],linky[Max],dx[Max],dy[Max];
/*linkx[]:表示的是左面x集合中的元素和谁相连;

linky[]:表示的是右面y集合中的元素和谁相连;

dx[]   : 表示在分层的过程中x集合中元素所在的层数

dy[]   :表示在分层的过程中y集合中的元素所在的层数;
*/
int DFS(int u){
     int len=ma[u].size();
     rep(i,0,len-1){
        int to=ma[u][i];
        if(dy[to]==dx[u]+1){
            dy[to]=0;
            if(!linky[to]||DFS(linky[to])){
                linkx[linky[to]=u]=to;
                return 1;
            }
        }
     }
     return 0;
}
int q[Max*Max];
bool BFS(){//重新分层寻找增广路
  memset(dx,0,sizeof(dx));
  memset(dy,0,sizeof(dy));
  int front1=0,rearl=0;
  rep(i,1,n)  if(!linkx[i]) q[rearl++]=i;
  bool f=0;
  while(front1<rearl){
    int u=q[front1++];
    int len=ma[u].size();
    rep(i,0,len-1){
       int to=ma[u][i];
       if(!dy[to]){//如果当前y集合中的这个元素还没有分层的话我们就将其分层;
        dy[to]=dx[u]+1;
        if(!linky[to])  f=1;//如果集合y中的这一个元素在x集合中没有匹配元素的话我们就找到了增广路
        else  dx[linky[to]]=dy[to]+1,q[rearl++]=linky[to];//如果匹配了的话我们要将x集合中的这个元素再入队列
       }
    }
  }
  return f;//如果有增广路说明还有匹配队,否则就没有了;
}
int main(){
 int ca=1;
 int T;
 scanf("%d",&T);
 while(T--){
    rep(i,0,3003){
    ma[i].clear();
    }
    ll t;
    scanf("%lld",&t);
    scanf("%d",&n);
    rep(i,1,n){
      scanf("%lld %lld %lld",&p[i].x,&p[i].y,&p[i].s);
    }
    scanf("%d",&m);
    rep(i,1,m){
      scanf("%lld %lld",&p1[i].x,&p1[i].y);
    }
    rep(i,1,n){
       rep(j,1,m){
           if((p[i].x-p1[j].x)*(p[i].x-p1[j].x)+(p[i].y-p1[j].y)*(p[i].y-p1[j].y)<=t*t*p[i].s*p[i].s){
               ma[i].push_back(j);
           }
       }
    }
    int num=0;
    memset(linkx,0,sizeof(linkx));
    memset(linky,0,sizeof(linky));
    while(BFS()){
        rep(i,1,n){
            if(DFS(i)){
                num++;
            }
        }
    }
    printf("Scenario #%d:\n%d\n\n",ca++,num);
 }
 return 0;
}
 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值