当前位置: 代码迷 >> 综合 >> POJ - 2253 Frogger 【dijkstra】
  详细解决方案

POJ - 2253 Frogger 【dijkstra】

热度:89   发布时间:2023-11-20 06:06:26.0

题意 :求出第一个坐标通往第二个坐标的所有路径中找到最大的边中里最小的那个;就是说我们去找所有路径中最大的边,然后在最大边里输出最小的那个;

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
const int INF = 0x3f3f3f3f;
using namespace std;
struct Node{double x,y;
}node[205];
int n;
bool vis[205];
double dis[205];
double map[205][205];
double dijkstra(int s,int End)
{memset(vis,false,sizeof(vis));for (int i = 1; i <= n; i++)dis[i] = map[s][i];dis[1] = 0.0;vis[s] = true;for (int i = 1; i < n ;i++){double Min = INF;int k = 0;for (int j = 1; j <= n; j++){if(!vis[j] && dis[j] <= Min){Min = dis[j];k = j;}}vis[k] = true;if(k == 0) break;for (int j = 1; j <= n; j++){if(!vis[j] && dis[j] > map[k][j]){dis[j] = min(max(map[k][j],dis[k]),dis[j]);}}}return dis[End];
}
int main()
{int Kase = 0;while(cin >> n && n){for (int i = 1; i <= n; i++)for(int j = 1; j <= n; j++)map[i][j] = map[j][i] = INF;for (int i = 1; i <= n; i++){double u , v;cin >> node[i].x >> node[i].y;for (int j = 1; j < i; j++){u = node[i].x - node[j].x;v = node[i].y - node[j].y;map[i][j] = map[j][i] = sqrt(u * u + v * v);}}double ans = dijkstra(1,2);printf("Scenario #%d\n",++Kase);printf("Frog Distance = %.3f\n",ans);cout << endl;}
}