一、内容
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
二、思路
- O(NM)匈牙利会超时,故使用HK即可。
三 、代码
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
const int N = 3e3 + 5, M = N * N, INF = 0x3f3f3f3f;
struct E {
int v, next;
} e[M];
struct Node {
double x, y, s;
} a[N], b[N];
int n, m, k, t, len, dis, h[N], mx[N], my[N], dx[N], dy[N];
bool vis[N];
void add(int u, int v) {
e[++len].v = v;
e[len].next = h[u];
h[u] = len;
}
bool bfs() {
queue<int> q;
memset(dx, -1, sizeof(dx));
memset(dy, -1, sizeof(dy));
//找出未匹配的点 (左边)
for (int i = 1; i <= n; i++) {
if (mx[i] == -1) q.push(i), dx[i] = 0;
}
dis = INF; //初始令增广路为无穷
while (!q.empty()) {
int u = q.front(); q.pop();
if (dx[u] > dis) break; //代表还有比dis更长的增广路 留待下次bfs扩展
for (int j = h[u]; j; j = e[j].next) {
int v = e[j].v;
if (dy[v] == -1) { //代表还未遍历过
dy[v] = dx[u] + 1; //给予层次
if (my[v] == -1) {
dis = dy[v]; //找到增广路
} else {
//如果已经匹配了 就把匹配好的点(左边)放入队列进行扩展
dx[my[v]] = dy[v] + 1;
q.push(my[v]);
}
}
}
}
return dis != INF; //等于INF就未找到增广路
}
bool dfs(int u) {
for (int j = h[u]; j; j = e[j].next) {
int v = e[j].v;
if (vis[v] || dy[v] != dx[u] + 1) continue; //如果未访问且是u的下层次点
vis[v] = true;
if (my[v] != -1 && dy[v] == dis) continue; //如果这个点已经匹配 且找到的增广路已经大于dis
if (my[v] == -1 || dfs(my[v])) {
my[v] = u; mx[u] = v;
return true;
}
}
return false;
}
int getMax() {
int ans = 0;
memset(mx, -1, sizeof(mx));
memset(my, -1, sizeof(my));
while (bfs()) { //查找是否有增广路
memset(vis, false, sizeof(vis)); //对于找到的一条增广路径查询
for (int i = 1; i <= n; i++) {
if (mx[i] == -1 && dfs(i)) ans++; //如果未匹配才去dfs
}
}
return ans;
}
int main() {
scanf("%d", &t);
for (int T = 1; T <= t; T++) {
memset(h, 0, sizeof(h)); len = 0;
scanf("%d%d", &k, &n);
for (int i = 1; i <= n; i++) {
scanf("%lf%lf%lf", &a[i].x, &a[i].y, &a[i].s);
}
scanf("%d", &m);
double x, y;
for (int i = 1; i <= m; i++) {
scanf("%lf%lf", &b[i].x, &b[i].y); //建立二分图
}
for (int i = 1; i <= m; i++) {
x = b[i].x, y = b[i].y;
for (int j = 1; j <= n; j++) {
double fx = a[j].x - x;
double fy = a[j].y - y;
double d = sqrt(fx*fx + fy*fy);
if (d <= k * a[j].s) add(j, i);
}
}
int ans = getMax();
printf("Scenario #%d:\n%d\n\n", T, ans);
}
return 0;
}