当前位置: 代码迷 >> 综合 >> HDU 2389 Rain on your Parade(二分匹配+Hopcroft-Carp算法模板题)
  详细解决方案

HDU 2389 Rain on your Parade(二分匹配+Hopcroft-Carp算法模板题)

热度:44   发布时间:2023-11-17 14:27:57.0

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:
2Scenario #2:
2

题解:

题意:

有一堆人在开party,然后t时间之后会下雨,有n个人,给出每个人的坐标和速度,然后又m把伞,给出伞坐标,问在下雨之前最多有几人能拿到伞(一人一把)

思路:

人匹配伞,一开始就用匈牙利算法搞了一遍,匈牙利算法时间复杂度是点数n乘上边数m,点有3000个,边最多有3000x3000个,乘起来毫无疑问会TLE。。。然后我就TLE了,搜了下题解原来是用HK算法搞,然后我就照着别人的模板根据自己的理解打了一遍。。我发现了如果这题用int处理而不是开根号算距离,用的时间是原来的1/4左右。。。学到了,还有就是静态数组处理会比动态处理快一些,这里用的是静态的邻接表写法的HK算法模板题

然后我看的大佬这题的博客:http://www.cnblogs.com/kuangbin/p/3224091.html

然后赋上他的HK算法邻接矩阵的模板:http://www.cnblogs.com/kuangbin/archive/2011/08/12/2135898.html

他的动态写法用时400多ms,我的静态的用时200多ms。。算是改进吧

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<deque>
#define M (t[k].l+t[k].r)/2
#define lson k*2
#define rson k*2+1
#define ll long long
#define INF 100861111;
using namespace std;
const int maxn=3005;
struct peo//存人的情况
{int x,y;int speed;
}a[maxn];
struct umb//存伞的情况
{int x,y;
}b[maxn];
int getdis(peo a,umb b)//获得距离的平方
{return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int p[maxn][maxn];//存第i个人可以取哪几把伞
int num[maxn];//存第i个人可以取多少伞
int usedx[maxn];//下面的都是用于HK算法的。。目前只写了一题还不太理解作用,照打了一遍
int usedy[maxn];
int dx[maxn],dy[maxn];
int vis[maxn],n,m;
int dis;//我也不知道有什么用,大佬也没讲
int find(int u)//相当于匈牙利算法的改进
{int i;for(i=0;i<num[u];i++){int v=p[u][i];if(!vis[v]&&dy[v]==dx[u]+1){vis[v]=1;if(usedy[v]!=-1&&dy[v]==dis)continue;if(usedy[v]==-1||find(usedy[v])){usedy[v]=u;usedx[u]=v;return 1;}}}return 0;
}
int searchp()//不知道怎么用,套模板。。
{queue<int>q;dis=INF;memset(dx,-1,sizeof(dx));memset(dy,-1,sizeof(dy));for(int i=1;i<=n;i++){if(usedx[i]==-1){q.push(i);dx[i]=0;}}while(!q.empty()){int u=q.front();q.pop();if(dx[u]>dis)break;for(int i=0;i<num[u];i++){int v=p[u][i];if(dy[v]==-1){dy[v]=dx[u]+1;if(usedy[v]==-1)dis=dy[v];else{dx[usedy[v]]=dy[v]+1;q.push(usedy[v]);}}}}return dis!=INF;
}
int hk()//hk算法
{int res=0,i;memset(usedx,-1,sizeof(usedx));memset(usedy,-1,sizeof(usedy));while(searchp()){memset(vis,0,sizeof(vis));for(i=1;i<=n;i++){if(usedx[i]==-1&&find(i))res++;}}return res;
}
int main()
{int i,j,test,cas;int t;scanf("%d",&test);for(cas=1;cas<=test;cas++){scanf("%d",&t);scanf("%d",&n);for(i=1;i<=n;i++)scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].speed);scanf("%d",&m);for(i=1;i<=m;i++)scanf("%d%d",&b[i].x,&b[i].y);memset(num,0,sizeof(num));for(i=1;i<=n;i++){for(j=1;j<=m;j++){if(getdis(a[i],b[j])<=a[i].speed*a[i].speed*t*t)//这里用int处理很快{p[i][num[i]]=j;num[i]++;}}}printf("Scenario #%d:\n",cas);printf("%d\n\n",hk());}return 0;
}