The Doors · znzryb
## 思路讲解

可以用dij做。搜索做不太了。
先建边,然后再跑dij,会快一些。
```cpp
auto check=[&](Line a){
int idx=lower_bound(all(A),Line(a.a,Point(INF,INF)))-A.begin();
bool ok=true;
FOR(i,idx,SZ(A)-1){
Line t=A[i];
if(sgn(t.a.x()-a.b.x())>=0) break;
if(isseginterline(t, a)){
ok=false;
break;
}
}
return ok;
};
vb vis(node.size()+5);
vector<vector<pair<ld,int> > > g(SZ(node)+5);
priority_queue<DIS> pq;
pq.EM(0,0);
FOR(i,0,SZ(node)-1){
FOR(j,i+1,SZ(node)-1){
if(node[i].x()==node[j].x()){
continue;
}
Point a=node[i],b=node[j];
if(check(Line(a,b))){
g[i].emplace_back(abs(a-b),j);
}
}
}
```
## AC代码
[https://vjudge.net/solution/63271204](https://vjudge.net/solution/63271204)
::: 源代码
```cpp
// by Gospel_rock
#include <algorithm>
#include <bits/stdc++.h>
#include <queue>
#include <utility>
#include <vector>
#define all(vec) vec.begin(),vec.end()
#define PB push_back
#define EB emplace_back
#define EM emplace
#define FI first
#define SE second
#define pct __builtin_popcountll
#define ctz __builtin_ctzll
#define SZ(a) ((long long) a.size())
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define debug(var) cerr << #var <<":"<<var<<"\n";
#define cend cerr<<"\n-----------\n"
#define fsp(x) fixed<<setprecision(x)
#define minn(vec) *min_element(vec.begin(),vec.end())
#define maxx(vec) *max_element(vec.begin(),vec.end())
using namespace std;
#ifdef LOCAL
template<typename T> void _print(T x) { cerr << x; }
// 可以打印多层数组,但是格式不是很好
template<typename T> void _print(vector<T> v) { cerr << "[ "; for (T i : v) _print(i), cerr << " "; cerr << "]"; }
template<typename T> void _print(set<T> s) { cerr << "{ "; for (T i : s) _print(i), cerr << " "; cerr << "}"; }
template<typename T, typename U> void _print(pair<T, U> p) { cerr << "{"; _print(p.first); cerr << ", "; _print(p.second); cerr << "}"; }
template<typename T, typename U> void _print(map<T, U> m) { cerr << "{ "; for (auto &p : m) _print(p), cerr << " "; cerr << "}"; }
// 打印两层数组,格式好一些
template<typename T> void _print(vector<vector<T>> v){cerr<<"{\n";for (auto i : v) _print(i), cerr << "\n";cerr<<" }";}
#endif
using ll = long long;using ull = unsigned long long;
using ld=double;using LD=long double;
using i128 = __int128;
using pdd = pair<ld,ld>;using plb = pair<ll,bool>;
using pll = pair<ll,ll>;using vll=vector<ll>;using vb=vector<bool>;
using tll = tuple<ll,ll,ll>;using vii=vector<int>;
using vpll = vector<pll>;using vtll = vector<tll>;
using vdb = vector<ld>;using vc=vector<char>;
using arr3 = array<ll,3> ;using arr2 = array<ll,2>;
using CD=complex<double>;
static constexpr ll MAXN=(ll)1e6+10,INF=(ll)1e17+3; // 1e17+3是素数 1e18+9是素数
static constexpr ll mod=(ll)1e9+7; // 998244353;
static constexpr double eps=1e-8;const double pi=acos(-1.0);
ll N,M,Q,X,K,T,lT;
inline void __();
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
// cin>>lT;
while(cin>>N)
__();
return 0;
}
/*
*/
using Real = ld;
using Point = complex<Real>;
#define x() real()
#define y() imag()
Real cross(const Point &a, const Point &b) {
return (conj(a) * b).imag();
}
Real dot(const Point &a, const Point &b) {
return (conj(a) * b).real();
}
int sgn(Real x) {
if (-eps < x && x < eps) return 0;
return x < 0 ? -1 : 1;
}
namespace std
{
bool operator <(const Point& a, const Point& b){
return sgn(real(a) - real(b)) ? real(a) < real(b) : imag(a) <
imag(b);
}
bool operator ==(const Point& a, const Point& b){
if(!sgn(a.imag()-b.imag()) && !sgn(a.real()-b.real())){
return true;
}
return false;
}
bool operator !=(const Point& a, const Point& b){
if(!sgn(a.imag()-b.imag()) && !sgn(a.real()-b.real())){
return false;
}
return true;
}
}
istream& operator>>(istream& in, Point& c) {
double r, i;
in >> r >> i;
c = Point(r, i);
return in;
}
// 采用两点式
struct Line{
Point a,b;
Line(){}
Line(Point a,Point b):a(a),b(b) {}
bool operator<(const Line&other) const{
return a!=other.a?a<other.a:b<other.b;
}
bool operator==(const Line&other) const{
return a==other.a && b==other.b;
}
};
// p+tv=q+sw
// (p-q) x w+tv x w = 0
// t=-(p-q)x w/(vxw)
// 传入的是方向式 拿到的是两直线交点
Point getintersect(const Line &a,const Line &b){
Point p=a.a, v=a.b-a.a,q=b.a,w=b.b-b.a;
Point u=p-q;
ld t=cross(w,u)/cross(v,w);
return p+v*t;
}
bool isseginterline(Line seg,Line line){
if(sgn(cross(line.a-seg.a, line.b-seg.a))*sgn(cross(line.a-seg.b, line.b-seg.b))>0){
return false;
}
return true;
}
struct DIS{
ld dis;
int idx;
bool operator<(const DIS &other) const{
return dis>other.dis;
}
DIS(ld dis,int idx):dis(dis),idx(idx) {}
};
inline void __(){
if(N==-1) return;
vector<Line> A;
vector<Point> node={Point(0,5)};
FOR(i,1,N){
ld x,y1,y2,y3,y4;
cin>>x>>y1>>y2>>y3>>y4;
Point a(x,0),b(x,y1);
A.emplace_back(a,b);
node.emplace_back(b);
Point a2(x,y2),b2(x,y3);
A.emplace_back(a2,b2);
node.emplace_back(a2);
node.emplace_back(b2);
Point a3(x,y4),b3(x,10);
A.emplace_back(a3,b3);
node.emplace_back(a3);
}
ld ans=INF;
Point dest(10,5);
node.EB(dest);
auto check=[&](Line a){
int idx=lower_bound(all(A),Line(a.a,Point(INF,INF)))-A.begin();
bool ok=true;
FOR(i,idx,SZ(A)-1){
Line t=A[i];
if(sgn(t.a.x()-a.b.x())>=0) break;
if(isseginterline(t, a)){
ok=false;
break;
}
}
return ok;
};
vb vis(node.size()+5);
vector<vector<pair<ld,int> > > g(SZ(node)+5);
priority_queue<DIS> pq;
pq.EM(0,0);
FOR(i,0,SZ(node)-1){
FOR(j,i+1,SZ(node)-1){
if(node[i].x()==node[j].x()){
continue;
}
Point a=node[i],b=node[j];
if(check(Line(a,b))){
g[i].emplace_back(abs(a-b),j);
#ifdef LOCAL
debug(j);debug(a);debug(b);
#endif
}
}
}
vector<ld> Dis(SZ(node)+5);
while (!pq.empty()) {
auto [dis,ind]=pq.top();pq.pop();
#ifdef LOCAL
debug(dis);
debug(ind);
#endif
// if(vis.count(a)) continue;
// vis.insert(a);
Point a=node[ind];
if(vis[ind]){
continue;
}
vis[ind]=true;
Dis[ind]=dis;
for(auto [w,v]:g[ind]){
// #ifdef LOCAL
// debug(w);debug(v);
// #endif
pq.EM(dis+w,v);
}
}
cout<<fsp(2)<<Dis[SZ(node)-1]<<"\n";
#ifdef LOCAL
cend;
#endif
}
/*
1
2
4 2 7 8 9
7 3 4.5 6 7
-1
AC
https://vjudge.net/solution/63271204
*/
```
:::
## 心路历程(WA,TLE,MLE……)