这题要说是最短路吧,其实更像DP,因为不需要把路径相加,而是更新为最小值。做这题最大的收获就是我才知道原来优先队列也能间接排序,自己写个cmp就行了,而且原理和sort的cmp一样。真的要感谢che学长。
1 #include <stdio.h> 2 #include <string.h> 3 #include <cmath> 4 #include <queue> 5 #include <vector> 6 using namespace std; 7 int n,m=1; 8 double d[205]; 9 bool used[205]; 10 typedef struct 11 { 12 int x,y; 13 }Point; 14 Point a[205]; 15 struct cmp 16 { 17 bool operator()(int a , int b) 18 { 19 return d[a] > d[b] ; 20 } 21 }; 22 double dis(Point d,Point b) 23 { 24 return sqrt(double(d.x-b.x)*(d.x-b.x)+(d.y-b.y)*(d.y-b.y)); 25 } 26 double max(double a,double b) 27 { 28 return a > b ?a :b ; 29 } 30 double Djstl() 31 { 32 int t,i; 33 priority_queue <int, vector<int>,cmp> q; 34 memset(used,0,sizeof(used)); 35 for(i = 1; i <= n; i++) 36 d[i] = 1000000; 37 d[1] = 0; 38 q.push(1); 39 while(!q.empty()) 40 { 41 t = q.top(); q.pop(); 42 if(used[t]) continue; 43 used[t] = 1; 44 for(i = 1; i <= n; i++) 45 if(!used[i] && max(d[t],dis(a[t],a[i])) < d[i]) 46 { 47 d[i] = max(d[t],dis(a[t],a[i])); 48 q.push(i); 49 } 50 } 51 return d[n]; 52 } 53 int main() 54 { 55 int i; 56 double ans; 57 while(scanf("%d",&n),n) 58 { 59 scanf("%d%d%d%d",&a[1].x,&a[1].y,&a[n].x,&a[n].y); 60 for(i = 2; i < n; i++) 61 scanf("%d%d",&a[i].x,&a[i].y); 62 ans = Djstl(); 63 printf("Scenario #%d\nFrog Distance = %.3lf\n\n",m++,ans); 64 } 65 return 0; 66 }