AcWing1125 牛的旅行(floyd)
看得出题目的直径也就是任意两点之间最短路的最大值,因此这是个多源汇最短路
而连接两个独立的区域,就需要取到最小值,然后跟每个集合的最大值进行取max
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<string> #include<algorithm> #include<cmath> #define x first #define y second using namespace std; typedef pair<double,double> pll; const int N=200; const double inf=1e20; double d[N][N]; double maxd[N]; string s[N]; int n; pll q[N]; double get(int i,int j){ double a=q[i].x-q[j].x; double b=q[i].y-q[j].y; return sqrt(a*a+b*b); } int main(){ cin>>n; int i; for(i=1;i<=n;i++){ cin>>q[i].x>>q[i].y; } for(i=1;i<=n;i++){ cin>>s[i]; } int j; for (int i =1; i <= n; i ++ ) for (int j = 1; j <= n; j ++ ){ if(i==j) continue; if (s[i][j-1] == '1') d[i][j] = get(i, j); else d[i][j] = inf; } for(int k=1;k<=n;k++){ for(i=1;i<=n;i++){ for(j=1;j<=n;j++){ d[i][j]=min(d[i][j],d[i][k]+d[k][j]); } } } double r1=0; for(i=1;i<=n;i++){ for(j=1;j<=n;j++){ if(d[i][j]<inf/2) maxd[i]=max(maxd[i],d[i][j]); } r1=max(r1,maxd[i]); } double r2=inf; for(i=1;i<=n;i++){ for(j=1;j<=n;j++){ if(d[i][j]>inf/2){ r2=min(r2,maxd[i]+maxd[j]+get(i,j)); } } } printf("%.6f\n",max(r1,r2)); }
没有人不辛苦,只有人不喊疼