我的解法是看了那个题目分析:
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
#define MAX 8000
int mark[MAX] = { 0 };//mark为0说明没有被圆包
int vis[MAX + 1] = { 0 };//记录dfs访问的
struct cir
{
int x;
int y;
int r;
};
struct pos
{
int x;
int y;
int c=-1;
};
struct Hlist
{
int n;
cir c;
struct Node* first = NULL;
}hlist[MAX+1];
struct Node
{
int n;
struct Node* next = NULL;
};
void link(int big, int small)
{
Node* tmp = hlist[big].first;
Node* t = new Node;
t->n = small;
if (tmp == NULL)
{
hlist[big].first = t;
return;
}
while (tmp->next != NULL)tmp = tmp->next;
tmp->next = t;
}
void dfs(int begin, int destny, int x,int &mark,int lenth,int &maxi)//mark为1表明已从起点走到终点可返回
{
vis[x] = 1;
if (x != begin)lenth++;
if (x == destny)
{
maxi = lenth;
mark = 1;
return;
}
Node* tmp = hlist[x].first;
while (tmp != NULL)
{
if(!vis[tmp->n])dfs(begin, destny, tmp->n, mark, lenth,maxi);
if (mark)
{
return;
}
tmp = tmp->next;
}
}
vector<cir>d;
int main()
{
int n;
cin >> n;
cir tmp;
for (int i = 0; i < n; i++)
{
cin >> tmp.x >> tmp.y >> tmp.r;
d.push_back(tmp);
}
pos a1;
pos a2;
cin >> a1.x >> a1.y >> a2.x >> a2.y;
int k = 0;
for (int i = 0; i < n; i++)
{
if (i == 0)
{
hlist[i].n = k;
hlist[i].c = d[i];
}
else
{
hlist[i].n = k;
hlist[i].c = d[i];
for (int j = 0; j < i; j++)
{
double dis = sqrt(pow((hlist[j].c.x - d[i].x), 2) + pow((hlist[j].c.y - d[i].y), 2));
if (dis < double(hlist[j].c.r + d[i].r))
{
if (hlist[j].c.r > d[i].r)
{
mark[i] = 1;
}
else mark[j] = 1;
link(j, i);
link(i, j);
}
}
}
k++;
}
hlist[n].n = n;
Node* t1 = hlist[n].first;
for (int i = 0; i < n; i++)
{
if (mark[i] == 0)
{
link(n, i);
link(i, n);
}
}
for (int i = 0; i < n; i++)
{
double dis1= sqrt(pow((hlist[i].c.x - a1.x), 2) + pow((hlist[i].c.y - a1.y), 2));
if (dis1 < hlist[i].c.r)
{
if (a1.c != -1)
{
if (hlist[i].c.r < hlist[a1.c].c.r)a1.c = i;
}
else
{
a1.c = i;
}
}
double dis2 = sqrt(pow((hlist[i].c.x - a2.x), 2) + pow((hlist[i].c.y - a2.y), 2));
if (dis2 < hlist[i].c.r)
{
if (a2.c != -1)
{
if (hlist[i].c.r < hlist[a2.c].c.r)a2.c = i;
}
else
{
a2.c = i;
}
}
}
//如果a1 a2都没被圆包围 则他们在最外面的非圆区域
if (a1.c == -1)a1.c = n;
if (a2.c == -1)a2.c = n;
int begin = a1.c;
int destny = a2.c;
int mark = 0;
int maxi = 0;
dfs(begin, destny, begin, mark, 0,maxi);
cout << a1.c << ' ' << a2.c << endl;
cout << maxi << endl;
return 0;
}
但是超时了,于是看到了另一种方法:
/*
标解是这样的:
题目分析
把平面上每一个区域看作一个结点,最外层没有边界的区域也看作一个结点。
如果一个区域刚好被另外一个区域直接包含,则连边。构成的图上做最短路径即可以得到40~60的分数。
又发现,上述得到的图是树结构的,在树上预处理好任意两点的最近公共祖先,之后的询问可以线形完成,
这便可以得到满分。
但是其实可以有更简单的做法
先判断两个人在任意一个圆的圆内还是圆外,如果一个人在圆内,一个人在圆外,
那么必须破坏一个圆,复杂度为N*Q Orz
这样做就很水了 因为考虑到
如果直接返回圆心和该点的欧几里德距离
那么就又设计到了浮点数的问题了 QAQ我太弱所以最讨厌浮点数什么的
所以我们可以直接保留平方就好
因为都是正数嘛~比平方和比自己都是等效的
再运用异或 用两个bool值表示是否在某个圆外
只有异或值为真才要拆了一堵墙
所以这样就可以直接解决了QWQ
*/
#pragma GCC optimize("O2")
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const long long MAXN=8010;
struct node
{
long long x,y,r,s;
}p[MAXN];
int n;
int q;
long long ans;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%lld%lld%lld",&p[i].x,&p[i].y,&p[i].r);
p[i].s=p[i].r*p[i].r;
}
cin>>q;
while(q--)
{
ans=0;
long long a,b,c,d;
scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
for(int i=1;i<=n;i++)
{
long long way1=(a-p[i].x)*(a-p[i].x)+(b-p[i].y)*(b-p[i].y);
long long way2=(c-p[i].x)*(c-p[i].x)+(d-p[i].y)*(d-p[i].y);
bool f1=false,f2=false;
if(way1>=p[i].s)
f1=true;
if(way2>=p[i].s)
f2=true;
if(f1^f2)
ans++;
}
printf("%lld\n",ans);
}
return 0;
}
题解来自:题解 - Vijos