dijstra算法的变形,定义:dist[i]为源点到点i的若干路径上的边的最大值的最小值,然后会发现可以用和dijstra一样的贪心方法,当前dist最小的以后都不会再被更新。
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cmath> 5 using namespace std; 6 7 const int INF = 9999999; 8 const int N = 201; 9 int head[N]; 10 int dist[N]; 11 bool visit[N]; 12 int n, e; 13 14 struct Node 15 { 16 int x, y; 17 } node[N]; 18 19 struct Edge 20 { 21 int v, next, w; 22 } edge[N * N]; 23 24 void addEdge( int u, int v, int w ) 25 { 26 edge[e].v = v; 27 edge[e].w = w; 28 edge[e].next = head[u]; 29 head[u] = e++; 30 } 31 32 int dis( int i, int j ) 33 { 34 return ( node[i].x - node[j].x ) * ( node[i].x - node[j].x ) + 35 ( node[i].y - node[j].y ) * ( node[i].y - node[j].y ); 36 } 37 38 void dij( int s ) 39 { 40 memset( visit, false, sizeof(visit) ); 41 for ( int i = 0; i <= n; i++ ) 42 { 43 dist[i] = INF; 44 } 45 dist[s] = 0; 46 for ( int i = 1; i <= n; i++ ) 47 { 48 int u = 0; 49 for ( int j = 1; j <= n; j++ ) 50 { 51 if ( !visit[j] && dist[j] < dist[u] ) 52 { 53 u = j; 54 } 55 } 56 visit[u] = true; 57 for ( int j = head[u]; j != -1; j = edge[j].next ) 58 { 59 int v = edge[j].v, w = edge[j].w; 60 if ( visit[v] ) continue; 61 dist[v] = min( dist[v], max( dist[u], w ) ); 62 } 63 } 64 } 65 66 int main () 67 { 68 int _case = 1; 69 while ( scanf("%d", &n), n ) 70 { 71 e = 0; 72 memset( head, -1, sizeof(head) ); 73 for ( int i = 1; i <= n; i++ ) 74 { 75 scanf("%d%d", &node[i].x, &node[i].y); 76 for ( int j = i - 1; j > 0; j-- ) 77 { 78 int tmp = dis( i, j ); 79 addEdge( i, j, tmp ); 80 addEdge( j, i, tmp ); 81 } 82 } 83 dij(1); 84 double ans = sqrt( dist[2] * 1.0 ); 85 printf("Scenario #%d Frog Distance = %.3lf ", _case++, ans); 86 } 87 return 0; 88 }