洛谷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;
}

 

posted @ 2019-05-01 21:48  Mystery_Sky  阅读(71)  评论(0编辑  收藏  举报