Problem:
Distance

For given two segments s1 and s2, print the distance between them.

s1 is formed by end points p0 and p1, and
s2 is formed by end points p2 and p3.

Input

The entire input looks like:

q (the number of queries)
1st query
2nd query
...
qth query

Each query consists of integer coordinates of end points of s1 and s2 in the following format:

xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3

Output

For each query, print the distance. The output values should be in a decimal fraction with an error less than 0.00000001.

Constraints

1 ≤ q ≤ 1000

-10000 ≤ xpi, ypi ≤ 10000

p0 ≠ p1 and
p2 ≠ p3.

Sample Input

3
0 0 1 0 0 1 1 1
0 0 1 0 2 1 1 2
-1 0 1 0 0 1 0 -1

Sample Output

1.0000000000
1.4142135624
0.0000000000

Solution in C# (judge verdict: Time Limit Exceeded)

using System;
using System.Text;

namespace CGL_2_D_Distance
{
	class Program
	{
		static void Main ( string[] args )
		{
			int inputCount = int.Parse (Console.ReadLine ());
			StringBuilder output = new StringBuilder (inputCount * 20);

			for (int lp = 0; lp < inputCount; lp++)
			{
				int[] inputNum = Array.ConvertAll (Console.ReadLine ().Split (' '), int.Parse);

				Vector2 point1 = new Vector2 (inputNum[0], inputNum[1]);
				Vector2 point2 = new Vector2 (inputNum[2], inputNum[3]);
				Vector2 point3 = new Vector2 (inputNum[4], inputNum[5]);
				Vector2 point4 = new Vector2 (inputNum[6], inputNum[7]);

				decimal result = Vector2.Distance (point1, point2, point3, point4);

				output.Append (result);
				output.AppendLine ();
			}

			Console.Write (output);
		}
	}

	class Vector2
	{
		public Vector2 ( decimal x, decimal y )
		{
			this.X = x;
			this.Y = y;
		}

		public decimal X { private set; get; }
		public decimal Y { private set; get; }

		public static Vector2 operator + ( Vector2 ob1, Vector2 ob2 )
		{
			return new Vector2 (ob1.X + ob2.X, ob1.Y + ob2.Y);
		}
		public static Vector2 operator - ( Vector2 ob1, Vector2 ob2 )
		{
			return new Vector2 (ob1.X - ob2.X, ob1.Y - ob2.Y);
		}
		public static Vector2 operator * ( Vector2 ob, decimal scalar )
		{
			return new Vector2 (ob.X * scalar, ob.Y * scalar);
		}

		public decimal Length ()
		{
			return X * X + Y * Y;
		}

		public static decimal DotProduct ( Vector2 line1, Vector2 line2 )
		{
			return line1.X * line2.X + line1.Y * line2.Y;
		}
		public static decimal CrossProduct ( Vector2 line1, Vector2 line2 )
		{
			return line1.X * line2.Y - line1.Y * line2.X;
		}
		public static decimal Sqrt ( decimal number )
		{
			decimal currentNum = number / 2m;
			decimal lastNum = 0m;

			while (currentNum != lastNum)
			{
				lastNum = currentNum;
				currentNum = (currentNum + number / currentNum) / 2.0m;
			}

			return currentNum;
		}

		public static decimal Distance ( Vector2 point1, Vector2 point2 )
		{
			return Sqrt ((point1 - point2).Length ());
		}

		public static decimal DistanceLP ( Vector2 linePoint1, Vector2 linePoint2, Vector2 point )
		{
			return Math.Abs (
				CrossProduct (linePoint2 - linePoint1, point - linePoint1) / Sqrt ((linePoint2 - linePoint1).Length ())
				);
		}

		public static decimal DistanceSP ( Vector2 linePoint1, Vector2 linePoint2, Vector2 point )
		{
			if (DotProduct (linePoint2 - linePoint1, point - linePoint1) < 0m)
				return Sqrt ((point - linePoint1).Length ());
			if (DotProduct (linePoint1 - linePoint2, point - linePoint2) < 0m)
				return Sqrt ((point - linePoint2).Length ());

			return DistanceLP (linePoint1, linePoint2, point);
		}

		public static decimal Distance ( Vector2 line1Point1, Vector2 line1Point2, Vector2 line2Point1, Vector2 line2Point2 )
		{
			if (Intersection (line1Point1, line1Point2, line2Point1, line2Point2))
			{
				return 0m;
			}

			return Math.Min (
				Math.Min (DistanceSP (line1Point1, line1Point2, line2Point1),
				DistanceSP (line1Point1, line1Point2, line2Point2)),

				Math.Min (DistanceSP (line2Point1, line2Point2, line1Point1),
				DistanceSP (line2Point1, line2Point2, line1Point2))
			);
		}

		public const int Counter_Clockwise = 1;
		public const int Clockwise = -1;
		public const int OnlineBack = 2;
		public const int OnlineFront = -2;
		public const int OnSegment = 0;

		public static int PointState ( Vector2 point1, Vector2 point2, Vector2 point3 )
		{
			Vector2 line1 = point2 - point1;
			Vector2 line2 = point3 - point1;

			decimal crossProduct = CrossProduct (line1, line2);

			if (crossProduct > 0m)
			{
				return Counter_Clockwise;
			}
			else if (crossProduct < 0m)
			{
				return Clockwise;
			}
			else if (DotProduct (line1, line2) < 0m)
			{
				return OnlineBack;
			}
			else if (line1.Length () < line2.Length ())
			{
				return OnlineFront;
			}

			return OnSegment;
		}

		public static bool Intersection ( Vector2 line1Point1, Vector2 line1Point2, Vector2 line2Point1, Vector2 line2Point2 )
		{
			Vector2 p1 = line1Point1;
			Vector2 p2 = line1Point2;
			Vector2 p3 = line2Point1;
			Vector2 p4 = line2Point2;

			return (PointState (p1, p2, p3) * PointState (p1, p2, p4) <= 0 &&
				PointState (p3, p4, p1) * PointState (p3, p4, p2) <= 0);
		}
	}
}