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: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static readonly double EPS = 1E-10;

        static void Main(string[] args)
        {
            int q = int.Parse(Console.ReadLine());

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < q; i++)
            {
                double[] input = Array.ConvertAll(Console.ReadLine().Split(' '), double.Parse);
                Pt a = new Pt(input[0], input[1]);
                Pt b = new Pt(input[2], input[3]);
                Pt c = new Pt(input[4], input[5]);
                Pt d = new Pt(input[6], input[7]);

                Seg s1 = new Seg(a, b);
                Seg s2 = new Seg(c, d);

                sb.AppendLine(GetDistanceSS(s1, s2).ToString());
            }
            Console.Write(sb);
        }

        static double GetDistanceSS(Seg s1,Seg s2)
        {
            if (IsIntersect(s1.p1, s1.p2, s2.p1, s2.p2)) return 0.0;
            double tempDistS1 = Math.Min(GetDistanceSP(s1, s2.p1), GetDistanceSP(s1, s2.p2));
            double tempDistS2 = Math.Min(GetDistanceSP(s2, s1.p1), GetDistanceSP(s2, s1.p2));
            return Math.Min(tempDistS1, tempDistS2);
        }

        static double GetDistanceSP(Seg s,Pt p)
        {
            if ((s.p2 - s.p1).Dot(p - s.p1) < 0.0) return (p - s.p1).Abs;
            if ((s.p1 - s.p2).Dot(p - s.p2) < 0.0) return (p - s.p2).Abs;
            return GetDistanceLP(s, p);
        }
        
        static double GetDistanceLP(Seg l,Pt p)
        {
            return Math.Abs((l.p2 - l.p1).Cross(p - l.p1) / (l.p2 - l.p1).Abs);
        }

        static bool IsIntersect(Pt p1, Pt p2, Pt p3, Pt p4)
        {
            return CCW(p1, p2, p3) * CCW(p1, p2, p4) <= 0 &&
                   CCW(p3, p4, p1) * CCW(p3, p4, p2) <= 0;
        }

        static int CCW(Pt p0, Pt p1, Pt p2)
        {
            Pt a = p1 - p0, b = p2 - p0;

            if (a.Cross(b) > EPS) return 1;
            if (a.Cross(b) < -EPS) return -1;
            if (a.Dot(b) < -EPS) return 2;
            if (a.Norm < b.Norm) return -2;
            return 0;
        }
    }

    class Pt : IComparable<Pt>
    {
        static readonly double EPS = 1E-10;

        internal double x, y;

        public Pt(double x, double y)
        {
            this.x = x;
            this.y = y;
        }

        public double Abs { get { return Math.Sqrt(Norm); } }
        public double Norm { get { return x * x + y * y; } }

        public double Dot(Pt p) { return x * p.x + y * p.y; }
        public double Cross(Pt p) { return x * p.y - y * p.x; }

        public static Pt operator +(Pt a, Pt b) { return new Pt(a.x + b.x, a.y + b.y); }
        public static Pt operator -(Pt a, Pt b) { return new Pt(a.x - b.x, a.y - b.y); }
        public static Pt operator *(Pt p, double d) { return new Pt(p.x * d, p.y * d); }
        public static Pt operator /(Pt p, double d) { return new Pt(p.x / d, p.y / d); }
        public static bool operator <(Pt a, Pt b) { return Math.Abs(a.x - b.x) > EPS ? a.x < b.x : a.y < b.y; }
        public static bool operator >(Pt a, Pt b) { return Math.Abs(a.x - b.x) > EPS ? a.x > b.x : a.y > b.y; }
        public static bool operator ==(Pt a, Pt b) { return Math.Abs(a.x - b.x) < EPS && Math.Abs(a.y - b.y) < EPS; }
        public static bool operator !=(Pt a, Pt b) { return Math.Abs(a.x - b.x) >= EPS || Math.Abs(a.y - b.y) >= EPS; }

        public int CompareTo(Pt other)
        {
            return x != other.x ? (int)(x - other.x) : (int)(y - other.y);
        }

        public override bool Equals(object obj)
        {
            Pt other = (Pt)obj;
            return Math.Abs(x - other.x) < EPS && Math.Abs(y - other.y) < EPS;
        }

        public override int GetHashCode()
        {
            return x.GetHashCode() ^ y.GetHashCode();
        }

        public override string ToString()
        {
            return string.Format("{0:f10} {1:f10}", x, y);
        }
    }

    class Seg
    {
        internal Pt p1, p2;

        public Seg(Pt a, Pt b)
        {
            p1 = a;
            p2 = b;
        }
    }
}