导弹防御塔---二分图匹配《lyd算法进阶》

版权声明: https://blog.csdn.net/sgh666666/article/details/82903118

题目描述

Freda控制着N座可以发射导弹的防御塔。每座塔都有足够数量的导弹,但是每座塔每次只能发射一枚。在发射导弹时,导弹需要T1秒才能从防御塔中射出,而在发射导弹后,发射这枚导弹的防御塔需要T2分钟来冷却。
所有导弹都有相同的匀速飞行速度V,并且会沿着距离最短的路径去打击目标。计算防御塔到目标的距离Distance时,你只需要计算水平距离,而忽略导弹飞行的高度。导弹在空中飞行的时间就是 (Distance/V) 分钟,导弹到达目标后可以立即将它击毁。
现在,给出N座导弹防御塔的坐标,M个入侵者的坐标,T1、T2和V,你需要求出至少要多少分钟才能击退所有的入侵者。

输入格式

第一行五个正整数N,M,T1,T2,V。
接下来M行每行两个整数,代表入侵者的坐标。
接下来N行每行两个整数,代表防御塔的坐标。

输出格式

输出一个实数,表示最少需要多少分钟才能击中所有的入侵者,四舍五入保留六位小数。

提示

对于40%的数据,N,M<=20.
对于100%的数据, 1≤N≤50, 1≤M≤50,坐标绝对值不超过10000,T1,T2,V不超过2000.

样例数据

输入样例 #1

输出样例 #1

3 3 30 20 1

0 0

0 50

50 0

50 50

0 1000

1000 0

91.500000

 

分析:

《lyd算法进阶》二分多重匹配例题。二分规定的时间内是否所有的入侵者能否被消灭即可,即是否存在最大二分匹配。

 

 

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<set>
#include<map>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define oo cout<<"!!!"<<endl;
typedef long long ll;
typedef unsigned long long ull;
#define ms(s) memset(s, 0, sizeof(s))
const int inf = 0x3f3f3f3f;
//head

const int maxn = 1111;
std::vector<int> g[maxn];
double t1,t2,V;
double dis[maxn][maxn];
int n,m;

struct max_mactch
{
	vector<int>g[maxn];
	bool vis[maxn];
	int left[maxn];

	void init()
	{
		rep(i,0,m+11)g[i].clear();
		memset(left,-1,sizeof left);
	}

	bool match(int u)
	{
		rep(i,0,g[u].size())
		{
			int v = g[u][i];
			if(!vis[v])
			{
				vis[v] = true;
				if(left[v] == -1 || match(left[v]))
				{
					left[v] = u;
					return true;
				}
			}
		}
		return false;
	}

	bool C(double mid)
	{
		double T = mid;
		init();
		int k = 1;

		while(T>=t1)
		{
			T -= t1;
			k++;
			rep(i,1,m+1)
				rep(j,1,n+1)
				if(dis[j][i] <= T)
					g[i].push_back(m+(k-1)*n+j);
			T-=t2;
		}
		int ans = 0;
		rep(i,1,m+1)
		{
			ms(vis);
			if(match(i))
				ans++;
		}
		return ans == m;

	}
}MM;
struct node
{
	double x,y;
}a[maxn];

int main() 
{
	
	cin>>n>>m>>t1>>t2>>V;
	t1 /= 60;

	rep(i,1,m+1)
	{
		cin>>a[i].x>>a[i].y;
	}
	rep(i,1,n+1)
	{
		double x,y;
		cin>>x>>y;
		rep(j,1,m+1)
		{
			dis[i][j] = sqrt((x-a[j].x) * (x-a[j].x) + (y-a[j].y) * (y - a[j].y)) / V;
		}

	}
	double l = t1,r = 1111111;
	rep(i,1,111)
	{
		double mid = (l+r)/2;
		if(MM.C(mid))r = mid;
		else l = mid;
	}
	printf("%.6lf",r);
}

猜你喜欢

转载自blog.csdn.net/sgh666666/article/details/82903118