AcWing 1125. 牛的旅行
原题链接
简单
作者:
zqc
,
2021-04-13 21:07:43
,
所有人可见
,
阅读 282
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
#define x first
#define y second
const int N = 200, INF = 0x3f3f3f3f;
char g[N][N];
double dist[N][N];
double maxd[N];
vector<PII> p;
double get_dist(PII a, PII b)
{
int x = abs(a.x - b.x);
int y = abs(a.y - b.y);
return sqrt(x * x + y * y);
}
int main(void)
{
int n;
cin >> n;
for (int i = 0; i < n; i ++)
{
int a, b;
cin >> a >> b;
p.push_back({a, b});
}
for (int i = 0; i < n; i ++)
{
for (int j = 0; j < n; j ++)
cin >> g[i][j];
}
for (int i = 0; i < n; i ++)
for (int j = 0; j < n; j ++)
if (i == j)
dist[i][j] = 0;
else if(g[i][j] == '1')
dist[i][j] = get_dist(p[i], p[j]);
else
dist[i][j] = INF;
for (int k = 0; k < n; k ++)
for (int i = 0; i < n; i ++)
for (int j = 0; j < n; j ++)
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
double ans1 = 0, ans2 = INF;
for (int i = 0; i < n; i ++)
{
for (int j = 0; j < n; j ++)
if (dist[i][j] != INF)
maxd[i] = max(maxd[i], dist[i][j]);
ans1 = max(ans1, maxd[i]);
}
for (int i = 0; i < n; i ++)
for (int j = 0; j < n; j ++)
if (dist[i][j] == INF)
ans2 = min(ans2, maxd[i] + maxd[j] + get_dist(p[i], p[j]));
printf("%lf", max(ans1, ans2));
return 0;
}