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位表示. 接下去每次就 ...
随机推荐
- 前端 JSer 装逼手册
阅读 8143收藏 2352016-7-18 SegmentFault 分享:吉祥物 @ SegmentFault 在装逼成本越来越高的 JS 圈,是时候充值一下了 -- 题记. 作者:kenberk ...
- yaf在windows7下32位的安装教程
首先下载php_yaf.dll文件http://pecl.php.net/package/yaf/2.2.9/windows 打开扩展extension=php_yaf.dll 然后下载工具 http ...
- Lazarus for Raspbian安装
春节前看到树莓派 2代开始销售,第一时间在淘宝下单购买,无奈春节期间放假,要到3月份才可能收到,只能用QEMU模拟器先熟悉树莓系统.对从turbo Pascal开始的人来讲,如果能在树莓系统使用Pas ...
- 设计模式:抽象工厂模式(Abstract Factory)
定 义:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. 结构图: 示例结构图: 实体类: class User { public int Id { get; set; } p ...
- css背景图片定位练习(一)
首先准备一张雪碧图,Like this 背景图片的定位方法有3种,比较常用的两种为 关键字:background-position: top left; (top/bottom/cennter/lef ...
- js实现选项卡
通过JavaScript实现如上选项卡切换的效果. 实现思路: 一.HTML页面布局 选项卡标题使用ul..li 选项卡内容使用div 二.CSS样式制作 整个选项卡的样式设置 选项卡标题的样式设置 ...
- LINQ之select方法选择多个字段
单个字段: var list1 = list.Select(field1 => field1.CouponID).ToList(); 多个字段: var list1 = list.Select( ...
- scrollba美化
1.overflow内容溢出时的设置(设定被设定对象是否显示滚动条) overflow-x水平方向内容溢出时的设置 overflow-y垂直方向内容溢出时的设置 以上三个属性设置的值 ...
- JQuery:JQuery设置HTML
JQuery:设置HTML1.Query - 设置内容和属性设置内容 - text().html() 以及 val()我们将使用前一章中的三个相同的方法来设置内容: text() - 设置或返回所选元 ...
- E: 软件包*需要重新安装,但是我无法找到相应的安装文件。(ubuntu14.04)
ubuntu安装搜狗输入法官方下载的安装包时,电脑有点卡我给强制关闭了,然后重启打开软件管理中心,直接闪退! 之后不能安装任何软件(包括命令). 显示: E: 软件包Sougou Pinyin需要重新 ...