求救
查看原帖
求救
163714
Wanna_Accepted楼主2020/8/27 20:34
#include<bits/stdc++.h>
using namespace std;
const int N=500;

double dis[N][N];
double lg[N];
int a[N][2];
int g[N];
int gr[N];
int n,m;
int fa[N];
double dist(int x,int y)
{
	return sqrt((a[x][0]-a[y][0])*(a[x][0]-a[y][0])+(a[x][1]-a[y][1])*(a[x][1]-a[y][1]));
}
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 getfather(int x)
{
	if(fa[x]==x)return x;
	else return fa[x]=getfather(fa[x]);
}
void join(int a,int b)
{
	int af=getfather(a);
	int bf=getfather(b);
	fa[af]=bf;
}
int main()
{
	freopen("1.txt","r",stdin);
	scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
    	scanf("%d%d",&a[i][0],&a[i][1]);
    }
    int c;
    for(int i=1;i<=n;i++)fa[i]=i;
    
    for(int i=1;i<=n;i++)
    {
    	for(int j=1;j<=n;j++)
      {
	     
         scanf("%1d",&c);
	     
	 	 if(c==1)
		 {
		 	if(getfather(i)!=getfather(j))
			join(i,j);
			dis[i][j]=dis[j][i]=dist(i,j);
		 }   
		 else if(i!=j)dis[i][j]=0x3f3f3f3f;	
      }
    }
    floyd();
    //cout<<dist(150,149);
//    for(int i=1;i<=n;i++)
//    {
//    	for(int j=1;j<=n;j++)
//    	{
//    		cout<<dis[i][j]<<endl;
//    	}
//    }
    int f1=getfather(1);
    int temp=2;
    while(getfather(temp)==f1)
    {
    	temp++;
    }
    int f2=getfather(temp);
    int cnt1=0;
    int cnt2=0;
    for(int i=1;i<=n;i++)
    {
    	if(getfather(i)!=f1&&getfather(i)!=f2)
    	{
    		double res=0x3f3f3f3f;
    		for(int j=1;j<=n;j++)
    		{
    			for(int k=1;k<=n;k++)
    			{
    				//if(j==149&&k==150)cout<<111111;
    				if(k==j)continue;
    			    res=min(res,dist(k,j));
    			    
    			}
    		}
    printf("%.6lf",res);
    			    return 0;
    }
   }
    for(int i=1;i<=n;i++)
    {
    	if(getfather(i)==f1)
    	{
    		g[++cnt1]=i;
    	}
    	else if(getfather(i)==f2)
    	{
    		gr[++cnt2]=i;
    	}
    }
    double d1=0;
    int p1,pp1,p2,pp2;
    for(int i=1;i<=cnt1;i++)
    {
    	for(int j=1;j<=cnt1;j++)
    	{
    		if(dis[g[i]][g[j]]>=d1&&dis[g[i]][g[j]]!=0x3f3f3f3f)
    		{
    			d1=dis[g[i]][g[j]];
    			p1=g[i];
    			p2=g[j];
    		}
    	}
    }
   //cout<<cnt1<<cnt2<<d1;
    for(int i=1;i<=cnt1;i++)
    {
      lg[g[i]]=max(dis[g[i]][p1],dis[g[i]][p2]);
    }
    //cout<<p1<<endl<<p2;
   double d2=0;
      for(int i=1;i<=cnt2;i++)
    {
    	for(int j=1;j<=cnt2;j++)
    	{
    		if(dis[gr[i]][gr[j]]>=d2&&dis[gr[i]][gr[j]]!=0x3f3f3f3f)
    		{
    			d2=dis[gr[i]][gr[j]];
    			pp1=gr[i];
    			pp2=gr[j];
    		}
    	}
    }
    //cout<<d1<<endl<<d2<<endl;
    for(int i=1;i<=cnt2;i++)
    {
      lg[gr[i]]=max(dis[gr[i]][pp1],dis[gr[i]][pp2]);
    }
    double ans=0x3f3f3f3f;
   
    for(int i=1;i<=cnt1;i++)
    {
    	for(int j=1;j<=cnt2;j++)
    	{
    	double tot=max(d2,max(lg[g[i]]+lg[gr[j]]+dist(g[i],gr[j]),d1));
    	ans=min(ans,tot);
    	}
    }
    printf("%.6lf",ans);
    return 0;
}
2020/8/27 20:34
加载中...