题解【洛谷P1522】牛的旅行 Cow Tours
作者:互联网
看到数据范围那么小,于是考虑 Floyd。
首先,我们设 \(maxd_i\) 表示离点 \(i\) 最远的距离。
我们发现,连接两个牧场后,假设连接的两个点是 \(i\) 和 \(j\),那么最长的距离就是 \(\max\{\max\{maxd_i,maxd_j\},maxd_i+maxd_j+dist(i,j)\}\)。
然后,\(\max\{maxd_i,maxd_j\}\) 其实是可以预处理的。
这样问题就很简单了。
#include <bits/stdc++.h>
using namespace std;
const int N = 153;
const double INF = 1e20;
int n, m;
double x[N], y[N];
char s[N][N];
double dist[N][N], maxd[N], ans1, ans2 = INF;
inline double getdist(int a, int b)
{
double dx = x[a] - x[b], dy = y[a] - y[b];
return sqrt(dx * dx + dy * dy);
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i+=1)
cin >> x[i] >> y[i];
for (int i = 1; i <= n; i+=1)
scanf("%s", s[i] + 1);
for (int i = 1; i <= n; i+=1)
for (int j = 1; j <= n; j+=1)
if (i != j)
{
if (s[i][j] == '0') dist[i][j] = INF;
else dist[i][j] = getdist(i, j);
}
for (int k = 1; k <= n; k+=1)
for (int i = 1; i <= n; i+=1)
for (int j = 1; j <= n; j+=1)
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
for (int i = 1; i <= n; i+=1)
for (int j = 1; j <= n; j+=1)
if (dist[i][j] != INF)
maxd[i] = max(maxd[i], dist[i][j]);
for (int i = 1; i <= n; i+=1) ans1 = max(ans1, maxd[i]);
for (int i = 1; i <= n; i+=1)
for (int j = 1; j <= n; j+=1)
if (dist[i][j] == INF)
ans2 = min(ans2, maxd[i] + maxd[j] + getdist(i, j));
printf("%.6lf\n", max(ans1, ans2));
return 0;
}
标签:max,洛谷,Cow,int,题解,dy,dx,double,maxd 来源: https://www.cnblogs.com/xsl19/p/12390173.html