hdu 4063 Aircraft (Geometry + SP)
几何加简单最短路。
题意是给出若干圆的圆心以及半径,求出从给出的起点到终点的最短路径的长度,可以移动的区域是圆覆盖到的任意一个位置。
做法是这样的,对圆两两求交点,用这些得到的交点以及起点和终点作为我们要构造的图的顶点。因为我们要的是最短路径,所以如果我们要从一个区域穿越到另一区域的时候,必然是经过这些交点的。然后我们可以对这些交点两两判断是否能够相互到达。这个判断才是这道题的关键。判断的方法可以有几种,我想到的一是先求出直线与所有圆的交点,排序以后判断所有的圆能否覆盖掉这条线段;而我的是另一种方法,就是用一个队列,将还没有被覆盖的线段存在队列里,每次跟圆相交判断线段是否仍未被覆盖。
最后来一个单源最短路就搞掂了!
代码如下:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue> using namespace std; const double EPS = 1e-;
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) {}
bool operator < (Point a) const { return sgn(x - a.x) < || sgn(x - a.x) == && y < a.y;}
bool operator <= (Point a) const { return (sgn(x - a.x) < || sgn(x - a.x) == && sgn(y - a.y) < ) || (sgn(x - a.x) == && sgn(y - a.y) == );}
bool operator == (Point a) const { return sgn(x - a.x) == && sgn(y - a.y) == ;}
void print() { cout << x << '~' << y << endl;}
} ;
template <class T> T sqr(T x) { return x * x;}
typedef Point Vec;
Vec operator + (Vec a, Vec b) { return Vec(a.x + b.x, a.y + b.y);}
Vec operator - (Vec a, Vec b) { return Vec(a.x - b.x, a.y - b.y);}
Vec operator * (Vec a, double p) { return Vec(a.x * p, a.y * p);}
Vec operator / (Vec a, double p) { return Vec(a.x / p, a.y / p);} inline double dotDet(Vec a, Vec b) { return a.x * b.x + a.y * b.y;}
inline double dotDet(Point o, Point a, Point b) { return dotDet(a - o, b - o);}
inline double crossDet(Vec a, Vec b) { return a.x * b.y - a.y * b.x;}
inline double crossDet(Point o, Point a, Point b) { return crossDet(a - o, b - o);}
inline double vecLen(Vec x) { return sqrt(dotDet(x, x));}
inline double angle(Vec v) { return atan2(v.y, v.x);}
inline double angle(Vec a, Vec b) { return acos(dotDet(a, b) / vecLen(a) / vecLen(b));}
inline Vec vecUnit(Vec x) { return x / vecLen(x);}
inline Vec rotate(Vec x, double rad) { return Vec(x.x * cos(rad) - x.y * sin(rad), x.x * sin(rad) + x.y * cos(rad));}
inline Vec normal(Vec x) { return Vec(-x.y, x.x) / vecLen(x);} struct Line {
Point s, t;
Line() {}
Line(Point s, Point t) : s(s), t(t) {}
Vec vec() { return t - s;}
Point point(double x) { return s + vec() * x;}
} ;
typedef Line Seg; inline bool onSeg(Point x, Point a, Point b) { return sgn(crossDet(a - x, b - x)) == && sgn(dotDet(a - x, b - x)) < ;}
inline bool onSeg(Point x, Seg s) { return onSeg(x, s.s, s.t);} inline Point lineIntersect(Point P, Vec v, Point Q, Vec w) { return P + v * (crossDet(w, P - Q) / crossDet(v, w));}
inline Point lineIntersect(Line a, Line b) { return lineIntersect(a.s, a.vec(), b.s, b.vec());} struct Circle {
Point c;
double r;
Circle() {}
Circle(Point c, double r) : c(c), r(r) {}
Point point(double a) { return Point(c.x + cos(a) * r, c.y + sin(a) * r);}
} ;
int lineCircleIntersect(Line L, Circle C, vector<Point> &sol) {
Vec nor = normal(L.vec());
Point mid = lineIntersect(C.c, nor, L.s, L.vec());
double len = sqr(C.r) - sqr(vecLen(C.c - mid));
if (sgn(len) < ) return ;
if (sgn(len) == ) { sol.push_back(mid); return ;}
Vec dis = vecUnit(L.vec());
len = sqrt(len);
sol.push_back(mid + dis * len);
sol.push_back(mid - dis * len);
return ;
} int circleCircleIntersect(Circle C1, Circle C2, vector<Point> &sol) {
double d = vecLen(C1.c - C2.c);
if (sgn(d) == ) {
if (sgn(C1.r - C2.r) == ) return -;
return - + (C1.r > C2.r);
}
if (sgn(C1.r + C2.r - d) < ) return ;
if (sgn(fabs(C1.r - C2.r) - d) > ) return - + (C1.r > C2.r);
double a = angle(C2.c - C1.c);
double da = acos((sqr(C1.r) + sqr(d) - sqr(C2.r)) / (2.0 * C1.r * d));
Point p1 = C1.point(a - da), p2 = C1.point(a + da);
sol.push_back(p1);
// p1.print();
if (p1 == p2) return ;
sol.push_back(p2);
// p2.print();
return ;
} inline bool ptInCircle(Point p, Circle c) { return sgn(vecLen(c.c - p) - c.r) <= ;} const int N = ;
Circle dev[N];
vector<Point> vex;
int sid, tid;
double mat[N * N][N * N]; Seg qseg[N * N]; double cal(int pi, int pj, int n) {
// queue<Seg> seg;
vector<Point> ip;
int qh, qt;
qh = qt = ;
// while (!seg.empty()) seg.pop();
if (vex[pj] < vex[pi]) swap(pi, pj);
Seg L = Seg(vex[pi], vex[pj]);
// seg.push(L);
qseg[qt++] = L;
for (int i = ; i < n; i++) {
// int sz = seg.size();
int sz = qt - qh;
for (int j = ; j < sz; j++) {
// Seg tmp = seg.front();
// seg.pop();
Seg tmp = qseg[qh++];
// if (pi == 0 && pj == 2) {
// tmp.s.print();
// tmp.t.print();
// puts("tmp");
// }
ip.clear();
if (lineCircleIntersect(L, dev[i], ip) == ) {
if (ip[] < ip[]) swap(ip[], ip[]);
// if (pi == 0 && pj == 4) {
// ip[0].print();
// ip[1].print();
// puts("ip");
// }
if (ip[] <= tmp.s || tmp.t <= ip[])
// seg.push(tmp);
qseg[qt++] = tmp;
else if (tmp.s <= ip[] && ip[] <= tmp.t) {
if (vecLen(tmp.s - ip[]) > EPS)
// seg.push(Seg(tmp.s, ip[0]));
qseg[qt++] = Seg(tmp.s, ip[]);
if (vecLen(tmp.t - ip[]) > EPS)
// seg.push(Seg(ip[1], tmp.t));
qseg[qt++] = Seg(ip[], tmp.t);
} else if (tmp.s < ip[] && ip[] <= tmp.t && sgn(vecLen(tmp.s - ip[])))
// seg.push(Seg(tmp.s, ip[0]));
qseg[qt++] = Seg(tmp.s, ip[]);
else if (tmp.s <= ip[] && ip[] < tmp.t && sgn(vecLen(ip[] - tmp.t)))
// seg.push(Seg(ip[1], tmp.t));
qseg[qt++] = Seg(ip[], tmp.t);
} else
// seg.push(tmp);
qseg[qt++] = tmp;
}
// if (seg.size() == 0)
if (qh == qt)
return vecLen(vex[pi] - vex[pj]);
}
return -1.0;
} void PRE(int n) {
vex.clear();
for (int i = ; i < n; i++) {
cin >> dev[i].c.x >> dev[i].c.y >> dev[i].r;
// vex.push_back(dev[i].c);
}
vex.push_back(dev[].c);
vex.push_back(dev[n - ].c);
Point ps = dev[].c, pt = dev[n - ].c;
for (int i = ; i < n; i++) {
for (int j = ; j < i; j++) {
// cout << i << " = = " << j << endl;
circleCircleIntersect(dev[i], dev[j], vex);
}
}
sort(vex.begin(), vex.end());
int t = (int) (unique(vex.begin(), vex.end()) - vex.begin());
while (vex.size() > t) vex.pop_back();
// cout << "size " << vex.size() << endl;
for (int i = , sz = vex.size(); i < sz; i++) {
if (vex[i] == ps) sid = i;
if (vex[i] == pt) tid = i;
for (int j = ; j <= i; j++) {
if (i == j) mat[i][j] = 0.0;
else mat[i][j] = mat[j][i] = cal(i, j, n);
}
}
// cout << sid << ' ' << tid << endl;
// for (int i = 0, sz = vex.size(); i < sz; i++) {
// vex[i].print();
// for (int j = 0; j < sz; j++) printf("%7.4f ", mat[i][j]);
// cout << endl;
// }
} const double FINF = 1e100;
double dis[N * N];
int q[N * N << ];
double spfa() {
int sz = vex.size();
// queue<int> Q;
// while (!Q.empty()) Q.pop();
int qh, qt;
qh = qt = ;
for (int i = ; i < sz; i++) dis[i] = FINF;
dis[sid] = 0.0;
// Q.push(sid);
q[qt++] = sid;
// while (!Q.empty()) {
while (qh < qt) {
// int cur = Q.front();
// Q.pop();
int cur = q[qh++];
for (int i = ; i < sz; i++) {
if (mat[cur][i] < ) continue;
if (dis[i] > dis[cur] + mat[cur][i]) {
dis[i] = dis[cur] + mat[cur][i];
// Q.push(i);
q[qt++] = i;
}
}
}
// for (int i = 0; i < sz; i++) cout << dis[i] << ' '; cout << endl;
return dis[tid];
} int main() {
// freopen("in", "r", stdin);
// freopen("out", "w", stdout);
int T, n;
cin >> T;
for (int cas = ; cas <= T; cas++) {
cin >> n;
PRE(n);
printf("Case %d: ", cas);
double ans = spfa();
if (ans < FINF) printf("%.4f\n", ans);
else puts("No such path.");
}
return ;
}
附加自己出的数据:
8
2
0 0 1
2 0 1
2
0 0 1
4 1 2
3
-3 0 2
0 2 2
3 0 2
3
-3 0 2
0 3 3
3 0 2
6
-6 0 2
-3 0 2
0 3 3
3 0 2
7 0 2
0 8 2
10
-6 0 2
-3 0 2
0 3 3
3 0 2
7 0 2
0 8 2
-4 8 2
-8 8 2
-7 2 2
-8 4 2
3
0 10 7
-1 -1 100
10 0 7
3
-3 0 2
0 3 3
3 0 2
——written by Lyon
hdu 4063 Aircraft (Geometry + SP)的更多相关文章
- HDU 4063 Aircraft(计算几何)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4063 Description You are playing a flying game. In th ...
- HDU 4063 Aircraft --几何,最短路
题意: 给一些圆,要求从第一个圆的圆心走到最后一个圆的圆心,中间路径必须在某个圆内,求最短路径的长度. 解法: 易知要保持在圆内且路径最短,走两圆相交的点能使路径尽量短,所以我们找出所有的两圆相交的点 ...
- hdu 4063 Aircraft(计算几何+最短路)
不说了...说多了都是泪...从昨天下午一直wa到现在,直到刚刚才让人帮我找到所谓的“bug”,其实也算不上bug... 这个题的思路就是:找出平面上的所有点:所有圆的交点以及所有圆的圆心.然后依次判 ...
- HDU 5298 Solid Geometry Homework 暴力
Solid Geometry Homework 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5298 Description Yellowstar ...
- hdu 4063 福州赛区网络赛 圆 ****
画几个图后,知道路径点集一定是起点终点加上圆与圆之间的交点,枚举每两个点之间是否能走,能走则连上线,然后求一遍最短路即可 #include<cstdio> #include<cstd ...
- HDU 4063 线段与圆相交+最短路
Aircraft Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- HDU - 6242:Geometry Problem(随机+几何)
Alice is interesting in computation geometry problem recently. She found a interesting problem and s ...
- hdu 4179 Difficult Routes (SP)
Problem - 4179 坑了我一个晚上的SP题. 题意是,给出若干空间中的点,给出其中某些点之间是有直线线段路径相连的.要求求出一条从s开始到t结束的路径,它的难度是d.难度的计算是空间线段两点 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
随机推荐
- oracle习题-emp表查询练习
emp表查询练习 1 查询emp表的全部记录 Select * from emp; 2 查询出每个雇员的编号.姓名.基本工资 Select empno,ename,sal from emp; 3 查询 ...
- 【等价的穿越】Burnside引理&Pólya计数法
Problem 起源: SGU 294 He's Circle 遗憾的是,被吃了. Poj有道类似的: Mission 一个长度为n(1≤n≤24)的环由0,1,2组成,求有多少本质不同的环. 实际上 ...
- php安装oci8和pdo_oci扩展实现连接oracle数据库
PHP一般跟MySQL数据库搭配使用,但最近遇到一个需求需要实现PHP连接Oracle,了解到PHP可以通过pdo_oci和oci8扩展来连接Oracle,这里将安装的过程记录下来. 安装环境:PHP ...
- vue-cli3 关闭eslint
关闭eslint 直接注释掉package.json文件中eslint的配置就可以了(以下是vue-cli的默认配置) "eslintConfig": { "root&q ...
- JQuery--mouseover()与moseout()的区别
mouseover()与mouseout()区别 普通鼠标移入移出事件 语法: mouseover()/mouseout() 功能: 当鼠标移入/移出到添加事件的元素或其子元素的时候,都会被触发!!m ...
- yum源配置及详解
红帽系列中,进行软件安装可以有三种方法,编译安装,rpm包安装,和yum源安装.其中yum方法安装最简单,因为它可以自动解决软件包之间的依赖关系... 一.常用yum源 yum源可以来源于多种文件 ...
- 【JZOJ4805】【NOIP2016提高A组模拟9.28】跟踪
题目描述 输入 输出 样例输入 4 2 1 3 1 2 2 3 3 4 样例输出 2 数据范围 解法 预处理出两个陌生人走到各个点的距离. 从石神处开始dfs,判断走到每一个点是否会被抓: 如果会,则 ...
- mac下的抓包工具Charles
在mac下面,居然没有好的抓包工具,这让我十分纠结,毕竟不可能为了抓一个http包就跑到win下折腾.或许有人说tcpdump这么好的工具,你怎么不用.说实话,tcpdump太复杂了,我还没有细看,再 ...
- photoshop正确的打开方式
首先这边我先贴一个地址:https://www.adobe.com/cn/products/photoshop.html 安装软件,这里就不赘述了,真的不会,可以百度^_^我当初就是百度的,哈哈 说到 ...
- Servlet接口
ServletRequest接口 ServletRequest的对象用于向Servlet提供客户端请求信息,如内容类型,内容长度,参数名称和值,标题信息,属性等. RequestDispatcher接 ...