[Uva 1001] Say Cheese [Floyd]


#include
#include
#include
#include
#include
using namespace std;
int n;
#define maxn 110
struct data{
	int x,y,z;
	int r;
	void read(bool flag){
		scanf("%d%d%d",&x,&y,&z);
		if(flag)scanf("%d",&r);
		else r = 0;
	}
	data(){
		x = y = z = r = 0;
	}
}e[maxn+2];
double dp[maxn][maxn];
double pow2(double x){
	return x*x;
}
double getdis(data u,data v){
	return sqrt(pow2(u.x-v.x)+pow2(u.y-v.y)+pow2(u.z-v.z));
}
int idx;
void init(){
	memset(dp,0,sizeof(dp));
}
int main(void)
{
	while(scanf("%d",&n),~n)
	{
		init();
		for(int i=1;i<=n;i++)e[i].read(1);
		e[n+1].read(0);e[n+2].read(0);
		for(int i=1;i<=n+2;i++){
			for(int j=i+1;j<=n+2;j++){
				double dist = getdis(e[i],e[j]);
				if(dist>e[i].r+e[j].r){
					dp[i][j]=dp[j][i]=dist-double(e[i].r+e[j].r);					
				}
				else dp[i][j] = dp[j][i] = 0;
			}
		}
		for(int k=1;k<=n+2;k++){
			for(int i=1;i<=n+2;i++){
				for(int j=1;j<=n+2;j++){
					if(i==j)continue;
					dp[i][j] = min(dp[i][j],dp[i][k]+dp[k][j]);
				}
			}
		}		
		printf("Cheese %d: Travel time = %.0f sec\n",++idx,dp[n+1][n+2]*10.0);
	}
	return 0;
}


你可能感兴趣的:(算法与数学)