hdu2389二分图之Hopcroft Karp算法

时间:2021-05-03 20:07:39
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.

InputThe 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. 
OutputFor 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
题意:给n个人坐标速度和m个伞坐标进行匹配,转化成二分图匹配
题解:二遍循环判断是否能到达,进行匹配,匈牙利算法会tle,用Hopcroft Karp算法(暂时还不是很理解,先背下来)
参考的博客,图很清楚但是注释有点少http://www.cnblogs.com/penseur/archive/2013/06/16/3138981.html
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007 using namespace std; const int N=+,maxn=+,inf=0x3f3f3f3f; struct edge{
int x,y,v;
}g[N],u[N]; int n,m,dis;
bool used[N],ok[N][N];
int mx[N],my[N];//mx保存右侧匹配点,my保存左侧匹配点
int dx[N],dy[N]; double road(edge a,edge b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool searchP()//寻找增广路径集
{
queue<int>q;
dis=inf;
memset(dy,-,sizeof dy);
memset(dx,-,sizeof dx);
for(int i=;i<=n;i++)
if(mx[i]==-)//将未访问过的左侧点加入队列
{
q.push(i);
dx[i]=;//距离设为0
}
while(!q.empty()){
int u=q.front();
q.pop();
if(dx[u]>dis)break;
for(int i=;i<=m;i++)//取左侧点匹配到右侧
{
if(ok[u][i]&&dy[i]==-)//右侧点联通且未访问
{
dy[i]=dx[u]+;//i对应距离为u对应距离+1
if(my[i]==-)dis=dy[i];//i无匹配点
else
{
dx[my[i]]=dy[i]+;
q.push(my[i]);
}
}
}
}
return dis!=inf;
}
bool dfs(int x)
{
for(int i=;i<=m;i++)
{
if(!used[i]&&ok[x][i]&&dy[i]==dx[x]+)//没有访问过且距上一点为1
{
used[i]=;
if(my[i]!=-&&dy[i]==dis)continue;
if(my[i]==-||dfs(my[i]))
{
my[i]=x;
mx[x]=i;
return ;
}
}
}
return ;
}
int solve()
{
int ans=;
memset(mx,-,sizeof mx);
memset(my,-,sizeof my);
while(searchP()){
memset(used,,sizeof used);
for(int i=;i<=n;i++)
if(mx[i]==-&&dfs(i))
ans++;
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int t,time;
cin>>t;
for(int k=;k<=t;k++)
{
cin>>time>>n;
for(int i=;i<=n;i++)cin>>g[i].x>>g[i].y>>g[i].v;
cin>>m;
for(int i=;i<=m;i++)cin>>u[i].x>>u[i].y;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(time*g[i].v>=road(g[i],u[j]))ok[i][j]=;
else ok[i][j]=;
}
}
cout<<"Scenario #"<<k<<":"<<endl<<solve()<<endl<<endl;
}
return ;
}