2021-10-082020级国庆七天乐第4天 D
作者:互联网
D . In a Jam
Description
A very hungry caterpillar has a hard time leafing food alone. Just yesterday he got himself into a real pickle (thankfully, he managed to chew his way out of that one). Today, he is in a jam and he needs your help finding his way out.
As it turns out, n drops of jam were spilled on the picnic table that the very hungry caterpillar is on. The ith drop of jam has spread out and now covers a circle of radius ri with center (xi,yi). Drops may overlap each other but no more than two outlines of drops will intersect at the same point. The caterpillar is located at the origin, (0,0). What is the shortest distance he needs to travel to reach a part of the table that isn’t covered in jam?
The Problem:
Given the locations and sizes of all the jam droplets on the table, find the shortest distance the very hungry caterpillar needs to travel to reach a part of the table that isn’t sticky and covered in jam.
Input
The input begins with a line containing a single integer, t, representing the number of tables to consider. Each table is defined across multiple lines. The first line for each table will contain a single integer, n (1≤n≤100), representing the number of jam droplets. On the next n lines, each contains three integers, xi,yi and ri (−1,000≤xi≤1,000; −1,000≤yi≤1,000; 1≤ri≤1,000), representing the center, (xi,yi), and radius, ri, of the circle of jam, respectively. The caterpillar is guaranteed to be within some jam (and will not be on an edge of some jam).
Output
For each table, output a single line containing a real number representing the minimum distance that the very hungry caterpillar needs to travel. The values should be output with three decimal places and rounded (0.0024 rounds to 0.002; 0.0025 rounds to 0.003).
Samples
Input 复制
2
2
-1 0 2
10 1 9
3
0 1 2
-1 0 2
400 1 400
Output
1.000
1.727
,n滴果酱洒在了野餐桌上,那只饥饿的毛毛虫就在上面。第i滴果酱已经扩散开来,现在覆盖了一个半径为ri的圆心(xi,yi)。液滴可以相互重叠,但在同一点上相交的液滴轮廓不得超过两个。毛毛虫位于原点(0,0)。要到达桌子上没有塞满果酱的部分,他需要走的最短距离是多少?
考虑到桌子上所有果酱滴的位置和大小,存一下没有果酱的点到原点的距离,找出饥饿的毛虫需要经过的最短距离,以到达桌子上没有粘性和果酱覆盖的部分。
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
static bool eq(ld a, ld b)
{
return abs(a - b) < 1e-6;
}
static bool leq(ld a, ld b)
{
return eq(a, b) || a < b;
}
struct Vec
{
ld x, y;
Vec(ld x, ld y): x(x), y(y) {}
Vec operator+(const Vec &o) const
{
return Vec(x + o.x, y + o.y);
}
Vec operator-(const Vec &o) const
{
return Vec(x - o.x, y - o.y);
}
Vec operator*(const ld c) const
{
return Vec(c*x, c*y);
}
ld operator*(const Vec &o) const
{
return x*o.x + y*o.y;
}
ld operator%(const Vec &o) const
{
return x*o.y - y*o.x;
}
ld mag2() const
{
return x*x + y*y;
}
ld mag() const
{
return sqrt(mag2());
}
Vec unit() const
{
return (*this) * (1 / mag());
}
Vec rot90() const
{
return Vec(-y, x);
}
Vec rot270() const
{
return Vec(y, -x);
}
bool operator==(const Vec &o) const
{
return eq(x, o.x) && eq(y, o.y);
}
bool operator!=(const Vec &o) const
{
return !(*this == o);
}
};
struct Circle
{
Vec c;
ld r;
Circle(Vec c, ld r): c(c), r(r) {}
bool operator==(const Circle &o) const
{
return c == o.c && eq(r, o.r);
}
bool operator!=(const Circle &o) const
{
return !(*this == o);
}
bool contains(Vec v)
{
Vec dir = c - v;
return !eq(dir.mag(), r) && leq(dir.mag(), r);
}
vector<Vec> intersect(Circle o)
{
Vec dir = o.c - c;
Vec unit = dir.unit();
ld d2 = dir.mag2();
ld d = sqrt(d2);
if (r + o.r < d || r + d < o.r || o.r + d < r)
return vector<Vec>();
if (eq(d, r + o.r) || eq(r, d + o.r) || eq(o.r, d + r))
return vector<Vec>(1, c + unit * (r / d));
if (d > r + o.r)
return vector<Vec>();
ld d1 = (r*r + d2 - o.r*o.r) / (2 * d);
ld h = sqrt(r*r - d1*d1);
vector<Vec> ret;
ret.push_back(c + (unit*d1) + (unit.rot270() *h));
ret.push_back(c + (unit*d1) + (unit.rot90() * h));
return ret;
}
};
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
vector<Circle> drops;
for (int i = 0; i < n; i++)
{
ld x, y, r;
cin >> x >> y >> r;
drops.push_back(Circle(Vec(x, y), r));
}
vector<Vec> pts;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i == j)
continue;
vector<Vec> inter = drops[i].intersect(drops[j]);
pts.insert(pts.end(), inter.begin(), inter.end());
}
Vec dir = drops[i].c;
if (dir == Vec(0, 0))
pts.push_back(Vec(drops[i].r, 0));
else
pts.push_back(dir.unit() * (dir.mag() - drops[i].r));
}
ld ans = 1e12;
for (int j = 0; j < static_cast<int>(pts.size()); j++)
{
bool good = true;
for (int i = 0; i < n; i++)
if (drops[i].contains(pts[j]))
{
good = false;
break;
}
if (good)
ans = min(ans, pts[j].mag());
}
cout << setprecision(3) << fixed << ans << "\n";
}
return 0;
}
标签:10,ld,return,dir,2021,const,drops,082020,Vec 来源: https://blog.csdn.net/qq_52172723/article/details/120657392