题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4063

Description

You are playing a flying game. 
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 first line of the input file is a single integer T. 
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

For each case, Output "No such path." if the craft can't get to the terminal point. 
Otherwise, output a float number, correct the result to 4 decimal places.(as shown in the sample output) 
 
题目大意&题解:http://blog.csdn.net/zxy_snow/article/details/6849550
PS:首位共点的时候一直输出No such path没发现调了半天……
 
下面是用计算几何的方法,不知为何G++一直TLE(难道是我写错了?),可能是因为加入了大量的函数运算然后没有被优化……
顺便贴个模板
 
代码(C++ 750MS):
 #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)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 【动态规划】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+ ...

  6. 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 ...

  7. HDU 4758——Walk Through Squares——2013 ACM/ICPC Asia Regional Nanjing Online

    与其说这是一次重温AC自动机+dp,倒不如说这是个坑,而且把队友给深坑了. 这个题目都没A得出来,我只觉得我以前的AC自动机的题目都白刷了——深坑啊. 题目的意思是给你两个串,每个串只含有R或者D,要 ...

  8. 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的距离是多少? 解题思路: 直觉告诉我 ...

  9. HDU 5010 Get the Nut(2014 ACM/ICPC Asia Regional Xi'an Online)

    思路:广搜, 因为空格加上动物最多只有32个那么对这32个进行编号,就能可以用一个数字来表示状态了,因为只有 ‘P’   'S' 'M' '.' 那么就可以用4进制刚好可以用64位表示. 接下去每次就 ...

随机推荐

  1. 修改MyEclipse默认的Servlet和jsp代码模板

    一.修改Servlet的默认模板代码 使用MyEclipse创建Servlet时,根据默认的Servlet模板生成的Servlet代码如下: 1 package gacl.servlet.study; ...

  2. Mongo对内嵌文档的CRUD

    { "_id" : ObjectId("5706032acd0a6194868cf53e"), "list" : { "age&q ...

  3. 转 创建 JavaScript XML 文档注释

    http://www.cnblogs.com/chenxizhang/archive/2009/07/12/1522058.html 如何:创建 JavaScript XML 文档注释 Visual ...

  4. 【Android测试】【随笔】模拟长按电源键

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/5195121.html 起因 昨天群里看到有人问如何实现一个 ...

  5. python装饰器方法

    前几天向几位新同事介绍项目,被问起了@login_required的实现,我说这是django框架提供的装饰器方法,验证用户是否登录,只要这样用就行了,因为自己不熟,并没有做过多解释. 今天查看dja ...

  6. JS判断对象类型

    对于确定JS内置对象类型,JS提供了typeof运算符,该运算符得到的结果为以下6种:number,boolean,string,function,object,undefined.不过对绝大多数对象 ...

  7. Asp.net MVC中Route的理解

    在Asp.net MVC中,对于一个请求,是通过路由找到对应的Controller类中的对应Action,并与model进行交互,最后返回到view. 就是说Asp.net MVC是用Route来定义 ...

  8. Python迁移MySQL数据到MongoDB脚本

    MongoDB是一个文档数据库,在存储小文件方面存在天然优势.随着业务求的变化,需要将线上MySQL数据库中的行记录,导入到MongoDB中文档记录. 一.场景:线上MySQL数据库某表迁移到Mong ...

  9. 利用Axis2默认口令安全漏洞可入侵WebService网站

    利用Axis2默认口令安全漏洞可入侵WebService网站 近期,在乌云上关注了几则利用Axis2默认口令进行渗透测试的案例,大家的渗透思路基本一致,利用的技术工具也大致相同,我在总结这几则案例的基 ...

  10. 关于mysql的错误 - no query specified

    Mysql----error:no query specified mysql下抛出错误:error:no query specified出现此错误是sql不合法原因:如:select * from ...