牛的旅行(floyd)

#include<iostream>
#include<cstring>
#include<cmath>
#define pdd pair<double,double>
#define x first
#define y second
using namespace std;
const int N=200;
const double inf=1e20;
double maxn[N];
pdd q[N];int n;
char g[N][N];
double dist[N][N];
double get(pdd a,pdd b){
    double dx=a.x-b.x;
    double dy=a.y-b.y;
    return sqrt(dx*dx+dy*dy);
}
int main(){
    cin>>n;
    for(int i=0;i<n;i++) cin>>q[i].x>>q[i].y;
    for(int i=0;i<n;i++) cin>>g[i];
    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(q[i],q[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 r1=0;
	for(int i=0;i<n;i++){
	    for(int j=0;j<n;j++ )
	        if (dist[i][j]!=inf)
	            maxn[i] = max(maxn[i], dist[i][j]);
	    r1=max(r1,maxn[i]);
	}
	
    double r2=inf;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++)
        	if(dist[i][j]==inf)
        		r2=min(r2,maxn[i]+maxn[j]+get(q[i],q[j]));
    }
    
    printf("%.6lf\n",max(r1,r2));
    return 0;
}

 

上一篇:7.1 NOI模拟赛 dp floyd


下一篇:Floyd求最短距离