Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists' sunscreen, he wants to avoid swimming and instead reach her by jumping.
Unfortunately Fiona's stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps.
To execute a given sequence of jumps, a frog's jump range obviously must be at least as long as the longest jump occuring in the sequence.
The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones.
You are given the coordinates of Freddy's stone, Fiona's stone and all other stones in the lake. Your job is to compute the frog distance between Freddy's and Fiona's stone.
Unfortunately Fiona's stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps.
To execute a given sequence of jumps, a frog's jump range obviously must be at least as long as the longest jump occuring in the sequence.
The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones.
You are given the coordinates of Freddy's stone, Fiona's stone and all other stones in the lake. Your job is to compute the frog distance between Freddy's and Fiona's stone.
The input will contain one or more test cases. The first line of each test case will contain the number of stones n (2<=n<=200). The next n lines each contain two integers xi,yi (0 <= xi,yi <= 1000) representing the coordinates of stone #i. Stone #1 is Freddy's stone, stone #2 is Fiona's stone, the other n-2 stones are unoccupied. There's a blank line following each test case. Input is terminated by a value of zero (0) for n.
For each test case, print a line saying "Scenario #x" and a line saying "Frog Distance = y" where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three decimals. Put a blank line after each test case, even after the last one.
2 0 0 3 4 3 17 4 19 4 18 5 0
Scenario #1 Frog Distance = 5.000 Scenario #2 Frog Distance = 1.414
简单的最短路问题的水题
两种方法都可以做它
第一种迪杰斯特拉方法(这种比较麻烦一点)
double Map[205][205]; double dis[205]; int vis[205]; struct node { double u,v; } ende[205]; int n; void dijst() { for(int i=1;i<=n;i++) { int r; double mi=INF; for(int j=1;j<=n;j++) { if(!vis[j]&&dis[j]<mi) { r=j; mi=dis[j]; } } vis[r]=1; for(int j=1;j<=n;j++) { if(!vis[j]&&dis[j]>max(dis[r],Map[r][j])) { dis[j]=max(dis[r],Map[r][j]); } } } } int main() { int l=0; while(~scanf("%d",&n)) { if(n==0) break;
for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) if(i==j) Map[i][j]=0; else Map[i][j]=INF;
这一步必须要有,如果没有的话就会出现错误。。 for(int i=1; i<=n; i++) scanf("%lf%lf",&ende[i].u,&ende[i].v); for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) Map[i][j]=sqrt((ende[i].u-ende[j].u)*(ende[i].u-ende[j].u)+
(ende[i].v-ende[j].v)*(ende[i].v-ende[j].v)); for(int i=1; i<=n; i++) dis[i]=Map[1][i],vis[i]=0; dijst(); printf("Scenario #%d\n",++l); printf("Frog Distance = %.3lf\n",dis[2]); printf("\n"); } }
另一种就是用弗洛伊德算法,找出最短的路径。我比较欣赏这种方法简单好记,但是时间复杂度有限。
好用但是也别忘了会超时、
struct node { double x,y; }s[1000]; int main() { int T,t=0; while(~scanf("%d",&T)) { if(T==0) break; t++; for(int i=0;i<T;i++) { scanf("%lf%lf",&s[i].x,&s[i].y); //printf("%lf%lf\n",s[i].x,s[i].y); } for(int i=1;i<=T;i++) for(int j=1;j<=T;j++) { if(i==j) Map[i][j]=0; else Map[i][j]=Map[j][i]=INF; } for(int i=0;i<T;i++) { for(int j=i;j<T;j++) { Map[i][j]=Map[j][i]=sqrt((s[i].x-s[j].x)*(s[i].x-s[j].x)+(s[i].y-s[j].y)*(s[i].y-s[j].y)); } } for(int k=0;k<T;k++) { for(int i=0;i<T;i++) { for(int j=0;j<T;j++) { if(Map[i][j]>max(Map[i][k],Map[k][j])) Map[i][j]=max(Map[i][k],Map[k][j]); } } } // for(int i=0;i<T;i++) // { // for(int j=0;j<T;j++) // { // printf("i=%d j=%d %.3lf ",i,j,Map[i][j]); // } // printf("\n"); // } printf("Scenario #%d\n",t); printf("Frog Distance = %.3lf\n",Map[0][1]); printf("\n"); } }