【BZOJ】【P2596】【Wc2007】【疯狂赛车】【题解】【数学+乱搞】

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2596

ydc大爷的题解写的很详细 http://ydcydcy1.blog.163.com/blog/static/21608904020140784943341/

求极值还是求导后O(1)算吧,我三分T了好几次 感谢@liangjs 的帮助

Code:

#include<bits/stdc++.h>
using namespace std;
const int maxn=2010;
const double eps=1e-6;
const double pi=acos(-1);
int n;
double va,vb;
inline int dcmp(double x){return (x>eps)-(x<-eps);}
struct point{
	double x,y;
	point(double _x=0,double _y=0):x(_x),y(_y){}
	point operator-(point o){return point(x-o.x,y-o.y);}
	point operator+(point o){return point(x+o.x,y+o.y);}
	double operator*(point o){return x*o.y-y*o.x;}
	double operator^(point o){return x*o.x+y*o.y;}
	point operator/(double p){return point(x/p,y/p);}
	point operator*(double p){return point(x*p,y*p);}
	bool operator==(point o)const{return !dcmp(x-o.x)&&!dcmp(y-o.y);}
	bool operator!=(point o)const{return !(*this==o);}
	friend bool operator<(point a,point b){return dcmp(a.x-b.x)<0||(!dcmp(a.x-b.x)&&dcmp(a.y-b.y)<0);}
}p[maxn];
inline double length(point A){return sqrt(A^A);}
inline point normal(point A){return A/length(A);}
struct edge{int u,v;double w;};
vector<edge>G[2*maxn*maxn];
inline void add(int u,int v,double w){
	G[u].push_back((edge){u,v,w});
	G[v].push_back((edge){v,u,w});
}
#define fst first
#define sec second
bool byX(pair<point,int> a,pair<point,int> b){return a.fst.x<b.fst.x;}
bool byY(pair<point,int> a,pair<point,int> b){return a.fst.y<b.fst.y;}
typedef pair<double,int> par;
short vis[2*maxn*maxn];
int cnt=0;
inline double dijk(){
	static priority_queue<par,vector<par>,greater<par> >q;
	static double d[2*maxn*maxn];
	for(int i=1;i<=cnt;i++)d[i]=1e10;
	d[1]=0;q.push(par(d[1],1));
	while(!q.empty()){
		int u=q.top().sec;q.pop();if(vis[u])continue;vis[u]=1;
		for(int i=0;i<G[u].size();i++){
			int v=G[u][i].v;
			double w=G[u][i].w;
			if(dcmp(d[v]-d[u]-w)>0){
				d[v]=d[u]+w;
				q.push(par(d[v],v));
			}
		}
	}return d[n];
}
double h,L;
inline double f(double x){
	return sqrt(h*h+(L-x)*(L-x))/vb+x/va;
}
inline bool Onseg(point a,point b,point c){
	return dcmp(a.x-b.x)*dcmp(a.x-c.x)<=0&&dcmp(a.y-b.y)*dcmp(a.y-c.y)<=0;	
}
double sqr(double x){return x*x;}
inline point Maxf(point P,point A,point B){
	double l=0,r=length(A-B);
	h=fabs(((P-A)*(B-A))/length(B-A));
	L=((P-A)^(B-A))/length(B-A);
	double x1=sqrt(sqr(vb*h)/(sqr(va)-sqr(vb)))+L;
	double x2=-sqrt(sqr(vb*h)/(sqr(va)-sqr(vb)))+L;
	l=dcmp(x2)==-1?x1:x2;
	double len=((P-A)^(B-A))/length(B-A)/length(P-A);
	point _P=A+normal(B-A)*l;
	if(Onseg(_P,A,B))return _P;
	return length(_P-B)<length(_P-A)?B:A;
}
int getint(){
	int res=0,f=1;char c=getchar();
	while(!isdigit(c))f=f==-1||c=='-'?-1:1,c=getchar();
	while(isdigit(c))res=res*10+c-'0',c=getchar();
	return res*f;
}
typedef pair<point,int> pr;
vector<pr>tmp;
int main(){
	scanf("%d",&n);n++;cnt=n;
	scanf("%lf%lf",&va,&vb);
	for(int i=2;i<=n;i++)p[i].x=getint(),p[i].y=getint();
	for(int i=1;i<=n;i++){ 
		for(int j=i+2;j<=n;j++)
		add(i,j,length(p[i]-p[j])/vb);
		if(i+1<=n)add(i,i+1,length(p[i]-p[i+1])/va);
	} 
	for(int j=1;j<n;j++){
		tmp.clear();
		point A=p[j],B=p[j+1];
		int u=j,v=j+1;
		for(int i=1;i<=n;i++)if(i!=j&&i!=j+1){
			point P=Maxf(p[i],p[j],p[j+1]);
			if(P!=p[j]&&P!=p[j+1]){
				tmp.push_back(pr(P,cnt+1+tmp.size()));
				add(i,cnt+tmp.size(),length(P-p[i])/vb);
			}
			P=Maxf(p[i],p[j+1],p[j]);
			if(P!=p[j]&&P!=p[j+1]){
				tmp.push_back(pr(P,cnt+1+tmp.size()));
				add(i,cnt+tmp.size(),length(P-p[i])/vb);			
			}
		}
		if(dcmp(A.x-B.x)){
			sort(tmp.begin(),tmp.end(),byX);
			unique(tmp.begin(),tmp.end());
			if(dcmp(A.x-B.x)==1)swap(u,v),swap(A,B);
		}else{
			sort(tmp.begin(),tmp.end(),byY);
			unique(tmp.begin(),tmp.end());
			if(dcmp(A.y-B.y)==1)swap(u,v),swap(A,B);
		}	
		if(tmp.empty())continue;
		add(u,tmp.front().sec,length(A-tmp.front().fst)/va);
		for(int i=0;i+1<tmp.size();i++){
			add(tmp[i].sec,tmp[i+1].sec,length(tmp[i].fst-tmp[i+1].fst)/va);
		}
		add(tmp.back().sec,v,length(B-tmp.back().fst)/va);
		cnt+=tmp.size();
	}
	
	printf("%.4lf\n",dijk()-0.00005);
	return 0;
}


你可能感兴趣的:(bzoj)