hdu2389二分图匹配

思路:只需要判断人到每个放伞的位置花的时间和离下雨的时间,看是否可以建边;

/*****************************************
Author      :Crazy_AC(JamesQi)
Time        :2015
File Name   :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
using namespace std;
#define MEM(a,b) memset(a,b,sizeof a)
#define pk push_back
template<class T> inline T Get_Max(const T&a,const T&b){return a < b?b:a;}
template<class T> inline T Get_Min(const T&a,const T&b){return a < b?a:b;}
typedef long long ll;
typedef pair<int,int> ii;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int maxn = 3e3 + 10;
vector<int> G[maxn];
int link[maxn];
bool vis[maxn];
int uN,vN;
struct node{
	int x,y;
	int v;
}people[maxn];
struct point{
	int x,y;
}umbrella[maxn];
double mul(double x){
	return x * x;
}
double get_dis(int i,int j){
	return sqrt(mul(people[i].x - umbrella[j].x) + mul(people[i].y - umbrella[j].y));
}
/*
bool SearchP(int u){
	for (int i = 0;i < G[u].size();i++){
		int v = G[u][i];
		if (!vis[v]){
			vis[v] = true;
			if (link[v] == -1 || SearchP(link[v])){
				link[v] = u;
				link[u] = v;
				return true;
			}
		}
	}
	return false;
}

inline int Hungary(int n){
	int ret = 0;
	MEM(link, -1);
	for (int i = 1;i <= n;i++){
		if (link[i] < 0){
			MEM(vis, false);
			if (SearchP(i)) ret++;
		}
	}
	return ret;
}
*/

int dx[maxn],dy[maxn];
int Mx[maxn],My[maxn];
int dis;
int n,m;
bool SearchP(){
	queue<int> que;
	MEM(dx, -1);
	MEM(dy, -1);
	for (int i = 1;i <= n;i++){
		if (Mx[i] == -1){
			que.push(i);
			dx[i] = 0;
		}
	}
	dis = INF;
	while(!que.empty()){
		int u = que.front();
		que.pop();
		if (dx[u] > dis) break;
		for (int i = 0;i < G[u].size();i++){
			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;
					que.push(My[v]);
				}
			}
		}
	}
	return dis != INF;
}

bool dfs(int u){
	for (int i = 0;i < G[u].size();++i){
		int v = G[u][i];
		if (!vis[v] && dy[v] == dx[u] + 1){
			if (My[v] != -1 && dis == dy[v]) continue;
			if (My[v] == -1 || dfs(My[v])){
				My[v] = u;
				Mx[u] = v;
				return true;
			}
		}
	}
	return false;
}



inline int Hopcroft_Karp(){
	int ret = 0;
	MEM(Mx, -1);
	MEM(My, -1);
	while(SearchP()){
		MEM(vis, false);
		for (int i = 1;i <= n;i++){
			if (Mx[i] == -1 && dfs(i)) ret++;
		}
	}
	return ret;
}

int main()
{	
	// ios::sync_with_stdio(false);
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	int kase,t;
	int iCase = 0;
	scanf("%d",&kase);
	while(kase--){
		scanf("%d%d",&t,&n);
		for (int i = 1;i <= n;i++){
			G[i].clear();
			scanf("%d%d%d",&people[i].x,&people[i].y,&people[i].v);
		}
		scanf("%d",&m);
		for (int i = 1;i <= m;i++){
			scanf("%d%d",&umbrella[i].x,&umbrella[i].y);
		}
		for (int i = 1;i <= n;i++){
			for (int j = 1;j <= m;j++){
				if (get_dis(i,j) <= t * people[i].v) G[i].push_back(j);
			}
		}
		// int ans = Hungary(n);
		int ans = Hopcroft_Karp();
		printf("Scenario #%d:\n",++iCase);
		printf("%d\n\n",ans);
		// if (kase) printf("\n");
	}
	return 0;
}


你可能感兴趣的:(图论,二分图匹配)