center-in-mass-2

You are studying a swarm of N fireflies. Each firefly is moving in a straight line at a constant speed. You are standing at the center of the universe, at position (0, 0, 0). Each firefly has the same mass, and you want to know how close the center of the swarm will get to your location (the origin).

You know the position and velocity of each firefly at t = 0, and are only interested in t ≥ 0. The fireflies have constant velocity, and may pass freely through all of space, including each other and you. Let M(t) be the location of the center of mass of the N fireflies at time t. Let d(t) be the distance between your position and M(t) at time t. Find the minimum value of d(t), dmin, and the earliest time when d(t) = dmin, tmin. 

INPUT

The first line of input contains a single integer T, the number of test cases. Each test case starts with a line that contains an integer N, the number of fireflies, followed by N lines of the form

x y z vx vy vz

Each of these lines describes one firefly: (x, y, z) is its initial position at time t = 0, and (vx, vy, vz) is its velocity. 

OUTPUT

For each test case, output

Case #X: dmin tmin

where X is the test case number, starting from 1. Any answer with absolute or relative error of at most 10-5 will be accepted.

CONSTRAINTS

All the numbers in the input will be integers.

1 ≤ T ≤ 100

The values of x, y, z, vx, vy and vz will be between -5000 and 5000, inclusive.

3 ≤ N ≤ 500

SAMPLE INPUT
1
3
3 0 -4 0 0 3
-3 -2 -1 3 0 0
-3 -1 2 0 3 0

SAMPLE OUTPUT
Case #1: 0.00000000 1.00000000

Explanation

NOTE

Given N points (xi, yi, zi), their center of the mass is the point (xc, yc, zc), where:

xc = (x1 + x2 + ... + xN) / N
yc = (y1 + y2 + ... + yN) / N
zc = (z1 + z2 + ... + zN) / N

Difficulty: EASY

Starter code:

Solutions:
from sys import stdin
from math import sqrt

def main():
	nextitem = iter(map(int, stdin.read().split())).__next__
	for t in range(1, 1 + nextitem()):
		n = nextitem()
		sx, sy, sz, svx, svy, svz = 0, 0, 0, 0, 0, 0
		for _ in range(n):
			x, y, z, vx, vy, vz = [nextitem() for _ in range(6)]
			sx += x
			sy += y
			sz += z
			svx += vx
			svy += vy
			svz += vz
		numer, denom = -(sx * svx + sy * svy + sz * svz), svx**2 + svy**2 + svz**2
		if denom == 0:
			dmin = sqrt(sx**2 + sy**2 + sz**2) / float(n)
			print('Case #%d: %.8f %.8f' % (t, dmin, 0))
		else:
			time = round(numer / float(denom), 9)
			if time < 0:
				dmin = sqrt(sx**2 + sy**2 + sz**2) / float(n)
				print('Case #%d: %.8f %.8f' % (t, dmin, 0))
			else:
				dmin = sqrt((time * svx + sx)**2 + (time * svy + sy)**2 + (time * svz + sz)**2) / float(n)
				print('Case #%d: %.8f %.8f' % (t, dmin, time))

main()




import math

def cross(a, b):
	c = [float(a[1])*float(b[2]) - float(a[2])*float(b[1]),
		 float(a[2])*float(b[0]) - float(a[0])*float(b[2]),
		 float(a[0])*float(b[1]) - float(a[1])*float(b[0])]
	return c

def modulo(a):
	b=float(a[0])*float(a[0])+float(a[1])*float(a[1])+float(a[2])*float(a[2])
	b=math.sqrt(b)
	return b

t=int(input())
for i in range(0,t):
	n=int(input());
	x=0;y=0;z=0;vx=0;vy=0;vz=0;
	for j in range(0,n):
		q=input()
		q=q.split()
		x=x+float(q[0])
		y=y+float(q[1])
		z=z+float(q[2])
		vx=vx+float(q[3])
		vy=vy+float(q[4])
		vz=vz+float(q[5])
	x=x/n;y=y/n;z=z/n;vx=vx/n;vy=vy/n;vz=vz/n;
	zn=[]
	zn.append(x);zn.append(y);zn.append(z);
	vn=[]
	vn.append(vx);vn.append(vy);vn.append(vz);
	c=cross(zn,vn)
	c=modulo(c);
	vn=modulo(vn);
	
	dist=c/float(vn)

	distfromorigin=modulo(zn)
	pointdist=math.sqrt((distfromorigin*distfromorigin)-(dist*dist))
	finaltime=pointdist/float(vn)	
	dist=format(dist, '.8f')
	finaltime=format(finaltime, '.8f')
	print("Case #"+str(i+1)+": "+str(dist)+" "+str(finaltime))