洛谷P1522 牛的旅行 Cow Tours
简单的floyd+剧毒的输入
#include <iostream> #include <cstdio> #include <cmath> using namespace std; //Mystery_Sky // #define maxn 500 #define INF 0x3f3f3f3f double dis[maxn][maxn], farthest[maxn]; int n; struct Map{ double x, y; }map[maxn]; inline double far(int a, int b) { double x = map[a].x - map[b].x; double y = map[a].y - map[b].y; return sqrt(x*x + y*y); } inline void floyd() { for(int k = 1; k <= n; k++) for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) if(dis[i][j] > dis[i][k] + dis[k][j]) dis[i][j] = dis[i][k] + dis[k][j]; } int main() { scanf("%d", &n); getchar(); char x; for(int i = 1; i <= n; i++) scanf("%lf %lf", &map[i].x, &map[i].y), getchar(); for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { x = getchar(); if(i != j) { if(x == '1') dis[i][j] = far(i, j); else dis[i][j] = INF; } } getchar(); } floyd(); double link = INF; double ans = 0; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) { if(dis[i][j] != INF) farthest[i] = max(farthest[i], dis[i][j]); ans = max(ans, farthest[i]); } for(int i = 1; i <= n; i++) for(int j = i+1; j <= n; j++) { if(dis[i][j] == INF){ link = min(link, farthest[i]+farthest[j]+far(i, j)); } } printf("%.6lf\n", max(ans, link)); return 0; }
唯愿,青春不辜负梦想,未来星辰闪耀