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
    评论
### 回答1: #include <stdio.h> int main() { int n, i; printf("输入一个正整数: "); scanf("%d",&n); printf("分解质因数: "); for(i=2; i<=n; i++) { while(n%i == 0) { printf("%d ",i); n=n/i; } } printf("\n"); return 0; } ### 回答2: 要实现一个运行速度最的分解质因数算法,可以使用Pollard Rho算法。这是一个基于随机性的质因数分解算法,其主要思想是通过不断迭代生成一个序列,其中的元素可能是合数,然后使用辗转相除法来判断是否找到一个质因数。 下面是使用C代码实现的简化版本: ``` #include <stdio.h> long long gcd(long long a, long long b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } long long pollardRho(long long n) { long long x = 2, y = 2, d = 1; while (d == 1) { x = (x * x + 1) % n; y = (y * y + 1) % n; y = (y * y + 1) % n; d = gcd((x - y + n) % n, n); } return d; } int main() { long long n; printf("请输入一个正整数:"); scanf("%lld", &n); printf("%lld的一个质因数是:%lld\n", n, pollardRho(n)); return 0; } ``` 该代码通过求解随机迭代序列的非平凡最大公约数来找到一个质因数。与传统的试除法相比,Pollard Rho算法在较大的数上运行速度更。在实际使用时,可以进一步改进代码以提高效率,并处理特殊情况。 ### 回答3: 要编写一个运行速度最的分解质因数算法,可以采用试除法的优化形式,又称为Pollard's rho算法。 首先,我们需要写一个用于检测一个数是否为素数的函数。可以使用试除法,从2到sqrt(n)逐个检查是否有因数。如果找到一个因数,说明n不是素数;如果没有找到因数,说明n是素数。 接下来,我们可以使用Pollard's rho算法来分解质因数。这个算法的思想是利用了环的周期性来找到质因数。 具体步骤如下: 1. 定义一个函数f(x),表示f(x) = x^2 + 1。 2. 初始化x = 2和y = 2,计算f(x)和f(f(y))。 3. 利用欧几里得算法计算f(x)和f(f(y))的差值d。 4. 如果d是一个质数,则d即为n的一个质因数。 5. 如果d是合数,重复步骤2-4直到找到一个质因数。 通过不断重复步骤2-4,我们可以找到n的一个质因数。然后可以递归地对这个质因数进行分解,直到无法再分解为止。 通过这种方式,可以得到一个速的质因数分解算法。当然,实现中还可以对代码进行优化,例如可以用位运算代替乘法和除法操作,以提高运行速度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值