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 JAVA (judge verdict: Accepted)

import java.awt.geom.Line2D;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    class P {
        double x, y;
        
        P(double x_, double y_) {
            x = x_;
            y = y_;
        }
        
        P add(P p) {
            return new P(x + p.x, y + p.y);
        }
        
        P sub(P p) {
            return new P(x - p.x, y - p.y);
        }
        
        P mul(double v) {
            return new P(x * v, y * v);
        }
        
        P div(double v) {
            return new P(x / v, y / v);
        }
        
        double dot(P p) {
            return x * p.x + y * p.y;
        }
        
        double det(P p) {
            return x * p.y - y * p.x;
        }
        
        double abs2() {
            return x * x + y * y;
        }
        
        double abs() {
            return Math.sqrt(abs2());
        }
        
        P norm() {
            return div(abs());
        }
        
        P rot90() {
            return new P(-y, x);
        }
        
        P proj(P src, P dst) {
            P p_sd = dst.sub(src).norm();
            return src.add(p_sd.mul(sub(src).dot(p_sd)));
        }
        
        P reflect(P src, P dst) {
            P middle = proj(src, dst);
            return add(middle.sub(this).mul(2));
        }
        
        public String toString() {
            return String.format("%f %f", x, y);
        }
    }
    
    P isLL(P p1, P p2, P q1, P q2) {
        double d = p2.sub(p1).det(q1.sub(q2));
        if(Math.abs(d) < 1e-10) return null;
        return p1.add(p2.sub(p1).mul(q1.sub(q2).det(p1.sub(q2)) / d));
    }
    
    boolean intersect(P p1, P p2, P q1, P q2) {
        return Line2D.linesIntersect(p1.x, p1.y, p2.x, p2.y, q1.x, q1.y, q2.x, q2.y);
    }
    
    double distSS(P p1, P p2, P q1, P q2) {
        if(intersect(p1, p2, q1, q2)) return 0;
        
        double dist = Double.MAX_VALUE;
        for(double d : new double[] {distSP(p1, p2, q1), distSP(p1, p2, q2), 
                                     distSP(q1, q2, p1), distSP(q1, q2, p2)}) {
            dist = Math.min(d, dist);
        }
        return dist;
    }
    
    double distSP(P p1, P p2, P p) {
        return Line2D.ptSegDist(p1.x, p1.y, p2.x, p2.y, p.x, p.y);
    }
    
    void run() {
        Scanner sc = new Scanner(System.in);
        int q = sc.nextInt();
        
        while(q-- > 0) {
            P p1 = new P(sc.nextDouble(), sc.nextDouble());
            P p2 = new P(sc.nextDouble(), sc.nextDouble());
            P q1 = new P(sc.nextDouble(), sc.nextDouble());
            P q2 = new P(sc.nextDouble(), sc.nextDouble());
            
            System.out.println(distSS(p1, p2, q1, q2));
        }
    }
    
    public static void main(String[] args) {
        new Main().run();
    }
    
    static void tr(Object... objects) {
        System.out.println(Arrays.deepToString(objects));
    }
}