Submission #14065649


Source Code Expand

Copy
#include <bits/stdc++.h>

using ll   = long long;
using uint = unsigned int;
using ull  = unsigned long long;
using ld   = double;
template<typename T, int n>
using arr = T (&)[n];
template<typename T, int n>
using c_arr = const T (&)[n];
template<typename T>
using max_heap = std::priority_queue<T>;
template<typename T>
using min_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
constexpr int popcount(const ull v) { return v ? __builtin_popcountll(v) : 0; }
constexpr int log2p1(const ull v) { return v ? 64 - __builtin_clzll(v) : 0; }
constexpr int lsbp1(const ull v) { return __builtin_ffsll(v); }
constexpr int clog(const ull v) { return v ? log2p1(v - 1) : 0; }
constexpr ull ceil2(const ull v) { return 1ULL << clog(v); }
constexpr ull floor2(const ull v) { return v ? (1ULL << (log2p1(v) - 1)) : 0ULL; }
constexpr bool btest(const ull mask, const int ind) { return (mask >> ind) & 1ULL; }
template<typename T> void bset(T& mask, const int ind) { mask |= ((T)1 << ind); }
template<typename T> void breset(T& mask, const int ind) { mask &= ~((T)1 << ind); }
template<typename T> void bflip(T& mask, const int ind) { mask ^= ((T)1 << ind); }
template<typename T> void bset(T& mask, const int ind, const bool b) { (b ? bset(mask, ind) : breset(mask, ind)); }
template<typename T> bool chmin(T& a, const T& b) { return (a > b ? a = b, true : false); }
template<typename T> bool chmax(T& a, const T& b) { return (a < b ? a = b, true : false); }

template<typename T> constexpr T inf_v      = std::numeric_limits<T>::max() / 4;
template<typename Real> constexpr Real pi_v = Real{3.141592653589793238462643383279502884};
constexpr ull TEN(const int n) { return n == 0 ? 1ULL : TEN(n - 1) * 10ULL; }
template<typename F> struct fix : F
{
    fix(F&& f) : F{std::forward<F>(f)} {}
    template<typename... Args> auto operator()(Args&&... args) const { return F::operator()(*this, std::forward<Args>(args)...); }
};
class printer
{
public:
    printer(std::ostream& os_ = std::cout) : os{os_} {}
    template<typename T> int operator()(const T& v) { return os << v, 0; }
    template<typename T> int operator()(const std::vector<T>& vs)
    {
        for (int i = 0; i < (int)vs.size(); i++) { os << (i ? " " : ""), this->operator()(vs[i]); }
        return 0;
    }
    template<typename T> int operator()(const std::vector<std::vector<T>>& vss)
    {
        for (int i = 0; i < (int)vss.size(); i++) { os << (0 <= i or i + 1 < (int)vss.size() ? "\n" : ""), this->operator()(vss[i]); }
        return 0;
    }
    template<typename T, typename... Args> int operator()(const T& v, const Args&... args) { return this->operator()(v), os << ' ', this->operator()(args...), 0; }
    template<typename... Args> int ln(const Args&... args) { return this->operator()(args...), os << '\n', 0; }
    template<typename... Args> int el(const Args&... args) { return this->operator()(args...), os << std::endl, 0; }
    template<typename... Args> int fmt(const std::string& s, const Args&... args) { return rec(s, 0, args...); }

private:
    int rec(const std::string& s, int index) { return os << s.substr(index, s.size()), 0; }
    template<typename T, typename... Args> int rec(const std::string& s, int index, const T& v, const Args&... args) { return index == s.size() ? 0 : s[index] == '%' ? (this->operator()(v), rec(s, index + 1, args...)) : (os << s[index], rec(s, index + 1, v, args...)); }
    std::ostream& os;
};
printer out;

template<typename T, typename F> std::vector<T> generated(const int n, F f)
{
    std::vector<T> ans(n);
    return std::generate(ans.begin(), ans.end(), f), ans;
}
std::vector<int> ioted(const int n, const int offset = 0)
{
    std::vector<int> ans(n);
    return std::iota(ans.begin(), ans.end(), offset), ans;
}
template<typename Vs> Vs reversed(const Vs& vs)
{
    auto ans = vs;
    return std::reverse(ans.begin(), ans.end()), ans;
}
template<typename T, typename F = std::less<T>> std::vector<T> sorted(const std::vector<T>& vs, F comp = F{})
{
    auto ans = vs;
    return std::sort(ans.begin(), ans.end(), comp), ans;
}
template<typename F> std::vector<int> sorted_iota(const int n, F comp = F{}, const int offset = 0) { return sorted(ioted(n, offset), comp); }
class scanner
{
public:
    scanner(std::istream& is_ = std::cin) : is{is_} { is.tie(nullptr), std::ios::sync_with_stdio(false); }
    template<typename T> T val()
    {
        static T v;
        return is >> v, v;
    }
    template<typename T> T val(const T offset) { return val<T>() - offset; }
    template<typename T> std::vector<T> vec(const int n)
    {
        return generated<T>(n, [&] { return val<T>(); });
    }
    template<typename T> std::vector<T> vec(const int n, const T offset)
    {
        return generated<T>(n, [&] { return val<T>(offset); });
    }
    template<typename T> std::vector<std::vector<T>> vvec(const int n0, const int n1)
    {
        return generated<std::vector<T>>(n0, [&] { return vec<T>(n1); });
    }
    template<typename T> std::vector<std::vector<T>> vvec(const int n0, const int n1, const T offset)
    {
        return generated<std::vector<T>>(n0, [&] { return vec<T>(n1, offset); });
    }
    template<typename... Args> auto tup() { return std::tuple<std::decay_t<Args>...>{val<Args>()...}; }
    template<typename... Args> auto tup(const Args&... offsets) { return std::tuple<std::decay_t<Args>...>{val<Args>(offsets)...}; }

private:
    std::istream& is;
};
scanner in;
#    define SHOW(...) static_cast<void>(0)

template<typename T, typename Int, int n, int i> auto make_v(typename std::enable_if<(i == n), c_arr<Int, n>>::type, const T& v = T{}) { return v; }
template<typename T, typename Int, int n, int i> auto make_v(typename std::enable_if<(i < n), c_arr<Int, n>>::type szs, const T& v = T{}) { return std::vector<decltype(make_v<T, Int, n, i + 1>(szs, v))>(szs[i], make_v<T, Int, n, i + 1>(szs, v)); }
template<typename T, typename Int, int n> auto make_v(c_arr<Int, n> szs, const T& t = T{}) { return make_v<T, Int, n, 0>(szs, t); }


template<typename T = int>
struct edge
{
    using cost_type = T;
    int u, v;
    T c;
    edge(const int u_, const int v_) : u{u_}, v{v_}, c{1} {}
    edge(const int u_, const int v_, const T& c_) : u{u_}, v{v_}, c{c_} {}
    operator int() const { return v; }
    int from() const { return u; }
    int to() const { return v; }
    T cost() const { return c; }
    friend std::ostream& operator<<(std::ostream& os, const edge& e) { return os << e.u << "->" << e.v << ":" << e.c; }
};
template<typename Edge>
class base_graph
{
public:
    base_graph(const int n) : sz{n}, es(n), res(0) {}
    void add_edge(const int u, const int v, const bool bi = false)
    {
        es[u].emplace_back(u, v);
        if (bi) { es[v].emplace_back(v, u); }
    }
    template<typename Cost>
    void add_edge(const int u, const int v, const Cost& c, const bool bi = false)
    {
        es[u].emplace_back(u, v, c);
        if (bi) { es[v].emplace_back(v, u, c); }
    }
    std::vector<Edge>& operator[](const int u) { return es[u]; }
    const std::vector<Edge>& operator[](const int u) const { return es[u]; }
    std::vector<Edge>& from(const int u) { return es[u]; }
    const std::vector<Edge>& from(const int u) const { return es[u]; }
    std::vector<Edge>& to(const int v) { return res[v]; }
    const std::vector<Edge>& to(const int v) const { return res[v]; }
    int size() const { return sz; }
    friend std::ostream& operator<<(std::ostream& os, const base_graph& g)
    {
        for (int i = 0; i < g.sz; i++) {
            for (const auto& e : g.es[i]) { os << e << '\n'; }
        }
        return os;
    }

private:
    int sz;
    std::vector<std::vector<Edge>> es, res;
};
template<typename Edge>
using base_tree = base_graph<Edge>;
using graph     = base_graph<edge<>>;
using tree      = base_graph<edge<>>;
template<typename Cost>
using cost_graph = base_graph<edge<Cost>>;
template<typename Cost>
using cost_tree = base_graph<edge<Cost>>;
template<typename Edge>
std::vector<typename Edge::cost_type> dijkstra(const base_graph<Edge>& g, const int s)
{
    using T = typename Edge::cost_type;
    std::vector<T> d(g.size(), inf_v<T>);
    using P = std::pair<T, int>;
    std::priority_queue<P, std::vector<P>, std::greater<P>> q;
    d[s] = 0, q.push({0, s});
    while (not q.empty()) {
        const T cost = q.top().first;
        const int v  = q.top().second;
        q.pop();
        if (d[v] < cost) { continue; }
        for (const auto& e : g[v]) {
            const auto c = e.cost();
            const int to = e.to();
            if (d[to] <= d[v] + c) { continue; }
            d[to] = d[v] + c, q.push({d[to], to});
        }
    }
    return d;
}
using C          = std::complex<ld>;
using L          = std::pair<C, C>;
constexpr ld eps = 1e-8;
ld Sqrt(ld x) { return x < 0 ? (ld)0 : std::sqrt(x); }
C normalize(const C& c) { return c / std::abs(c); }
ld getarg(const C& a, const C& b) { return std::arg(b * std::conj(a)); }
ld cross(const C& a, const C& b) { return std::imag(std::conj(a) * b); }
ld dot(const C& a, const C& b) { return std::real(std::conj(a) * b); }
C rot(const C& a, const C& b, const ld& th) { return b + (a - b) * C(1, th); }
int ccw(C a, C b, C c)
{
    b -= a;
    c -= a;
    if (cross(b, c) > 0) return +1;              // counter clockwise
    if (cross(b, c) < 0) return -1;              // clockwise
    if (dot(b, c) < 0) return +2;                // c--a--b on line
    if (std::norm(b) < std::norm(c)) return -2;  // a--b--c on line
    return 0;                                    // a--c--b on line
}
C projection(const L& l, const C& p)
{
    const ld t = dot(p - l.first, l.first - l.second) / std::norm(l.first - l.second);
    return l.first + t * (l.first - l.second);
}
ld get(const C& c1, const C& c2) { return (dot(c1, c2) < 0 ? (ld)-1.0 : (ld)1.0) * std::abs(c2) / std::abs(c1); }
bool intersectSP(const L& s, const C p) { return std::abs(s.first - p) + std::abs(s.second - p) - std::abs(s.second - s.first) < eps; }
ld distanceSP(const L& s, const C p)
{
    const C r = projection(s, p);
    if (intersectSP(s, r)) return std::abs(r - p);
    return std::min(abs(s.first - p), std::abs(s.second - p));
}
ld distanceLP(const L& l, const C& p) { return std::abs(p - projection(l, p)); }

int main()
{
    const auto N  = in.val<int>();
    const auto v  = in.val<ld>();
    const auto sx = in.val<ld>(), sy = in.val<ld>(), tx = in.val<ld>(), ty = in.val<ld>();
    const auto s = C{sx, sy}, t = C{tx, ty};
    std::vector<C> ps{s};
    for (int i = 0; i < N; i++) {
        ld x, y;
        std::cin >> x >> y;
        ps.push_back(C{x, y});
    }
    ps.push_back(t);

    auto comp = [](const C& c1, const C& c2) { return c1.real() != c2.real() ? c1.real() < c2.real() : c1.imag() < c2.imag(); };
    std::map<C, int, decltype(comp)> zip(comp);
    for (int i = 0; i < ps.size(); i++) { zip[ps[i]] = 0; }
    for (int i = 0; i < ps.size(); i++) {
        for (int j = 1; j < N; j++) {
            const auto pp = ps[j], np = ps[j + 1];
            const auto l  = L{pp, np};
            const auto q  = projection(l, ps[i]);
            const auto d  = distanceLP(l, ps[i]);
            const auto r  = d / std::sqrt(v * v - 1);
            const auto dq = (r / std::abs(np - pp)) * (np - pp);
            const auto q1 = q - dq, q2 = q + dq;
            std::vector<C> qs{q, q1, q2, pp, np};
            for (const auto& q : qs) {
                if (intersectSP(l, q)) { zip[q] = 0; }
            }
        }
    }
    std::vector<C> unzip;
    for (const auto& p : zip) { unzip.push_back(p.first); }
    const int V = unzip.size();
    for (int i = 0; i < V; i++) { zip[unzip[i]] = i; }
    cost_graph<ld> g(V);
    const int S = zip[s], T = zip[t];
    std::vector<std::vector<int>> ls(V);
    for (int i = 0; i < V; i++) {
        const auto pi = unzip[i];
        for (int k = 1; k < N; k++) {
            const auto pp = ps[k], np = ps[k + 1];
            const auto l = L{pp, np};
            if (intersectSP(l, pi)) { ls[i].push_back(k); }
        }
    }

    for (int i = 0; i < V; i++) {
        for (int j = i + 1; j < V; j++) {
            const auto pi = unzip[i], pj = unzip[j];
            std::vector<int> inter;
            std::set_intersection(ls[i].begin(), ls[i].end(), ls[j].begin(), ls[j].end(), std::back_inserter(inter));
            const bool on = not inter.empty();
            g.add_edge(i, j, std::abs(pi - pj) / (on ? v : (ld)1), true);
        }
    }
    const auto ds = dijkstra(g, S);
    std::cout << std::fixed << std::setprecision(15) << ds[T] << std::endl;
    return 0;
}

Submission Info

Submission Time
Task D - Rail Tour
User pachicobue
Language C++14 (GCC 5.4.1)
Score 100
Code Size 12905 Byte
Status
Exec Time 537 ms
Memory 224508 KB

Judge Result

Set Name Score / Max Score Test Cases
All 100 / 100 00-sample-00, 00-sample-01, 00-sample-02, 00-sample-03, 00-sample-04, 10-random_large-00, 10-random_large-01, 10-random_large-02, 10-random_large-03, 10-random_large-04, 10-random_large-05, 10-random_large-06, 10-random_large-07, 10-random_large-08, 10-random_large-09, 20-random_v_small-00, 20-random_v_small-01, 20-random_v_small-02, 20-random_v_small-03, 20-random_v_small-04, 20-random_v_small-05, 20-random_v_small-06, 20-random_v_small-07, 20-random_v_small-08, 20-random_v_small-09, 30-complex_graph-00
Case Name Status Exec Time Memory
00-sample-00 1 ms 256 KB
00-sample-01 1 ms 256 KB
00-sample-02 1 ms 256 KB
00-sample-03 1 ms 256 KB
00-sample-04 1 ms 256 KB
10-random_large-00 135 ms 62448 KB
10-random_large-01 157 ms 70256 KB
10-random_large-02 126 ms 61716 KB
10-random_large-03 234 ms 109036 KB
10-random_large-04 164 ms 72048 KB
10-random_large-05 255 ms 117360 KB
10-random_large-06 140 ms 61036 KB
10-random_large-07 163 ms 67184 KB
10-random_large-08 159 ms 65200 KB
10-random_large-09 187 ms 72300 KB
20-random_v_small-00 163 ms 67388 KB
20-random_v_small-01 179 ms 71152 KB
20-random_v_small-02 259 ms 120304 KB
20-random_v_small-03 95 ms 49052 KB
20-random_v_small-04 191 ms 96248 KB
20-random_v_small-05 237 ms 112116 KB
20-random_v_small-06 223 ms 104820 KB
20-random_v_small-07 93 ms 48960 KB
20-random_v_small-08 214 ms 102384 KB
20-random_v_small-09 230 ms 111984 KB
30-complex_graph-00 537 ms 224508 KB