sgu198:Get Out!(计算几何+spfa判负环)

题意:

平面上有一堆圆,再给出一个圆P,问P能否在其他圆的缝隙中穿过到达无穷远处?

分析:

把每一个圆加上圆P的半径,圆P就成了一个点。

我们把每两个相交的圆之间连两条有向边,边权为P到线段的有向角度(互为相反数)。

然后spfa判断有没有负环即可。   

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN = 309;
const double eps = 1e-10;
int n;
double sx, sy, sr;
struct cir
{
	double x, y, r;	
	cir(double x = 0, double y = 0, double r = 0):x(x), y(y), r(r) {};
	void read(){scanf("%lf%lf%lf", &x, &y, &r);};
}isl[MAXN], O;
struct Edge
{
	int v, ne;
	double d;	
}edge[MAXN*MAXN];
int edgehead[MAXN], p = 1;
double dist[MAXN];
bool vis[MAXN];
int ct[MAXN];

cir operator - (const cir a, const cir b) {return cir(a.x-b.x, a.y-b.y, 0);}
double cross(const cir a, const cir b) {return a.x*b.y-a.y*b.x;}
double dot(const cir a, const cir b) {return a.x*b.x+a.y*b.y;}
double dis(const cir a, const cir b) {return sqrt(dot(a-b, a-b));}
int dcmp(double a) 
{
	if(fabs(a) <= eps) return 0;
	else return a>0?1:-1;	
}

bool check(int i, int j)
{
	double dist = dis(isl[i], isl[j]);	
	if(dcmp(dist-isl[i].r-isl[j].r) < 0) return true;
	else return false; 
}

double calc(int i, int j)
{
	double l1 = dis(isl[i], cir(sx, sy, 0)), l2 = dis(isl[j], cir(sx, sy, 0)), l3 = dis(isl[i], isl[j]);
	return acos((l1*l1+l2*l2-l3*l3)/2/l1/l2);
}

void add(int u, int v, double d)
{
	edge[p].v = v;edge[p].d = d;
	edge[p].ne = edgehead[u];edgehead[u] = p++;	
}

bool spfa()
{
	queue<int> q;
	for(int i = 1; i <= n; ++i)	q.push(i), vis[i] = true;
	while(!q.empty())
	{
		int u = q.front();q.pop();
		vis[u] = false;
		for(int i = edgehead[u]; i; i = edge[i].ne)
		{
			int v = edge[i].v;
			double d = edge[i].d;
			if(dcmp(dist[v]-dist[u]-d) > 0)
			{
				dist[v] = dist[u]+d;
				if(vis[v]) continue;
				else q.push(v), vis[v] = true;
				if(++ct[v] > n)
					return false;
			}
		}
	}
	return true;
}

int main()
{
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i)
		isl[i].read();
	scanf("%lf%lf%lf", &sx, &sy, &sr);
	for(int i = 1; i <= n; ++i)
		isl[i].r += sr;
	for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= n; ++j)
			if(i != j && check(i, j))
			{
				int f = cross(isl[i]-cir(sx, sy, 0), isl[j]-cir(sx, sy, 0))>0?1:-1;
				add(i, j, f*calc(i, j));
			}
	if(spfa()) puts("YES");
	else puts("NO");
	return 0;	
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值