牛的旅行
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 const int maxn=157; 7 int x[maxn],y[maxn]; 8 double map[maxn][maxn],dmax[maxn]; 9 int n; 10 double d(int x,int y){ 11 double ret; 12 ret=(double)sqrt(x*x+y*y); 13 return ret; 14 } 15 int main(){ 16 cin>>n; 17 memset(map,0x7f7f7f7f,sizeof(map)); 18 for(int i=1;i<=n;i++) map[i][i]=0; 19 for(int i=1;i<=n;i++) cin>>x[i]>>y[i]; 20 for(int i=1;i<=n;i++){ 21 for(int j=1;j<=n;j++){ 22 char a;cin>>a; 23 if(a=='1'){ 24 map[i][j]=min(map[i][j],d(abs(x[i]-x[j]),abs(y[i]-y[j]))); 25 map[j][i]=map[i][j]; 26 } 27 } 28 } 29 for(int k=1;k<=n;k++) 30 for(int i=1;i<=n;i++) 31 for(int j=1;j<=n;j++) 32 if(map[i][j]>map[i][k]+map[k][j]) 33 map[i][j]=map[i][k]+map[k][j]; 34 double l1=0; 35 for(int i=1;i<=n;i++) 36 for(int j=i+1;j<=n;j++) 37 if(map[i][j]<0x7f7f7f7f){ 38 dmax[i]=max(dmax[i],map[i][j]); 39 dmax[j]=max(dmax[j],map[i][j]); 40 l1=max(l1,dmax[i]);l1=max(l1,dmax[j]); 41 } 42 int num1,num2; 43 double ans=0x7f7f7f7f; 44 for(int i=1;i<=n;i++){ 45 for(int j=1;j<=n;j++){ 46 if(map[i][j]>=0x7f7f7f7f){ 47 if(ans>dmax[i]+dmax[j]+d(abs(x[i]-x[j]),abs(y[i]-y[j]))){ 48 ans=dmax[i]+dmax[j]+d(abs(x[i]-x[j]),abs(y[i]-y[j])); 49 num1=i;num2=j; 50 } 51 } 52 } 53 } 54 ans=max(ans,l1); 55 printf("%.6lf\n",ans); 56 return 0; 57 }
这种d[x]+d[y]+dis[x][y]的想法不是第一次见了,DP求树的直径时就是这样