HDU 4063 Aircraft(计算几何)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4063
Description
In the game, player controls an aircraft in a 2D-space.
The mission is to drive the craft from starting point to terminal point.
The craft needs wireless signal to move.
A number of devices are placed in the 2D-space, spreading signal.
For a device Di, it has a signal radius -- Ri.
When the distance between the craft and Di is shorter or equal to Ri, it(the craft) gets Di's wireless signal.
Now you need to tell me the shortest path from starting point to terminal point.
Input
The rest of the test file contains T blocks.
Each block starts with an integer n, followed by n devices given as (xi, yi, Ri).
(xi, yi) is position of Di, and Ri is the radius of its signal range.
The first point is the starting point.
The last point is the terminal point.
T <= 25;
2 <= n <= 20 for most cases;
20 < n <= 25 for several cases, completely random generated.
-1000 <= xi, yi <= 1000 , 1 <= ri <= 1000.
All are integers.
Output
Otherwise, output a float number, correct the result to 4 decimal places.(as shown in the sample output)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL; const int MAXN = ;
const int MAXV = MAXN * MAXN * ;
const int MAXE = MAXV * MAXV;
const double EPS = 1e-;
const double INF = 1e100; inline double sqr(double x) {
return x * x;
} inline int sgn(double x) {
return (x > EPS) - (x < -EPS);
} struct Point {
double x, y;
Point() {}
Point(double x, double y): x(x), y(y) {}
void read() {
scanf("%lf%lf", &x, &y);
}
bool operator < (const Point &rhs) const {
if(sgn(x - rhs.x) != ) return x < rhs.x;
return sgn(y - rhs.y) < ;
}
bool operator == (const Point &rhs) const {
return sgn(x - rhs.x) == && sgn(y - rhs.y) == ;
}
Point operator + (const Point &rhs) const {
return Point(x + rhs.x, y + rhs.y);
}
Point operator - (const Point &rhs) const {
return Point(x - rhs.x, y - rhs.y);
}
double operator * (const Point &rhs) const {
return x * rhs.x + y * rhs.y;
}
Point operator * (double d) const {
return Point(x * d, y * d);
}
Point operator / (double d) const {
return Point(x / d, y / d);
}
Point rotate() const {
return Point(-y, x);
}
double length() const {
return sqrt(*this * *this);
}
}; double dist(const Point &a, const Point &b) {
return (a - b).length();
} double cross(const Point &a, const Point &b) {
return a.x * b.y - a.y * b.x;
} double cross(const Point &o, const Point &a, const Point &b) {
return cross(a - o, b - o);
} double Point_to_Line(const Point &p, const Point &st, const Point &ed) {
return fabs(cross(p, st, ed)) / dist(st, ed);
} Point intersection(const Point &u1, const Point &u2, const Point &v1, const Point &v2) {
double t = cross(u1 - v1, v1 - v2) / cross(u1 - u2, v1 - v2);
return u1 + (u2 - u1) * t;
} struct Circle {
Point c;
double r;
Circle() {}
Circle(Point c, double r): c(c), r(r) {}
void read() {
c.read();
scanf("%lf", &r);
}
bool contain(const Circle &rhs) const {
return sgn(dist(c, rhs.c) + rhs.r - r) <= ;
}
bool contain(const Point &p) const {
return sgn(dist(c, p) - r) <= ;
}
bool intersect(const Circle &rhs) const {
return sgn(dist(c, rhs.c) - r - rhs.r) < ;
}
bool tangency(const Circle &rhs) const {
return sgn(dist(c, rhs.c) - r - rhs.r) == ;
}
}; int intersection(const Circle &cir, const Point &st, const Point &ed, Point &p1, Point &p2) {
if(sgn(Point_to_Line(cir.c, st, ed) - cir.r) > ) return ;
Point p = cir.c + (ed - st).rotate();
p = intersection(p, cir.c, st, ed);
double t = sqrt(sqr(cir.r) - sqr(dist(p, cir.c))) / dist(st, ed);
p1 = p + (ed - st) * t;
p2 = p - (ed - st) * t;
return - (p1 == p2);
} int intersection(const Circle &c1, const Circle &c2, Point &p1, Point &p2) {
if(c1.contain(c2) || c2.contain(c1)) return ;
if(!c1.intersect(c2) && !c1.tangency(c2)) return ;
double t = 0.5 * ( + (sqr(c1.r) - sqr(c2.r)) / sqr(dist(c1.c, c2.c)));
Point u = c1.c + (c2.c - c1.c) * t, v = u + (c1.c - c2.c).rotate();
return intersection(c1, u, v, p1, p2);
} struct Graph {
int head[MAXV], ecnt;
int to[MAXE], next[MAXE];
double cost[MAXE];
int n, st, ed; void init() {
memset(head, -, sizeof(head));
ecnt = ;
} void add_edge(int u, int v, double c) {
to[ecnt] = v; cost[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; cost[ecnt] = c; next[ecnt] = head[v]; head[v] = ecnt++;
} double dis[MAXV];
bool vis[MAXV]; double dijksrta(int st, int ed, int n) {
for(int i = ; i < n; ++i) dis[i] = INF;
dis[st] = ;
memset(vis, , sizeof(vis));
while(true) {
int u = -; double d = INF;
for(int i = ; i < n; ++i) if(!vis[i])
if(dis[i] < d) d = dis[i], u = i;
if(d == INF) break;
vis[u] = true;
for(int p = head[u]; ~p; p = next[p]) {
int &v = to[p];
if(!vis[v]) dis[v] = min(dis[v], dis[u] + cost[p]);
}
}
return dis[ed];
}
} G; Circle cir[MAXN];
int T, n; Point list[MAXV]; bool havePath(Point st, Point ed) {
if(ed < st) swap(st, ed);
if(st == ed) return true;
Point p1, p2;
int pcnt = ;
list[pcnt++] = st;
list[pcnt++] = ed;
for(int i = ; i < n; ++i) {
int c = intersection(cir[i], st, ed, p1, p2);
if(c >= ) list[pcnt++] = p1;
if(c >= ) list[pcnt++] = p2;
}
sort(list, list + pcnt);
for(int i = ; i < pcnt; ++i) {
if(list[i] < st || list[i] == st) continue;
bool flag = false;
for(int j = ; j < n && !flag; ++j)
if(cir[j].contain(list[i]) && cir[j].contain(list[i - ])) flag = true;
if(!flag) return false;
if(list[i] == ed) break;
}
return true;
} Point p[MAXV];
int cnt; double solve() {
Point p1, p2;
cnt = ;
p[cnt++] = cir[].c; p[cnt++] = cir[n - ].c;
for(int i = ; i < n; ++i) {
for(int j = i + ; j < n; ++j) {
int c = intersection(cir[i], cir[j], p1, p2);
if(c >= ) p[cnt++] = p1;
if(c >= ) p[cnt++] = p2;
}
}
G.init();
for(int i = ; i < cnt; ++i) {
for(int j = i + ; j < cnt; ++j)
if(havePath(p[i], p[j])) G.add_edge(i, j, dist(p[i], p[j]));
}
return G.dijksrta(, , cnt);
} int main() {
scanf("%d", &T);
for(int kase = ; kase <= T; ++kase) {
scanf("%d", &n);
for(int i = ; i < n; ++i) cir[i].read();
double res = solve();
if(res == INF) printf("Case %d: No such path.\n", kase);
else printf("Case %d: %.4f\n", kase, res);
}
}
下面是用了一部分解析几何的方法,比上面的还要快……
代码(G++ 437MS):
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <numeric>
using namespace std;
typedef long long LL; const int MAXN = ;
const int MAXV = MAXN * MAXN * ;
const int MAXE = MAXV * MAXV;
const double EPS = 1e-;
const double INF = 1e100; inline double sqr(double x) {
return x * x;
} inline int sgn(double x) {
return (x > EPS) - (x < -EPS);
} struct Point {
double x, y;
Point() {}
Point(double x, double y): x(x), y(y) {}
void read() {
scanf("%lf%lf", &x, &y);
}
bool operator < (const Point &rhs) const {
if(sgn(x - rhs.x) != ) return x < rhs.x;
return sgn(y - rhs.y) < ;
}
bool operator == (const Point &rhs) const {
return sgn(x - rhs.x) == && sgn(y - rhs.y) == ;
}
Point operator + (const Point &rhs) const {
return Point(x + rhs.x, y + rhs.y);
}
Point operator - (const Point &rhs) const {
return Point(x - rhs.x, y - rhs.y);
}
double operator * (const Point &rhs) const {
return x * rhs.x + y * rhs.y;
}
Point operator * (double d) const {
return Point(x * d, y * d);
}
Point operator / (double d) const {
return Point(x / d, y / d);
}
Point rotate() const {
return Point(-y, x);
}
double length() const {
return sqrt(*this * *this);
}
}; double dist(const Point &a, const Point &b) {
return (a - b).length();
} double cosIncludeAngle(const Point &a, const Point &b, const Point &o) {
Point p1 = a - o, p2 = b - o;
return (p1 * p2) / (p1.length() * p2.length());
} struct Circle {
Point c;
double r;
Circle() {}
Circle(Point c, double r): c(c), r(r) {}
void read() {
c.read();
scanf("%lf", &r);
}
bool contain(const Circle &rhs) const {
return sgn(dist(c, rhs.c) + rhs.r - r) <= ;
}
bool contain(const Point &p) const {
return sgn(dist(c, p) - r) <= ;
}
bool intersect(const Circle &rhs) const {
return sgn(dist(c, rhs.c) - r - rhs.r) < ;
}
bool tangency(const Circle &rhs) const {
return sgn(dist(c, rhs.c) - r - rhs.r) == ;
}
}; int intersection(const Point &st, const Point &ed, const Circle &cir, Point &p1, Point &p2) {
double angle = cosIncludeAngle(ed, cir.c, st);
if(isnan(angle)) {
Point v = (ed - st) / dist(st, ed);
p1 = cir.c + v * cir.r;
p2 = cir.c - v * cir.r;
return + (cir.r > );
}
double B = dist(cir.c, st);
double a = , b = - * B * angle, c = sqr(B) - sqr(cir.r);
double delta = sqr(b) - * a * c;
if(sgn(delta) < ) return ;
if(sgn(delta) == ) delta = ;
double x1 = (-b - sqrt(delta)) / ( * a), x2 = (-b + sqrt(delta)) / ( * a);
Point v = (ed - st) / dist(ed, st);
p1 = st + v * x1;
p2 = st + v * x2;
return + sgn(delta);
} int intersection(const Circle &c1, const Circle &c2, Point &p1, Point &p2) {
if(c1.contain(c2) || c2.contain(c1)) return ;
if(!c1.intersect(c2) && !c1.tangency(c2)) return ;
double d = dist(c1.c, c2.c);
double d1 = (sqr(c2.r) + sqr(d) - sqr(c1.r)) / / d;
double d2 = sqrt(sqr(c2.r) - sqr(d1));
Point v1 = c2.c + (c1.c - c2.c) / d * d1;
Point v2 = (c1.c - c2.c).rotate() / d;
p1 = v1 + v2 * d2;
p2 = v1 - v2 * d2;
return - (p1 == p2);
} struct Graph {
int head[MAXV], ecnt;
int to[MAXE], next[MAXE];
double cost[MAXE];
int n, st, ed; void init() {
memset(head, -, sizeof(head));
ecnt = ;
} void add_edge(int u, int v, double c) {
to[ecnt] = v; cost[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; cost[ecnt] = c; next[ecnt] = head[v]; head[v] = ecnt++;
} double dis[MAXV];
bool vis[MAXV]; double dijksrta(int st, int ed, int n) {
for(int i = ; i < n; ++i) dis[i] = INF;
dis[st] = ;
memset(vis, , sizeof(vis));
while(true) {
int u = -; double d = INF;
for(int i = ; i < n; ++i) if(!vis[i])
if(dis[i] < d) d = dis[i], u = i;
if(d == INF) break;
vis[u] = true;
for(int p = head[u]; ~p; p = next[p]) {
int &v = to[p];
if(!vis[v]) dis[v] = min(dis[v], dis[u] + cost[p]);
}
}
return dis[ed];
}
} G; Circle cir[MAXN];
int T, n; Point list[MAXV]; bool havePath(Point st, Point ed) {
if(ed < st) swap(st, ed);
if(st == ed) return true;
Point p1, p2;
int pcnt = ;
list[pcnt++] = st;
list[pcnt++] = ed;
for(int i = ; i < n; ++i) {
int c = intersection(st, ed, cir[i], p1, p2);
if(c >= ) list[pcnt++] = p1;
if(c >= ) list[pcnt++] = p2;
}
sort(list, list + pcnt);
for(int i = ; i < pcnt; ++i) {
if(list[i] < st || list[i] == st) continue;
bool flag = false;
//Point x = (list[i] + list[i - 1]) / 2;
for(int j = ; j < n && !flag; ++j)
if(cir[j].contain(list[i]) && cir[j].contain(list[i - ])) flag = true;
if(!flag) return false;
if(list[i] == ed) break;
}
return true;
} Point p[MAXV];
int cnt; double solve() {
Point p1, p2;
cnt = ;
p[cnt++] = cir[].c; p[cnt++] = cir[n - ].c;
for(int i = ; i < n; ++i) {
for(int j = i + ; j < n; ++j) {
int c = intersection(cir[i], cir[j], p1, p2);
if(c >= ) p[cnt++] = p1;
if(c >= ) p[cnt++] = p2;
}
}
G.init();
for(int i = ; i < cnt; ++i) {
for(int j = i + ; j < cnt; ++j)
if(havePath(p[i], p[j])) G.add_edge(i, j, dist(p[i], p[j]));
}
return G.dijksrta(, , cnt);
} int main() {
scanf("%d", &T);
for(int kase = ; kase <= T; ++kase) {
scanf("%d", &n);
for(int i = ; i < n; ++i) cir[i].read();
double res = solve();
if(res == INF) printf("Case %d: No such path.\n", kase);
else printf("Case %d: %.4f\n", kase, res);
}
}
HDU 4063 Aircraft(计算几何)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)的更多相关文章
- HDU 4069 Squiggly Sudoku(DLX)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4069 Problem Description Today we play a squiggly sud ...
- HDU 4064 Carcassonne(插头DP)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4064 Problem Description Carcassonne is a tile-based ...
- HDU 4031 Attack(离线+线段树)(The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031 Problem Description Today is the 10th Annual of ...
- HDU 4291 A Short problem(2012 ACM/ICPC Asia Regional Chengdu Online)
HDU 4291 A Short problem(2012 ACM/ICPC Asia Regional Chengdu Online) 题目链接http://acm.hdu.edu.cn/showp ...
- 【动态规划】HDU 5492 Find a path (2015 ACM/ICPC Asia Regional Hefei Online)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5492 题目大意: 一个N*M的矩阵,一个人从(1,1)走到(N,M),每次只能向下或向右走.求(N+ ...
- hdu 5877 线段树(2016 ACM/ICPC Asia Regional Dalian Online)
Weak Pair Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- HDU 4758——Walk Through Squares——2013 ACM/ICPC Asia Regional Nanjing Online
与其说这是一次重温AC自动机+dp,倒不如说这是个坑,而且把队友给深坑了. 这个题目都没A得出来,我只觉得我以前的AC自动机的题目都白刷了——深坑啊. 题目的意思是给你两个串,每个串只含有R或者D,要 ...
- Hdu 5459 Jesus Is Here (2015 ACM/ICPC Asia Regional Shenyang Online) 递推
题目链接: Hdu 5459 Jesus Is Here 题目描述: s1 = 'c', s2 = 'ff', s3 = s1 + s2; 问sn里面所有的字符c的距离是多少? 解题思路: 直觉告诉我 ...
- HDU 5010 Get the Nut(2014 ACM/ICPC Asia Regional Xi'an Online)
思路:广搜, 因为空格加上动物最多只有32个那么对这32个进行编号,就能可以用一个数字来表示状态了,因为只有 ‘P’ 'S' 'M' '.' 那么就可以用4进制刚好可以用64位表示. 接下去每次就 ...
随机推荐
- [daily][network] NAT原理(转)
写在转发之前: 一直以来,我一直有一个疑惑,SNAT的时候,如果两个内网主机恰巧使用了相同的源端口号该怎么办呢? 我自己猜测的方法是改掉一个端口号,把端口一起映射(当然还有另一个设想,就是把包同时广播 ...
- .NET同页面内用户控件与父页面以及控件之间方法调用
用户控件调用父页面的方法: //获得父页面 Page p =this.Parent.Page; Type pageType = p.GetType(); //父页面的方法名 MethodInfo mi ...
- 通过runtime替换系统类实现的代码(从github开源库fdstackview中摘录)
其中部分代码为汇编:由此可见oc的runtime的灵活性和能力.此代码仅供参考 // ---------------------------------------------------- // R ...
- Java高级之线程同步
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 关于实现多线程的意义,"从业四年看并发"一文已经讲述,而本篇主要讲一下常用的设计 ...
- CLH锁 、MCS锁
一.引文 1.1 SMP(Symmetric Multi-Processor) 对称多处理器结构,指服务器中多个CPU对称工作,每个CPU访问内存地址所需时间相同.其主要特征是共享,包含对CPU,内存 ...
- Delphi Memory-Mapped File简单示例
{ Copyright ?1999 by Delphi 5 Developer's Guide - Xavier Pacheco and Steve Teixeira } unit MainFrm; ...
- self.nsme 和 _name 的问题
.h文件 @interface myclass:NSObject @property(nonatomic,retain)NSArray*MyArray; @end .m文件 @implementati ...
- 昂贵的聘礼---poj1062(最短路)
题目链接:http://poj.org/problem?id=1062 题意很清楚: 可以虚拟一个起点0,由于存在等级关系,所以可以枚举等级,然后把各种关系建立边,然后计算0到1的距离即可,去最小值即 ...
- table tricks
- SQL Server Logical/Physical Reads
Summary Info: Logical Reads : Reading Data pages from CachePhysical Reads : Reading Data pages ...