Rain on your Parade

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

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

题意:

给出客人和雨伞的二维坐标,知道几分钟后下雨以及客人的移动速度,问可以拿到雨伞最多有多少人。

题解:

将客人与其可以到达的雨伞连边,进行二分图的最大匹配即可。

但这题比较坑的地方就是数据量较大,匈牙利算法会超时(好像很少有题会卡匈牙利算法........),所以就应该用匈牙利算法的优化版:HK算法。

HK算法的思想就是先通过bfs预处理出最小增光路集,然后dfs增广的时候就把这些增广路集一并增广。

具体的算法分析可以看看这个:http://files.cnblogs.com/files/liuxin1.pdf

直接上代码~~

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <cmath>
  6. #include <queue>
  7. using namespace std;
  8. const int N = ;
  9. int T,t,n,m,ans,lim,cnt=;
  10. int d[N][N],link[N][N],match[N],match2[N],check[N],disx[N],disy[N];
  11. struct guests{
  12. int x,y,v;
  13. }g[N];
  14. inline void init(){
  15. memset(link,,sizeof(link));memset(match,-,sizeof(match));
  16. ans=;cnt++;memset(check,,sizeof(check));memset(match2,-,sizeof(match2));
  17. }
  18. inline int dfs(int x){
  19. for(int i=;i<=n;i++){
  20. if(disy[i]==disx[x]+ && !check[i] &&link[x][i]){
  21. check[i]=;
  22. if(match[i]!=- && disy[i]==lim) continue ;//此时增广路会大于lim
  23. if(match[i]==- || dfs(match[i])){
  24. match[i]=x;
  25. match2[x]=i;
  26. return ;
  27. }
  28. }
  29. }
  30. return ;
  31. }
  32. inline bool bfs(){
  33. queue<int> q;
  34. memset(disx,-,sizeof(disx));
  35. memset(disy,-,sizeof(disy));lim = (<<);
  36. for(int i=;i<=m;i++) if(match2[i]==-){
  37. q.push(i);disx[i]=;
  38. }
  39. while(!q.empty()){
  40. int u=q.front();q.pop();
  41. if(disx[u]>lim) break ; //条件成立,所求增广路必然比当前的增广路长度长
  42. for(int i=;i<=n;i++){
  43. if(link[u][i] && disy[i]==-){
  44. disy[i]=disx[u]+;
  45. if(match[i]==-) lim=disy[i];//找到增广路,记录长度
  46. else{
  47. disx[match[i]]=disy[i]+;
  48. q.push(match[i]);//入队,寻找更长的增广路
  49. }
  50. }
  51. }
  52. }
  53. return lim!=(<<) ;
  54. }
  55. int main(){
  56. scanf("%d",&T);
  57. while(T--){
  58. init();
  59. scanf("%d%d",&t,&m);
  60. for(int i=;i<=m;i++){
  61. scanf("%d%d%d",&g[i].x,&g[i].y,&g[i].v);
  62. }
  63. scanf("%d",&n);
  64. for(int i=,x,y;i<=n;i++){
  65. scanf("%d%d",&x,&y);
  66. for(int j=;j<=m;j++){
  67. d[j][i]=abs(g[j].x-x)+abs(g[j].y-y);
  68. if(d[j][i]<=t*g[j].v) link[j][i]=;
  69. }
  70. }
  71. while(bfs()){
  72. memset(check,,sizeof(check));
  73. for(int i=;i<=m;i++){
  74. if(dfs(i)) ans++;
  75. }
  76. }
  77. printf("Scenario #%d:\n%d\n\n",cnt,ans);
  78. }
  79.  
  80. return ;
  81. }

HDU2389:Rain on your Parade(二分图最大匹配+HK算法)的更多相关文章

  1. HDU2389 Rain on your Parade —— 二分图最大匹配 HK算法

    题目链接:https://vjudge.net/problem/HDU-2389 Rain on your Parade Time Limit: 6000/3000 MS (Java/Others)  ...

  2. hdu2389 Rain on your Parade 二分图匹配--HK算法

    You’re giving a party in the garden of your villa by the sea. The party is a huge success, and every ...

  3. hdu-2389.rain on your parade(二分匹配HK算法)

    Rain on your Parade Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 655350/165535 K (Java/Ot ...

  4. SPOJ 4206 Fast Maximum Matching (二分图最大匹配 Hopcroft-Carp 算法 模板)

    题目大意: 有n1头公牛和n2头母牛,给出公母之间的m对配对关系,求最大匹配数.数据范围:  1 <= n1, n2 <= 50000, m <= 150000 算法讨论: 第一反应 ...

  5. Hdu2389 Rain on your Parade (HK二分图最大匹配)

    Rain on your Parade Problem Description You’re giving a party in the garden of your villa by the sea ...

  6. Hdu 3289 Rain on your Parade (二分图匹配 Hopcroft-Karp)

    题目链接: Hdu 3289 Rain on your Parade 题目描述: 有n个客人,m把雨伞,在t秒之后将会下雨,给出每个客人的坐标和每秒行走的距离,以及雨伞的位置,问t秒后最多有几个客人可 ...

  7. UESTC 919 SOUND OF DESTINY --二分图最大匹配+匈牙利算法

    二分图最大匹配的匈牙利算法模板题. 由题目易知,需求二分图的最大匹配数,采取匈牙利算法,并采用邻接表来存储边,用邻接矩阵会超时,因为邻接表复杂度O(nm),而邻接矩阵最坏情况下复杂度可达O(n^3). ...

  8. HDU 1045 - Fire Net - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 Time Limit: 2000/1000 MS (Java/Others) Mem ...

  9. 51Nod 2006 飞行员配对(二分图最大匹配)-匈牙利算法

    2006 飞行员配对(二分图最大匹配) 题目来源: 网络流24题 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 第二次世界大战时期,英国皇家空军从沦陷国 ...

随机推荐

  1. linux网络服务实验

    1.设置window IP地址为192.168.3.XX,掩码24位. 2.设置Linux IP地址为192.168.3.YY,掩码24位.window与Linux互相ping通. 3.在linux中 ...

  2. MySQL分区的限制(最多有多少个分区)

    MySQL分区的限制 •   只能对数据表的整型列进行分区,或者数据列可以通过分区函数转化成整型列 •   最大分区数目不能超过1024 •   如果含有唯一索引或者主键,则分区列必须包含在所有的唯一 ...

  3. OrCAD创建原理图符号图

    1. 首先创建一个库 2. 右键新创建的库,添加新的器件New Part 3. 修改器件属性 4. 添加引脚 添加完引脚之后如图,其中双击引脚,即可修改引脚名字和序号 5. 添加符号的外形 添加完外形 ...

  4. CSS流布局权威指南

    http://www.cnblogs.com/qieguo/p/5421252.html

  5. AV Foundation 实现文字转语音

    AV Foundation 主要框架 CoreAudio 音频处理框架 扩展学习:<Learning CoreAudio> CoreVideo 视频处理的管道模式,逐帧访问 CoreMed ...

  6. 洛谷P2346四子连棋

    题目描述 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步. 黑白双方交替走棋,任意一方可 ...

  7. 图的同构 (Graph Isomorphism)

    整理摘自:https://www.jianshu.com/p/c33b5d1b4cd9 同构是在数学对象之间定义的一类映射,它能揭示出在这些对象的属性或者操作之间存在的关系.若这两个数学结构之间存在同 ...

  8. RDL/RDLC批量单据打印

    使用RDL或RDLC进行单据打印时,单张单据打印比较直观简单,无需说明.下面我们来谈一下批量单据打印的实现方法.以下以RDL的ReportBuilder设计环境为例进行讲解,RDLC.VS设计环境同理 ...

  9. 数据结构11——KMP

    一.博客导航 KMP算法 扩展KMP算法

  10. postgres(pl/pgsql)

    复制后期看 https://www.cnblogs.com/stephen-liu74/archive/2012/06/06/2312759.html https://www.cnblogs.com/ ...