Language: C++

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

#define NO_INTERSECT POS(-12345, -12345)
#define PARALLEL POS(-10001, -10001)
#define COLINE POS(12345, 12345)
typedef long double T;

const T EPS = 1e-10;

class POS {
public:
    T x, y;
    POS(const T& x = 0, const T& y = 0) : x(x), y(y) {}
    POS(const POS& x) : x(x.x), y(x.y) {}

    bool operator==(const POS& rhs) const {
        return x == rhs.x && y == rhs.y;
    }

    POS& operator+=(const POS& rhs) {
        x += rhs.x;
        y += rhs.y;
        return *this;
    }

    POS operator -() {
        POS tmp(-x, -y);
        return tmp;
    }

    double dist(const POS& rhs) const {
        T tmp_x = x-rhs.x, tmp_y = y-rhs.y;
        return sqrt(tmp_x*tmp_x+tmp_y*tmp_y);
    }
};

POS const operator-(const POS& lhs, const POS& rhs) {
    POS tmp = rhs;
    tmp = -tmp;
    return POS(lhs) += (tmp);
}

class LINE {
public:
    POS start, end, vec;
    double angle;
    LINE() {}
    LINE(const POS& start, const POS& end) :
        start(start), end(end), vec(end - start),
        angle(atan2(vec.x, vec.y)) {}
    LINE(const POS& end) :
        start(0, 0), end(end), vec(end), angle(atan2(vec.x, vec.y)) {}

    bool operator/(const LINE& rhs) const {
        return (vec.x*rhs.vec.y == vec.y*rhs.vec.x);
    }

    POS intersect(const LINE& rhs) const {
        if(*this==rhs) return COLINE;
        if(*this/rhs) return PARALLEL;
        double A1 = vec.y, B1 = -vec.x, C1 = end.x*start.y - start.x*end.y;
        double A2 = rhs.vec.y, B2 = -rhs.vec.x, C2 = rhs.end.x*rhs.start.y - rhs.start.x*rhs.end.y;
        return POS( (B2*C1-B1*C2)/(A2*B1-A1*B2), (A1*C2-A2*C1)/(A2*B1-A1*B2));
    }

    bool operator==(const LINE& rhs) const {
        return (*this/rhs) && (rhs.on_line(start));
    }

    bool on_line(const POS& a) const {
        if(vec.x == 0) {
            if(start.x != a.x) return false;
            return true;
        }
        if(vec.y == 0) {
            if(start.y != a.y) return false;
            return true;
        }
        return fabs(((a.x-start.x)/vec.x*vec.y + start.y)-a.y) < EPS;
    }
};

class LINESEG : public LINE {
public:
    LINESEG() : LINE(POS(0, 0)) {}
    LINESEG(const LINE& input) : LINE(input) {}
    LINESEG(const POS& start, const POS& end) : LINE(start, end) {}

    bool on_lineseg(const POS& a) const {
        if(!on_line(a)) return false;
        bool first, second;
        if(vec.x >= 0) first = (a.x >= start.x)&&(a.x <= end.x);
        else first = (a.x <= start.x)&&(a.x >= end.x);
        if(vec.y >= 0) second = (a.y >= start.y)&&(a.y <= end.y);
        else second = (a.y <= start.y)&&(a.y >= end.y);
        return first&&second;
    }

    T cross(const LINESEG& rhs) const {
        return vec.x*rhs.vec.y - vec.y*rhs.vec.x;
    }

    POS intersect(const LINESEG& rhs) const {
        LINE a1b1(start, rhs.start);
        LINE a1b2(start, rhs.end);
        LINE b1a1(rhs.start, start);
        LINE b1a2(rhs.start, end);

        POS tmp(this->LINE::intersect(rhs));

        if(tmp == COLINE) {
            if((start==rhs.start) && (!rhs.on_lineseg(end)) && (!on_lineseg(rhs.end))) return start;
            if((start==rhs.end) && (!rhs.on_lineseg(end)) && (!on_lineseg(rhs.start))) return start;
            if((end==rhs.end) && (!rhs.on_lineseg(start)) && (!on_lineseg(rhs.end))) return end;
            if((end==rhs.end) && (!rhs.on_lineseg(start)) && (!on_lineseg(rhs.start))) return end;
            if(on_lineseg(rhs.start) || on_lineseg(rhs.end) || rhs.on_lineseg(start) || rhs.on_lineseg(end)) return COLINE;
            return NO_INTERSECT;
        }

        bool intersected = ((cross(a1b1) * cross(a1b2) < 0) && (rhs.cross(b1a1)*(rhs.cross(b1a2)) < 0));
        if(!intersected) return NO_INTERSECT;
        if(!on_lineseg(tmp) || !rhs.on_lineseg(tmp)) return NO_INTERSECT;
        return tmp;
    }
};

int main()
{
    int q;
    cin >> q;
    cout << fixed << setprecision(10);
    while(q--){
        int xp0, yp0, xp1, yp1, xp2, yp2, xp3, yp3;
        cin >> xp0 >> yp0 >> xp1 >> yp1 >> xp2 >> yp2 >> xp3 >> yp3;
        POS p0(xp0, yp0), p1(xp1, yp1), p2(xp2, yp2), p3(xp3, yp3);
        LINESEG l0(p0, p1), l1(p2, p3);
        if(!(l0.intersect(l1) == NO_INTERSECT)){
            cout << 0.0000000000 << endl;
        } else {
            T ans = 1e14;
            ans = min(ans, p0.dist(p2));
            ans = min(ans, p0.dist(p3));
            ans = min(ans, p1.dist(p2));
            ans = min(ans, p1.dist(p3));
            cout << ans << endl;
        }
    }
}

Compiler output:
a.cc: In function 'int main()':
a.cc:145:22: error: no matching function for call to 'min(T&, double)'
  145 |             ans = min(ans, p0.dist(p2));
      |                   ~~~^~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:145:22: note:   deduced conflicting types for parameter 'const _Tp' ('long double' and 'double')
  145 |             ans = min(ans, p0.dist(p2));
      |                   ~~~^~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
a.cc:146:22: error: no matching function for call to 'min(T&, double)'
  146 |             ans = min(ans, p0.dist(p3));
      |                   ~~~^~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:146:22: note:   deduced conflicting types for parameter 'const _Tp' ('long double' and 'double')
  146 |             ans = min(ans, p0.dist(p3));
      |                   ~~~^~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
a.cc:147:22: error: no matching function for call to 'min(T&, double)'
  147 |             ans = min(ans, p1.dist(p2));
      |                   ~~~^~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:147:22: note:   deduced conflicting types for parameter 'const _Tp' ('long double' and 'double')
  147 |             ans = min(ans, p1.dist(p2));
      |                   ~~~^~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
a.cc:148:22: error: no matching function for call to 'min(T&, double)'
  148 |             ans = min(ans, p1.dist(p3));
      |                   ~~~^~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:148:22: note:   deduced conflicting types for parameter 'const _Tp' ('long double' and 'double')
  148 |             ans = min(ans, p1.dist(p3));
      |                   ~~~^~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided