## 分析

$n$对应的是横坐标不是纵坐标。

## 代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <queue>
#define rin(i,a,b) for(int i=(a);i<=(b);i++)
#define rec(i,a,b) for(int i=(a);i>=(b);i--)
typedef long long LL;
using std::cin;
using std::cout;
using std::endl;

int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*f;
}

const int MAXN=605;
int s,dis[MAXN];
double R,C,sx,sy;
bool vis[MAXN],invalid[MAXN];
struct Edge{
int to,nxt;
}e[MAXN*MAXN];

ecnt++;
e[ecnt].to=ed;
}

struct Po{
double x,y;
inline friend Po operator + (Po A,Po B){return (Po){A.x+B.x,A.y+B.y};}
inline friend Po operator - (Po A,Po B){return (Po){A.x-B.x,A.y-B.y};}
inline friend Po operator * (Po A,double B){return (Po){A.x*B,A.y*B};}
inline friend Po operator / (Po A,double B){return (Po){A.x/B,A.y/B};}
}re[MAXN];
inline int dcmp(double A,double B){return fabs(A-B)<1e-8?0:((A>B)*2)-1;}
inline Po getmid(Po A,Po B){return (Po){(A.x+B.x)/2,(A.y+B.y)/2};}
typedef Po Ve;
inline double getlen(Ve A){return sqrt(A.x*A.x+A.y*A.y);}
inline double getcross(Ve A,Ve B){return A.x*B.y-A.y*B.x;}
inline Ve getnor(Ve A){return (Ve){-A.y,A.x}/getlen(A);}
struct Li{
Po u;Ve v;int id;double ang;
inline friend bool operator < (Li A,Li B){return dcmp(A.ang,B.ang)<0;}
inline Po getp(double t){return u+v*t;}
}l[MAXN],que[MAXN];
inline Po getinter(Li A,Li B){return A.getp(getcross(B.v,A.u-B.u)/getcross(A.v,B.v));}
inline Li getpb(Po A,Po B,int id){return (Li){getmid(A,B),getnor(B-A),id,0};}
inline bool isleft(Po A,Li B){return dcmp(getcross(B.v,A-B.u),0)>0;}

inline void solve(int x){
lcnt=0;
l[++lcnt]=(Li){(Po){0,0},(Ve){1,0},n+1,0},l[lcnt].ang=std::atan2(l[lcnt].v.y,l[lcnt].v.x);
l[++lcnt]=(Li){(Po){C,0},(Ve){0,1},n+1,0},l[lcnt].ang=std::atan2(l[lcnt].v.y,l[lcnt].v.x);
l[++lcnt]=(Li){(Po){C,R},(Ve){-1,0},n+1,0},l[lcnt].ang=std::atan2(l[lcnt].v.y,l[lcnt].v.x);
l[++lcnt]=(Li){(Po){0,R},(Ve){0,-1},n+1,0},l[lcnt].ang=std::atan2(l[lcnt].v.y,l[lcnt].v.x);
rin(i,1,n) if(!invalid[i]) if(i!=x) l[++lcnt]=getpb(re[x],re[i],i),l[lcnt].ang=std::atan2(l[lcnt].v.y,l[lcnt].v.x);
std::sort(l+1,l+lcnt+1);
hd=1,tl=0;
rin(i,1,lcnt){
while(hd<tl&&!isleft(getinter(que[tl-1],que[tl]),l[i])) tl--;
while(hd<tl&&!isleft(getinter(que[hd],que[hd+1]),l[i])) hd++;
if(hd<=tl&&dcmp(getcross(l[i].v,que[tl].v),0)==0){
if(isleft(l[i].u,que[tl])) que[tl]=l[i];
}
else que[++tl]=l[i];
}
while(hd<tl&&!isleft(getinter(que[tl-1],que[tl]),que[hd])) tl--;
}

std::queue<int> bfq;
inline void bfs(){
memset(dis,0x3f,sizeof dis);
memset(vis,0,sizeof vis);
while(!bfq.empty()) bfq.pop();
vis[s]=1,dis[s]=1,bfq.push(s);
while(!bfq.empty()){
int x=bfq.front();bfq.pop();
trav(i,x){
int ver=e[i].to;
if(vis[ver]) continue;
vis[ver]=1,dis[ver]=dis[x]+1;
if(ver==n+1) return;
bfq.push(ver);
}
}
}

int main(){
while(T--){
s=0;
memset(invalid,0,sizeof invalid);
if(n==0){printf("0\n");continue;}
rin(i,1,n){
if(re[i].x>=C||re[i].y>=R) invalid[i]=1;
}
rin(i,1,n) if(!invalid[i]) solve(i);
rin(i,1,n) if(!invalid[i]) if(!s||dcmp(getlen(re[i]-(Po){sx,sy}),getlen(re[s]-(Po){sx,sy}))<0) s=i;
bfs();
printf("%d\n",dis[n+1]-1);
}
return 0;
}


posted on 2018-12-19 13:53  ErkkiErkko  阅读(106)  评论(0编辑  收藏

• 随笔 - 91
• 文章 - 0
• 评论 - 20