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 <= 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
此算法相对于匈牙利算更加高效,原因是,此算法通过BFS更新+DFS可以同时更新许多条网络(类似于最大流中的Dinic算法),而匈牙利算法只能一次更新一条路径
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
const int maxn=3005;
int dx[maxn],dy[maxn];//保存距离(类似于分层图)
int mx[maxn],my[maxn];//保存匹配值
vector<int> G[maxn];
int n,m,speed[maxn];;
struct node
{
int x,y;
} um[maxn],guest[maxn];
double dist(node a,node b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool bfs()
{
memset(dx,0,sizeof dx);
memset(dy,0,sizeof dy);
queue<int> q;
for(int i=1; i<=n; i++)
if(mx[i]==-1) q.push(i);//将未匹配的X集合中的加入队列
bool flag=false;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=0; i<G[u].size(); i++)
{
int v=G[u][i];
if(!dy[v])//y[i]为零表示为被访问过
{
dy[v]=dx[u]+1;
if(my[v]!=-1)//如果原先就有匹配值,则将原先的值加入到队列中重新匹配
{
dx[my[v]]=dy[v]+1;
q.push(my[v]);
}
else flag=true;
}
}
}
return flag;
}
bool dfs(int u)//寻找增广路径
{
for(int i=0; i<G[u].size(); i++)
{
int v=G[u][i];
if(dy[v]==dx[u]+1)//不为零表示未被访问过,仅当其为紧接着的下一层时,才进行访问
{
dy[v]=0;
if(my[v]==-1||dfs(my[v]))//满足匹配条件
{
my[v]=u;
mx[u]=v;
return true;
}
}
}
return false;
}
int Hopcroft_Karp()
{
memset(mx,-1,sizeof mx);
memset(my,-1,sizeof my);
int ans=0;
while(bfs())//不断BFS更新残余网络
{
for(int i=1; i<=n; i++)
if(mx[i]==-1&&dfs(i)) ans++;
}
return ans;
}
int main()
{
int Case;
scanf("%d",&Case);
for(int ase=1; ase<=Case; ase++)
{
int time;
scanf("%d%d",&time,&n);
for(int i=1; i<=n; i++)
scanf("%d%d%d",&guest[i].x,&guest[i].y,&speed[i]);
scanf("%d",&m);
for(int i=1; i<=m; i++)
scanf("%d%d",&um[i].x,&um[i].y);
for(int i=1; i<=n; i++)
G[i].clear();
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
if(dist(guest[i],um[j])<=(double)time*speed[i])//添加边的条件
G[i].push_back(j);
printf("Scenario #%d:\n%d\n\n",ase,Hopcroft_Karp());
}
return 0;
}