题目链接:
https://vjudge.net/problem/POJ-2253
题目大意:
青蛙A想访问青蛙B,必须跳着石头过去,不幸的是,B所在的石头太远了,需要借助其他的石头,求从A到B的路径中,青蛙最少需要的跳跃能力是多远
思路:
理清题意,这里规定的是每条路中的最大边为青蛙需要的跳跃能力,要求这个跳跃能力的最小值,思路和POJ2263一样POJ2263求的是最小边的最大值,这里求的是最大边的最小值,同样是松弛方程改变一下就可以,三种算法均可,这里附上Floyd和dijkstra
Floyd:188ms
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 #include<queue> 7 #include<stack> 8 #include<map> 9 #include<set> 10 #include<sstream> 11 #define MEM(a, b) memset(a, b, sizeof(a)); 12 using namespace std; 13 typedef long long ll; 14 const int maxn = 200 + 10; 15 const int INF = 0x3f3f3f3f; 16 int T, n, m, cases, tot; 17 double Map[maxn][maxn]; 18 struct node 19 { 20 double x, y; 21 }; 22 node a[maxn]; 23 int main() 24 { 25 while(cin >> n && n) 26 { 27 MEM(a, 0); 28 MEM(Map, 0); 29 for(int i = 1; i <= n; i++) 30 { 31 cin >> a[i].x >> a[i].y; 32 } 33 for(int i = 1; i <= n; i++) 34 { 35 for(int j = 1; j <= n; j++) 36 Map[i][j] = sqrt((a[i].x - a[j].x) * (a[i].x - a[j].x) + (a[i].y - a[j].y) * (a[i].y - a[j].y)); 37 } 38 for(int k = 1; k <= n; k++) 39 { 40 for(int i = 1; i <= n; i++) 41 { 42 for(int j = 1; j <= n; j++) 43 { 44 Map[i][j] = min(Map[i][j], max(Map[i][k], Map[k][j])); 45 } 46 } 47 } 48 printf("Scenario #%d\n", ++cases); 49 printf("Frog Distance = %.3f\n\n", Map[1][2]); 50 } 51 return 0; 52 }
dijkstra:47ms
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 #include<queue> 7 #include<stack> 8 #include<map> 9 #include<set> 10 #include<sstream> 11 #define MEM(a, b) memset(a, b, sizeof(a)); 12 using namespace std; 13 typedef long long ll; 14 const int maxn = 200 + 10; 15 const int INF = 0x3f3f3f3f; 16 int T, n, m, cases, tot; 17 double Map[maxn][maxn]; 18 struct node 19 { 20 double x, y; 21 }; 22 node a[maxn]; 23 bool v[maxn]; 24 double d[maxn]; 25 void dijkstra(int u) 26 { 27 memset(v, 0, sizeof(v)); 28 for(int i = 0; i <= n; i++)d[i] = INF; 29 d[u] = 0; 30 for(int i = 1; i <= n; i++) 31 { 32 int x, m = INF; 33 for(int i = 1; i <= n; i++)if(!v[i] && d[i] <= m)m = d[x = i];//找最小值 34 v[x] = 1; 35 for(int i = 1; i <= n; i++)d[i] = min(d[i], max(d[x], Map[x][i])); 36 } 37 } 38 int main() 39 { 40 while(cin >> n && n) 41 { 42 MEM(a, 0); 43 MEM(Map, 0); 44 for(int i = 1; i <= n; i++) 45 { 46 cin >> a[i].x >> a[i].y; 47 } 48 for(int i = 1; i <= n; i++) 49 { 50 for(int j = 1; j <= n; j++) 51 Map[i][j] = sqrt((a[i].x - a[j].x) * (a[i].x - a[j].x) + (a[i].y - a[j].y) * (a[i].y - a[j].y)); 52 } 53 dijkstra(1); 54 printf("Scenario #%d\n", ++cases); 55 printf("Frog Distance = %.3f\n\n", d[2]); 56 } 57 return 0; 58 }