POJ 2253 Frogger dijkstra 变形

    xiaoxiao2021-12-14  21

    B - Frogger Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit  Status

    Description

    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. 

    Input

    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.

    Output

    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.

    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
    题意有点让我搞不懂啊。。。

    有N块石头,1—N。每块石头都有x,y坐标,青蛙一号站在第一块石头上,青蛙二号站在第二块石头上,青蛙一号想要通过这N块石头去找青蛙二号,因为青蛙一号可以踩在任何一块石头上,所以从第一块石头到第二块石头有很多条路径,假设为X,在每一条路径中,都有跳跃范围(即在这条路径中,两块石头之间的最大距离),那么一共有X个跳跃范围,我们要求的就是这X个跳跃范围的最小值,就是the frog distance。   比如有  有两条通路  1(4)5 (3)2 代表1到5之间的边为4,  5到2之间的边为3,那么该条通路跳跃范围(两块石头之间的最大距离)为 4,  另一条通路 1(6) 4(1) 2 ,该条通路的跳跃范围为6, 两条通路的跳跃范围分别是 4 ,6,我们要求的就是最小的那一个跳跃范围,即4.

    边的遍历和点值的更新。这个点值代表的是,从1号石头到第[i]块石头的frog distance。 用floyed算法和dijkstra算法,把更新点值的语句改动一下就可以。

    dijkstra  #include<stdio.h> #include<iostream> #include<math.h> #include<string.h> #include<algorithm> using namespace std; const int inf=99999999; int n; double dis[210]; double mp[210][210]; bool book[210]; struct node { int x,y; }q[210]; double dd(node a,node b) { return sqrt((b.y-a.y)*(b.y-a.y)+(b.x-a.x)*(b.x-a.x)); } void dijkstra(int start)//dijkstra模板 { double ans; memset(book,0,sizeof(book)); for(int i=1;i<=n;i++) dis[i]=inf;//初始的跳跃范围应该尽量大; dis[start]=0; for(int i=1;i<=n;i++) { ans=inf; int v; for(int j=1;j<=n;j++) { if(!book[j]&&dis[j]<ans)//这里每次还是选取离源点最近的点(跳跃范围最近的),仔细一想因为我们要找最小的frog dis 那么就是要使它的最大的跳跃范围尽可能的小,所以要尽量走最短的路; { ans=dis[j]; v=j; } } book[v]=1; for(int j=1;j<=n;j++) { dis[j]=min(dis[j],max(dis[v],mp[v][j]));//这里的松弛条件可能有点绕,就是每条路径中的最大距离即为跳跃范围,我们又要选取跳跃范围中最小的; } } return ; } int main() { int t=1; while(scanf("%d",&n)!=EOF){ if(n==0) break; //memset(mp,0,sizeof(mp)); //memset(q,0,sizeof(q)); for(int i=1;i<=n;i++) scanf("%d %d",&q[i].x,&q[i].y); for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) { mp[i][j]=dd(q[i],q[j]); mp[j][i]=dd(q[i],q[j]); } dijkstra(1); printf("Scenario #%d\nFrog Distance = %.3f\n\n", t++, dis[2]); } } Floyed 这个代码比较精炼。 #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #define inf 0x3f3f3f3f using namespace std; double dis[220][220]; double cal(int x1,int y1,int x2,int y2) { return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } int main(){ int n,i,j,k,t=1; int x[220],y[220]; while(scanf("%d",&n)!=EOF) { if(n==0) break; for(i=1;i<=n;i++) { scanf("%d %d",&x[i],&y[i]); } for(i=1;i<=n;i++) { for(j=i+1;j<=n;j++) dis[i][j]=dis[j][i]=cal(x[i],y[i],x[j],y[j]); } for(k=1;k<=n;k++) for(i=1;i<=n;i++) for(j=1;j<=n;j++) { dis[i][j]=min(dis[i][j],max(dis[i][k],dis[k][j])); }//这里一样,floyed原本是通过每一个点去松弛任意两点间的距离,使其变得更小,我们在这里是通过每一个点去找它的路径中最大跳跃范围并使其最小; printf("Scenario #%d\nFrog Distance = %.3f\n\n", t++, dis[1][2]); } } Marcus-Bao 认证博客专家 推荐系统 ACM算法竞赛 机器学习 本科毕业于国内知名四非大学,现中国科学院大学博士生,中国科学院计算技术研究所vipl实验室,老年ACM铁牌退役选手,喜欢算法竞赛,会点数据结构和算法,熟悉c++,python等;现阶段研究方向主要为机器学习与数据挖掘,比较关注推荐系统,发过顶会,炼过丹,平时博客主要记录些关于算法、数据结构,人工智能技术以及平时看的论文总结分享等,欢迎大家关注我,一起多多交流共同进步!
    转载请注明原文地址: https://ju.6miu.com/read-972261.html

    最新回复(0)