RT,和这个帖子的问题不太一样
我的ans1=39796.4但ans2=40058.1,而上面帖子中ans2=22693.9
#include <bits/stdc++.h>
using namespace std;
const int N = 150 + 10;
typedef pair<int, double> PID;
struct Point { double x, y; };
double getdist(Point p1, Point p2) { return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)); }
Point p[N];
double d[N][N]/* getdist */,
dist[N][N]/* floyd */,
maxd[N]/* farm */,
val[N]/* from one point */;
int n, c[N]/* color */, idx;
double e[N][N];
char s[N];
void init() {
for(int i = 0; i <= N - 2; i ++ )
for(int j = 0; j <= N - 2; j ++ )
if(i != j)
e[i][j] = e[j][i] = dist[i][j] = dist[j][i] = 1500000000.0;
scanf("%d", &n);
for(int i = 1; i <= n; i ++ ) scanf("%lf%lf", &p[i].x, &p[i].y);
for(int i = 1; i <= n; i ++ )
for(int j = i + 1; j <= n; j ++ )
d[i][j] = d[j][i] = getdist(p[i], p[j]);
for(int i = 1; i <= n; i ++ ) {
scanf(" %s", s + 1);
for(int j = 1; j <= n; j ++ )
if(s[j] == '1')
e[i][j] = e[j][i] = dist[i][j] = dist[j][i] = d[i][j];
}
}
void dfs(int x, int col) {
c[x] = col;
for(int i = 1; i <= n; i ++ ) {
if(i == x || c[i] || e[i][x] > 1e9) continue;
dfs(i, col);
}
}
void separate() {
for(int i = 1; i <= n; i ++ )
if(!c[i]) { idx ++ ; dfs(i, idx); }
}
void floyd() {
for(int k = 1; k <= n; k ++ )
for(int i = 1; i <= n; i ++ )
for(int j = 1; j <= n; j ++ )
if(dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
void get_max_from_one() {
for(int i = 1; i <= n; i ++ )
for(int j = 1; j <= n; j ++ )
if(dist[i][j] < 1e9)
val[i] = max(val[i], dist[i][j]);
}
void get_max_in_farm() {
for(int i = 1; i <= n; i ++ )
for(int j = i + 1; j <= n; j ++ )
if(c[i] == c[j] && dist[i][j] < 1e9)
maxd[c[i]] = max(maxd[c[i]], dist[i][j]);
}
void get_ans() {
double ans1 = 0.0, ans = 15000000000.0;
for(int i = 1; i <= n; i ++ ) ans1 = max(ans1, val[i]); // 必须和之前直径比较
for(int i = 1; i <= n; i ++ )
for(int j = i + 1; j <= n; j ++ )
if(c[i] != c[j])
if(d[i][j] + val[i] + val[j] > max(maxd[c[i]], maxd[c[j]])) {
ans = min(ans, d[i][j] + val[i] + val[j]);
}
cout << ans1 << " " << ans << endl;
printf("%.6lf\n", max(ans, ans1));
}
int main() {
init();
separate();
floyd();
get_max_from_one();
get_max_in_farm();
get_ans();
return 0;
}