题目大意
- 出个n个点的坐标,任意两个点都是互相可到达的。求第0点到第1点的所有路径中,长度最大的边的最小值。
Floyd算法求解
- Floyd是求图中任意两个点的最短路,即dis[i][j]表示i到达j的最短距离。
这里稍作变换,令dis[i][j]表示i到达j的所有路径中长度最大的边的最小值。则
if(dis[i][j] > max(dis[i][k] , dis[j][k])) dis[i][j] = max(dis[i][k] , dis[j][k])
代码
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
const int maxn = 210;
struct Pos {
int x , y;
};
Pos p[maxn];
double dis[maxn][maxn];
double distant(Pos const &A , Pos const &B) {
return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));
}
int main()
{
int n , caseno = 1;
while(cin >> n && n)
{
for(int i = 0; i < n; i++) cin >> p[i].x >> p[i].y;
for(int i = 0; i < n; i++) for(int j = i; j < n; j++) {
if(i == j) dis[i][j] = 0;
else dis[i][j] = dis[j][i] = distant(p[i] , p[j]);
}
//floyd
for(int k = 0; k < n; k++)
{
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
if(dis[i][j] > max(dis[i][k] , dis[j][k]))
dis[i][j] = max(dis[i][k] , dis[j][k]);
}
cout << fixed << setprecision(3);
cout << "Scenario #" << caseno++ << endl;
cout << "Frog Distance = " << dis[0][1] << endl << endl;
}
return 0;
}
Dijstra算法求解
- dijstra是求原点s到达其它点的最短路,即dis[i]表示原点s到达i的最短距离。
- 这里也是稍作变化,令dis[i]表示s到达i的所有路径中长度最大的边的最小值。对松弛操作稍作改变即可。
代码
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <iomanip>
using namespace std;
#define INF 0x3fffffff
const int maxn = 210;
struct Pos {
int x , y;
};
struct Edge {
int from , to;
double v;
Edge() {}
Edge(int x , int y , double z) {from = x; to = y; v = z;}
};
struct HeapNode {
double d;
int u; //d表示当前节点u距s的距离
HeapNode() {}
HeapNode(double x , int y) {d = x; u = y;}
bool operator < (const HeapNode & rhs) const {
return d > rhs.d;
}
};
vector<Edge> edges;
vector<int> G[maxn]; //G[i][j]表示节点i的第j条边在edges中的编号
Pos p[maxn];
void init(int n)
{
edges.clear();
for(int i = 0; i <= n; i++) G[i].clear();
}
void addEdge(int from , int to , double v) {
edges.push_back(Edge(from , to , v));
int m = edges.size();
G[from].push_back(m - 1);
}
double distant(Pos const &A , Pos const &B) {
return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));
}
int vis[maxn]; double dis[maxn];
double dijstra(int s , int n)
{
priority_queue<HeapNode> Q;
for(int i = 0; i < n; i++) dis[i] = (s == i ? 0 : INF);
memset(vis , 0 , sizeof(vis));
Q.push(HeapNode(0 , s));
while(!Q.empty())
{
HeapNode cur = Q.top(); Q.pop();
int u = cur.u;
if(vis[u]) continue;
vis[u] = 1;
for(unsigned i = 0; i < G[u].size(); i++) {
Edge &e = edges[G[u][i]];
if(dis[e.to] > max(dis[u] , e.v)) {
dis[e.to] = max(dis[u] , e.v);
Q.push(HeapNode(dis[e.to] , e.to));
}
}
}
return dis[1];
}
int main()
{
int n , caseno = 1;
while(cin >> n && n)
{
init(n);
for(int i = 0; i < n; i++) cin >> p[i].x >> p[i].y;
for(int i = 0; i < n; i++) for(int j = i + 1; j < n; j++) {
addEdge(i , j , distant(p[i],p[j]));
addEdge(j , i , distant(p[i],p[j]));
}
cout << fixed << setprecision(3);
cout << "Scenario #" << caseno++ << endl;
cout << "Frog Distance = " << dijstra(0 , n) << endl << endl;
}
return 0;
}