【测试】【最短路】图论专题训练--智捅马蜂窝
题目:智捅马蜂窝(hornet.pas/cpp/in/out)
题目描述
背景
为了统计小球的方案数,平平已经累坏了。于是,他摘掉了他那800度的眼镜,躺在树下休息。
后来,平平发现树上有一个特别不一样的水果,又累又饿的平平打算去把它摘下来。
题目描述
现在,将大树以一个N个节点的无向图的形式给出,每个节点用坐标(Xi,Yi)来表示表示,平平要从第一个点爬到第N个点,除了从一个节点爬向另一个相邻的节点以外,他还有一种移动方法,就是从一个节点跳下,到达正下方的某个节点(之间可隔着若干个点和边),即当Xj=Xi and Yi<Yj 时,平平就可以从j节点下落到i节点,他下落所用时间满足自由落体公式,t=sqrt((Yj-Yi)*2/g) (注意:g取10)。如果出现两线相交的情况,我们不认为它们是相通的。
数据规模
对于100%数据,1<=N<=100,1<=V<=10,0<=X,Y<=100.
建议使用extended(pas)或double(c and c++)计算,我们对于精度造成的误差将不予重测。
输入格式
两个整数N,V,N表示节点个数,V表示平平爬树的速度。
接下来N行,每行包含3个整数X,Y,F,X,Y是这个点的坐标,F是他的父节点(F一定小于这个点的标号,第一行的F为0)。
注意:两节点间距离按欧几里德距离计算 dis = sqrt( ( x1 – x2 ) 2+ ( y1 – y2 )2 )
输出格式
输出仅包括一行,从1到N所用的最少所需时间T,保留两位小数。
样例输入
9 1
5 0 0
5 5 1
6 5 2
7 6 2
6 9 2
3 6 2
4 5 2
3 2 7
7 2 3
样例输出
8.13
题目很简单,读入的时候建图没问题,然后需要一次O(n2)的循环来找出所有能够自由下落的点,然后注意,一定要更优才更新值!!!
program hornet; const maxn=100+100; var n,v:longint; x,y,f:array[0..maxn] of longint; map:array[0..maxn,0..maxn] of extended;//记录时间 dist:array[0..maxn] of extended; h:array[0..maxn] of boolean; procedure init; begin assign(input,'hornet.in'); assign(output,'hornet.out'); reset(input); rewrite(output); end; procedure outit; begin close(input); close(output); halt; end; function dis(i,j:longint):extended; begin exit(sqrt(sqr(x[i]-x[j])+sqr(y[i]-y[j]))); end; procedure readdata; var i,j:longint; begin read(n,v); for i:=1 to n do//处理相连情况 begin read(x[i],y[i],f[i]); if f[i]=0 then continue; map[i,f[i]]:=dis(i,f[i])/v; map[f[i],i]:=dis(i,f[i])/v; end; for i:=1 to n do//处理自由落体 for j:=1 to n do begin if i=j then continue; if (x[i]=x[j])and(y[i]>y[j]) then//自由落体 i --> j begin map[i,j]:=sqrt((2*(y[i]-y[j]))/10); end; end; end; procedure dijkstra; var i,k,j:longint; total,min:extended; begin total:=0; for i:=1 to n do begin k:=0;min:=maxlongint; for j:=1 to n do begin if (not h[j])and(min>dist[j]) then begin k:=j;min:=dist[j]; end; end; h[k]:=true; for j:=1 to n do begin if (not h[j])and(map[k,j]<>0)and(dist[j]>dist[k]+map[k,j]) then dist[j]:=dist[k]+map[k,j]; end; end; writeln(dist[n]:0:2); end; procedure main; var i,k,j:longint; begin for i:=1 to n do dist[i]:=1e10; dist[1]:=0; dijkstra; end; begin init; readdata; main; outit; end.