法一:floyd
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<iomanip>
#define int long long int
using namespace std;
const int N = 510;
const int M = 1e5 + 10;
int n, idx;
double f[N][N], x[M], y[M];
signed main(){
while(cin >> n && n){
idx ++;
memset(f, 0, sizeof f);
for(int i = 1; i <= n; i++){
cin >> x[i] >> y[i];
}
for(int i = 1;i <= n; i++){
for(int j = i + 1; j <= n; j++){
double dis = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
f[i][j] = dis;
f[j][i] = dis;
}
}
for(int k = 1; k <= n; k++){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
f[i][j] = min(f[i][j], max(f[i][k], f[k][j]));
}
}
}
cout <<"Scenario #" << idx << endl;
cout <<"Frog Distance = " << fixed << setprecision(3) << f[1][2] << endl;
cout << endl;
}
return 0;
}
法二:dijkstra
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<iomanip>
#define int long long int
using namespace std;
const int N = 510;
const int M = 1e5 + 10;
int n, idx;
double f[N][N], x[M], y[M], dist[N];
bool vis[N];
void dijkstra(){
for(int i = 1; i <= n; i++){
vis[i] = false;
dist[i] = f[1][i];
}
vis[1] = true;
//每次找到未被更新的最小的值对应的点,然后用这个点去更新其他点
for(int i = 1; i <= n; i++){
int u = -1;
double minn = 0x3f3f3f3f;
for(int j = 1; j <= n; j++){
if(!vis[j] && dist[j] < minn){
minn = dist[j];
u = j;
}
}
vis[u] = true;
for(int j = 1; j <= n; j++){
if(!vis[j] && dist[j] > max(dist[u], f[u][j])){
dist[j] = max(dist[u], f[u][j]);
}
}
}
}
signed main(){
while(cin >> n && n){
idx ++;
memset(f, 0, sizeof f);
for(int i = 1; i <= n; i++){
cin >> x[i] >> y[i];
}
for(int i = 1;i <= n; i++){
for(int j = i + 1; j <= n; j++){
double dis = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
f[i][j] = dis;
f[j][i] = dis;
}
}
dijkstra();
cout <<"Scenario #" << idx << endl;
cout <<"Frog Distance = " << fixed << setprecision(3) << dist[2] << endl;
cout << endl;
}
return 0;
}
标签:dist,idx,int,题解,long,const,include,Frogger
From: https://www.cnblogs.com/N-lim/p/16906987.html