Rain on your Parade(二分图匹配-Hopcroft-Carp算法)

(代码里有很多注释)
匈牙利算法的复杂度是O(n*e),那么如果对于一个点和边比较多的图,匈牙利算法很容易超时,采用Hopcroft-Carp算法能够在 O(sqrt(n)*e)的复杂度内实现二分图匹配。
大致过程如下:

  1. 利用bfs找出是否存在增广路并保存路径
  2. dfs对找到的增广路进行匹配,由于之前已经保存了路径,所以找起来很快

Rain on your Parade(二分图匹配-Hopcroft-Carp算法)

judge:链接
Time limit:3000 ms
Memory limit:165535 kB
OS:Windows
Source:HDU 2008-10 Public Contest
参考自:链接

描述

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.

输入

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 &lt; = t &lt; = 5 ) (1 &lt;=t &lt;= 5) (1<=t<=5). The next line contains the number of guests m ( 1 &lt; = m &lt; = 3000 ) m (1 &lt;= m &lt;= 3000) m(1<=m<=3000), followed by m lines containing x x x- and y y y-coordinates as well as the speed s s si in units per minute ( 1 &lt; = s i &lt; = 3000 ) (1 &lt;= s i &lt;= 3000) (1<=si<=3000) of the guest as integers, separated by spaces. After the guests, a single line contains n ( 1 &lt; = n &lt; = 3000 ) n (1 &lt;= n &lt;= 3000) 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 10000 10000.

输出

For each test case, write a line containing “ S c e n a r i o   # i : Scenario\ \#i: 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.

样例输入

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

样例输出

Scenario #1:
2

Scenario #2:
2

题意

t分钟后会下雨,有m个宾客,宾客的速度是si,n把伞
要求输出下雨前能够得到伞的宾客的数量。

直接套H-C模板即可

代码

#include <bits/stdc++.h>
using namespace std;
#define _for(i, a) for(int i = 0; i < (a); i++)
#define _rep(i, a, b) for(int i = (a); i <= (b); i++)
#define maxn 3005
#define inf 0x3f3f3f3f
int Mx[maxn], My[maxn];//Mx存与左侧集合相连接的右边的点,My存与右边的
int dx[maxn], dy[maxn];/*
		存连接关系--即增广路的路径
		(dx的值为与之相连的上一个右边集合的点,dy同理)*/
int dis;//标识是否找到增广路及找到的增广路的深度
int un;
int used[maxn];//dfs中标识是否走过
vector<int> G[maxn];//邻接链表
struct poi {
	int x, y, v;
	void input1() { cin >> x >> y >> v; }
	void input2() { cin >> x >> y; }
}p1[maxn], p2[maxn];
int getdis(poi i, poi j) {
	return (i.x - j.x)*(i.x - j.x) + (i.y - j.y)*(i.y - j.y);
}
bool bfs() {
	queue<int> q;
	dis = inf;
	memset(dx, -1, sizeof(dx));
	memset(dy, -1, sizeof(dy));
	_for(i, un) {
		if (Mx[i] == -1) {//如果没有匹配过
			q.push(i);
			dx[i] = 0;
		}
	}
	while (q.size()) {
		int u = q.front();
		q.pop();
		if (dx[u] > dis) break;//如果已经找到增广路即退出
		int sz = G[u].size();
		_for(i, sz) {
			int v = G[u][i];
			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;
}//找出增广路并保存路径
bool dfs(int u) {
	int sz = G[u].size();
	_for(i, sz) {
		int v = G[u][i];
		if (!used[v] && dy[v] == dx[u] + 1) {//沿着路径走
			used[v] = 1;
			if (My[v] != -1 && dy[v] == dis) continue;/*
				如果当前节点已经匹配且为路径的末尾,
				则代表这并不是一个增广路,放弃这条路*/
			if (My[v] == -1 || dfs(My[v])) {//当前节点未匹配或者下面可以腾出位置
				My[v] = u;
				Mx[u] = v;
				return 1;
			}
		}
	}
	return 0;
}
int Maxmatch() {
	int ans = 0;
	memset(Mx, -1, sizeof(Mx));
	memset(My, -1, sizeof(My));
	while (bfs()) {
		memset(used, 0, sizeof(used));
		_for(i, un) {
			if (Mx[i] == -1 && dfs(i)) {//当前节点未匹配且找到一条增广路
				ans++;
			}
		}
	}
	return ans;
}
int main() {
	int T;
	cin >> T;
	_rep(Case, 1, T) {
		int t, m;
		cin >> t >> un;
		_for(i, un) p1[i].input1();
		cin >> m;
		_for(i, m) p2[i].input2();
		_for(i, un) G[i].clear();
		_for(i, un) {
			_for(j, m) {
				if (t*t*p1[i].v*p1[i].v >= getdis(p1[i], p2[j])) {
					G[i].push_back(j);
				}
			}
		}
		printf("Scenario #%d:\n%d\n\n", Case, Maxmatch());
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值