第一次写HK, 这个算法的原理和dinic比较类似,都是先bfs求出层次图,然后在层次图上进行dfs搜索增广路。

这个算法要注意的几点

1. 每次构建层次图时,最大层(dis)为最近的Y中的未覆盖的点

2. 每次在层次图中找的时候最多找到dis(最大层).

听说可以证明到这个算法的复杂度 为 sqrt(n)*m 。 我没有去证

但是对比dinic ,这种算法应该都是会提速的,将无规律的搜索变成有规律的

Rain on your Parade

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 655350/165535 K (Java/Others) Total Submission(s): 2046    Accepted Submission(s): 624

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
Recommend
lcy
// 对于这种题,用邻接矩阵会更好些吧
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;#define N 3033
#define INF 0x3ffffffbool g[N][N];
int gx[N],gy[N],spd[N];
int t;
int n,m;
int mark[N],frt[N],frt1[N];
int lvx[N],lvy[N];
int que[10*N];
int flag;/*double dis(double x,double y,double x1,double y1)  有sqrt就会导致不精确
{return sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1));
}*//*void add_edge(int u,int v)
{edge[cnt].to=v;edge[cnt].next=pre[u];pre[u]=cnt++;
}*/int bfs()
{memset(lvx,-1,sizeof(lvx));memset(lvy,-1,sizeof(lvy));int qf=0,qd=0;for(int i=1;i<=n;i++){if( frt1[i] == -1 ){lvx[i]=0;que[qf++]=i;}}int v;flag = INF;while( qf>qd ){int cur=que[qd++];if( lvx[cur] > flag ) break;for(int i=1;i<=m;i++){v=i;if( lvy[v] != -1 || g[cur][v]==0) continue;lvy[v] = lvx[cur]+1;if( frt[v]==-1 )  // 如果==-1 的时候
            {flag=lvy[v];}else{//if(lvx[ frt[v] ] != -1) continue; // 据bfs的性质,每个点只能访问一次lvx[ frt[v] ] = lvy[v]+1;que[ qf++ ] = frt[v];}}}return flag!=INF;
}int dfs(int s)
{int v;for(int i=1;i<=m;i++){v=i;if(mark[v]==1 || lvx[s]+1 != lvy[v] || g[s][v]==0 ) continue;// 一个程序wa了绝对是有错误。mark[v] = 1;if(frt[v] != -1 && lvy[v] == flag) continue; // 加了这个就不超时了?!!!,这个是必须用的if( frt[v] == -1 || dfs(frt[v]) ){frt[v]=s;frt1[s]=v;return 1;}}return 0;
}int main()
{int tt=1;int T;scanf("%d",&T);while(T--){memset(g,0,sizeof(g));scanf("%d",&t);scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d%d%d",&gx[i],&gy[i],&spd[i]);}scanf("%d",&m);for(int i=1;i<=m;i++){int x,y;scanf("%d%d",&x,&y);for(int j=1;j<=n;j++){if( ((gx[j]-x)*(gx[j]-x)+(gy[j]-y)*(gy[j]-y)) <= t*t*spd[j]*spd[j]){g[j][i]=1;}}}int sum=0;memset(frt,-1,sizeof(frt));memset(frt1,-1,sizeof(frt1));while( bfs() ) // 先对当前网络分层,然后在dfs搜索增广路径,类似于dinic,只在层次网络上搜索,路径很少可以减少时间复杂度
        {for(int i=1;i<=n;i++){memset(mark,0,sizeof(mark));if(frt1[i] == -1) // 如果sum += dfs(i);}}printf("Scenario #%d:\n",tt++);printf("%d\n",sum);printf("\n");}return 0;
}

转载于:https://www.cnblogs.com/chenhuan001/archive/2013/04/25/3043742.html

HDU 2389(HK 最大二分匹配)相关推荐

  1. HDU 2389(二分最大匹配优化算法,Hopcroft-Carp)

    HDU 2389(二分最大匹配优化算法,Hopcroft-Carp) 题目链接: 大致题意; 您能帮助客人在下雨之前尽可能多地找到一把雨伞? 给定所有客人的位置和跑步速度,雨伞位置,到下雨开始时的时间 ...

  2. hdu 1054 Strategic Game 最小点覆盖 = 最大二分匹配

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1054 简单二分匹配,根据题意构造一个无向图.然后求最小点覆盖,然后扫描mark数组将曾经匹配的点所匹配 ...

  3. 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  4. hdu 1281棋盘游戏(二分匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281 Problem Description 小希和Gardon在玩一个游戏:对一个N*M的棋盘,在格 ...

  5. HDU - 3081 Marriage Match II 【二分匹配】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3081 题意 有n对男女 女生去选男朋友 如果女生从来没和那个男生吵架 那么那个男生就可以当她男朋友 女 ...

  6. hdu 5093 二分匹配

    /* 题意:给你一些冰岛.公共海域和浮冰,冰岛可以隔开两个公共海域,浮冰无影响 求选尽可能多的选一些公共海域点每行每列仅能选一个. 限制条件:冰山可以隔开这个限制条件.即*#*可以选两个 预处理: * ...

  7. HDU 4685 Prince and Princess(二分匹配加点建图+强连通分量)

    题目链接 Problem Description There are n princes and m princesses. Princess can marry any prince. But pr ...

  8. HDU 5943 2016CCPC杭州 K: Kingdom of Obsession(二分匹配)

    题意:给你一张二分图,左边是s+1到s+n这n个数,右边是1到n这n个数 如果x在左边,y在右边,且x%y==0,那么x可以和y匹配,问这个二分图是否存在完美匹配 如果左边有两个以上的质数出现,那么一 ...

  9. HDU 6178 Monkeys(树上的二分匹配+fread)

    题目地址 题意:你有n个节点,n-1条边构成一棵树,有m个猴子,猴子要在节点上,并且每个猴子至少要和另一只猴子有边相连,求最多需要多少条边 思路:最好的应该是猴子都是两两匹配的,因为每2个猴子相连的话 ...

  10. hdu 1045Fire Net (建图 、二分匹配)

    题意:需要求最大的放置炮台数(每个炮台都不能在其他炮台攻击范围内). 炮台攻击范围是:该炮台所在的行和列,但是'X'可以阻挡炮台攻击. 思路:二分匹配必须要有两个互不关联的集合,所以可以将行和列看做两 ...

最新文章

  1. 资料分享:送你一本《机器学习实战》电子书!
  2. 简单介绍C语言使用四种方法初始化结构体
  3. 图灵五周年真情回馈广大读者,多种买赠活动等着您
  4. 【算法学习】枚举与剪枝(一)
  5. 于变局中开新局!《2021中国SaaS市场研究报告》报告发布
  6. leetcode 11容纳最多水
  7. 研发团队绩效_如何在团队内部建立网络绩效文化
  8. 关于JavaScript中cookie的用法的例子
  9. 用python海龟画图_天呐!python 的乌龟绘图怎么用啊!?
  10. JavaScript:加载请求本地资源工具StaticResourceUtil.js
  11. springboot的war部署到tomcat正常启动,访问404问题
  12. PPT高级教程及技巧 .
  13. 自盲化能力 Paillier和EIGamal
  14. halcon测试篇:求两条线之间的交点
  15. IGV web 工具部署
  16. 英语听说计算机查分,提醒:今日英语听说考成绩查询,你准备好了吗?
  17. 网络安全——攻防对抗
  18. C++核心准则ES.56​:只在需要将一个对象显式移动到另外的作用域时使用std::move​
  19. 浅谈Session并且实现购物车
  20. Spring框架快速入门(Spring超全面讲解)

热门文章

  1. 洛谷 P1881 绳子对折
  2. linux 重定向命令
  3. Ubuntu下wxWidgets学生公寓管理编程,sqlite3的用法(mysql数据库),窗体,下面是部分添加和删除功能,其他功能可以联系我。。...
  4. javascript面向对象技术基础
  5. 如何 珍惜自己和珍重别人。珍惜一切
  6. 4、线程--线程同部
  7. 扎克伯格为女儿选的量子物理学童书 你看得懂不?
  8. 解决chrome崩溃的方法
  9. Head First 设计模式目录
  10. ubuntu openStack icehouse dashboard theme自定义