标签:
题目链接:http://poj.org/problem?id=2253
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 38366 | Accepted: 12357 |
Description
Input
Output
Sample Input
2 0 0 3 4 3 17 4 19 4 18 5 0
Sample Output
Scenario #1 Frog Distance = 5.000 Scenario #2 Frog Distance = 1.414
题目大意:input 第一行的n代表 在一个湖中有n个石头,接下来是每个石头的坐标,Freddy在第一个石头上,Fiona在第二个石头上,找出一条最短路径使的
Freddy跳到Fiona所在的石头上,输出这条路径中他需要跳的最远的一次的距离。
解题思路: 迪杰斯特拉算法的应用 将dis数组中的值变成最短路径中的最大权值
AC代码:
1 #include <stdio.h> 2 #include <string.h> 3 #include <math.h> 4 #include <algorithm> 5 #include <iostream> 6 #include <iomanip> 7 using namespace std; 8 #define inf 999999999 9 double dis[220]; 10 int visit[220]; 11 double p[220][220]; 12 int n; 13 void dijkstra() 14 { 15 int i,j,pos = 0,minn; 16 memset(visit,0,sizeof(visit)); 17 visit[1] = 1; 18 dis[1] = 0; 19 for (i = 2; i <= n; i ++) 20 dis[i] = p[1][i]; 21 22 for (i = 1; i <= n; i ++) 23 { 24 minn = inf; 25 for (j = 1; j <= n; j ++) 26 { 27 if (!visit[j] && dis[j] < minn) 28 { 29 minn = dis[j]; 30 pos = j; 31 } 32 } 33 visit[pos] = 1; 34 if (pos == 2) 35 break; 36 for (j = 1; j <= n; j ++) //将dis数组中的值变成最短路径中的最大权值 37 { 38 if (!visit[j] && dis[j] > max(dis[pos],p[pos][j])) 39 dis[j] = max(dis[pos],p[pos][j]); 40 } 41 } 42 } 43 int main () 44 { 45 double x[220],y[220]; 46 int i,j,f = 1; 47 while (~scanf("%d",&n)) 48 { 49 if (n == 0) 50 break; 51 for (i = 1; i <= n; i ++) 52 for (j = 1; j <= n; j ++) 53 p[i][j] = inf; 54 55 for (i = 1; i <= n; i ++) 56 scanf("%lf%lf",x+i,y+i); 57 58 for (i = 1;i < n; i ++) 59 { 60 for (j = i+1; j <= n; j ++) 61 { 62 double xx = x[i]-x[j]; 63 double yy = y[i]-y[j]; 64 p[j][i] = p[i][j] = (double)sqrt(xx*xx+yy*yy); 65 } 66 } 67 dijkstra(); 68 printf("Scenario #%d\n",f ++); 69 //printf("Frog Distance = %.3lf\n",dis[2]); //不知道为什么这种输出一直WA 下面的AC 70 cout<<fixed<<setprecision(3)<<"Frog Distance = "<<dis[2]<<endl; //setprecision需要加上头文件#include <iomanip> 71 cout<<endl; 72 } 73 }
标签:
原文地址:http://www.cnblogs.com/yoke/p/5862501.html