POJ2253 Frogger
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.
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
大意就是求任意两个点之间所有路径中最大两点距离的最小值。可以看作是Floyd的变形,只需要把转移方程修改一下即可:d[i][j]=min(d[i][j],max(d[i][k],d[k][j]))其中d[i][j]表示i到j所有路程经过的最长边的最小值。
当然也可以用dijkstra来写,松弛操作改为:if(!vis[y] && d[y]>max(d[x],a[x][y]))
d[y]=max(d[x],a[x][y]);
这其实也算是某种意义上的最短路,只不过定义不同。g++提交的话记得要把.3lf改成.3f才能过。
//Floyd #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; int n; struct node { double x; double y; }nod[205]; double d[205][205]; double floyd() { int i,j,k; for(k=1;k<=n;k++) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { d[i][j]=min(d[i][j],max(d[i][k],d[k][j])); } } } return d[1][2]; } int main() { int cnt=0; while(scanf("%d",&n)!=EOF&&n) { cnt++; int i,j; //memset(d,0x3f,sizeof(d)); double不能用memset for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { d[i][j]=1e9; } } for(i=1;i<=n;i++) { double x,y; scanf("%lf%lf",&x,&y); nod[i].x=x; nod[i].y=y; } for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { double len=sqrt((nod[i].x-nod[j].x)*(nod[i].x-nod[j].x)+(nod[i].y-nod[j].y)*(nod[i].y-nod[j].y)); d[i][j]=d[j][i]=min(d[i][j],len); cout<<d[i][j]<<endl; } } double out=floyd(); printf("Scenario #%d\n",cnt); printf("Frog Distance = %.3lf\n\n",out); } }
//Dijkstra #include <iostream> #include <algorithm> #include <cstdio> #include <cmath> #include <cstring> using namespace std; double a[305][305]; double d[305]; bool vis[305]; int n; struct node { double x; double y; }nod[205]; void dijkstra() { d[1]=0; int i; for(i=1;i<n;i++) { int x=0; int j; for(j=1;j<=n;j++) { if(!vis[j]&&(x==0||d[j]<d[x]))x=j; } vis[x]=1; int y; for(y=1;y<=n;y++) { } } } int main() { int cnt=0; while(scanf("%d",&n)!=EOF&&n) { cnt++; int i,j; //memset(d,0x3f,sizeof(d)); double不能用memset for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { a[i][j]=1e9; } d[i]=1e9; vis[i]=0; } for(i=1;i<=n;i++) { double x,y; scanf("%lf%lf",&x,&y); nod[i].x=x; nod[i].y=y; } for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { double len=sqrt((nod[i].x-nod[j].x)*(nod[i].x-nod[j].x)+(nod[i].y-nod[j].y)*(nod[i].y-nod[j].y)); a[i][j]=a[j][i]=min(a[i][j],len); } } // for(i=1;i<=n;i++) // { // for(j=1;j<=n;j++) // { // cout<<a[i][j]<<' '; // } // cout<<endl; // } dijkstra(); printf("Scenario #%d\nFrog Distance = %.3lf\n\n",cnt,d[2]); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!