結果

問題 No.5007 Steiner Space Travel
ユーザー komori3komori3
提出日時 2023-04-25 09:56:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 955 ms / 1,000 ms
コード長 15,866 bytes
コンパイル時間 3,282 ms
コンパイル使用メモリ 227,108 KB
実行使用メモリ 4,608 KB
スコア 8,980,505
最終ジャッジ日時 2023-04-25 09:57:08
合計ジャッジ時間 34,851 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 955 ms
4,372 KB
testcase_01 AC 955 ms
4,372 KB
testcase_02 AC 954 ms
4,608 KB
testcase_03 AC 955 ms
4,396 KB
testcase_04 AC 954 ms
4,368 KB
testcase_05 AC 954 ms
4,368 KB
testcase_06 AC 954 ms
4,376 KB
testcase_07 AC 954 ms
4,372 KB
testcase_08 AC 955 ms
4,368 KB
testcase_09 AC 954 ms
4,372 KB
testcase_10 AC 955 ms
4,368 KB
testcase_11 AC 954 ms
4,368 KB
testcase_12 AC 954 ms
4,368 KB
testcase_13 AC 954 ms
4,368 KB
testcase_14 AC 955 ms
4,368 KB
testcase_15 AC 954 ms
4,368 KB
testcase_16 AC 954 ms
4,372 KB
testcase_17 AC 954 ms
4,372 KB
testcase_18 AC 954 ms
4,608 KB
testcase_19 AC 954 ms
4,572 KB
testcase_20 AC 955 ms
4,372 KB
testcase_21 AC 955 ms
4,368 KB
testcase_22 AC 955 ms
4,396 KB
testcase_23 AC 954 ms
4,404 KB
testcase_24 AC 955 ms
4,372 KB
testcase_25 AC 955 ms
4,372 KB
testcase_26 AC 954 ms
4,464 KB
testcase_27 AC 954 ms
4,368 KB
testcase_28 AC 955 ms
4,368 KB
testcase_29 AC 954 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _CRT_NONSTDC_NO_WARNINGS
#define _SILENCE_CXX17_ITERATOR_BASE_CLASS_DEPRECATION_WARNING
#include <bits/stdc++.h>
#include <random>
#include <unordered_set>
#include <array>
#include <optional>
#ifdef _MSC_VER
#include <opencv2/core.hpp>
#include <opencv2/core/utils/logger.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <conio.h>
#include <ppl.h>
#include <filesystem>
#include <intrin.h>
/* g++ functions */
int __builtin_clz(unsigned int n) { unsigned long index; _BitScanReverse(&index, n); return 31 - index; }
int __builtin_ctz(unsigned int n) { unsigned long index; _BitScanForward(&index, n); return index; }
namespace std { inline int __lg(int __n) { return sizeof(int) * 8 - 1 - __builtin_clz(__n); } }
/* enable __uint128_t in MSVC */
//#include <boost/multiprecision/cpp_int.hpp>
//using __uint128_t = boost::multiprecision::uint128_t;
#else
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif

/** compro io **/
namespace aux {
    template<typename T, unsigned N, unsigned L> struct tp { static void output(std::ostream& os, const T& v) { os << std::get<N>(v) << ", "; tp<T, N + 1, L>::output(os, v); } };
    template<typename T, unsigned N> struct tp<T, N, N> { static void output(std::ostream& os, const T& v) { os << std::get<N>(v); } };
}
template<typename... Ts> std::ostream& operator<<(std::ostream& os, const std::tuple<Ts...>& t) { os << '{'; aux::tp<std::tuple<Ts...>, 0, sizeof...(Ts) - 1>::output(os, t); return os << '}'; } // tuple out
template<class Ch, class Tr, class Container> std::basic_ostream<Ch, Tr>& operator<<(std::basic_ostream<Ch, Tr>& os, const Container& x); // container out (fwd decl)
template<class S, class T> std::ostream& operator<<(std::ostream& os, const std::pair<S, T>& p) { return os << '{' << p.first << ", " << p.second << '}'; } // pair out
template<class S, class T> std::istream& operator>>(std::istream& is, std::pair<S, T>& p) { return is >> p.first >> p.second; } // pair in
std::ostream& operator<<(std::ostream& os, const std::vector<bool>::reference& v) { os << (v ? '1' : '0'); return os; } // bool (vector) out
std::ostream& operator<<(std::ostream& os, const std::vector<bool>& v) { bool f = true; os << '{'; for (const auto& x : v) { os << (f ? "" : ", ") << x; f = false; } os << '}'; return os; } // vector<bool> out
template<class Ch, class Tr, class Container> std::basic_ostream<Ch, Tr>& operator<<(std::basic_ostream<Ch, Tr>& os, const Container& x) { bool f = true; os << '{'; for (auto& y : x) { os << (f ? "" : ", ") << y; f = false; } return os << '}'; } // container out
template<class T, class = decltype(std::begin(std::declval<T&>())), class = typename std::enable_if<!std::is_same<T, std::string>::value>::type> std::istream& operator>>(std::istream& is, T& a) { for (auto& x : a) is >> x; return is; } // container in
template<typename T> auto operator<<(std::ostream& out, const T& t) -> decltype(out << t.stringify()) { out << t.stringify(); return out; } // struct (has stringify() func) out
/** io setup **/
struct IOSetup { IOSetup(bool f) { if (f) { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); } std::cout << std::fixed << std::setprecision(15); } }
iosetup(true); // set false when solving interective problems
/** string formatter **/
template<typename... Ts> std::string format(const std::string& f, Ts... t) { size_t l = std::snprintf(nullptr, 0, f.c_str(), t...); std::vector<char> b(l + 1); std::snprintf(&b[0], l + 1, f.c_str(), t...); return std::string(&b[0], &b[0] + l); }
/** dump **/
#define DUMPOUT std::cerr
std::ostringstream DUMPBUF;
#define dump(...) do{DUMPBUF<<"  ";DUMPBUF<<#__VA_ARGS__<<" :[DUMP - "<<__LINE__<<":"<<__FUNCTION__<<']'<<std::endl;DUMPBUF<<"    ";dump_func(__VA_ARGS__);DUMPOUT<<DUMPBUF.str();DUMPBUF.str("");DUMPBUF.clear();}while(0);
void dump_func() { DUMPBUF << std::endl; }
template <class Head, class... Tail> void dump_func(Head&& head, Tail&&... tail) { DUMPBUF << head; if (sizeof...(Tail) == 0) { DUMPBUF << " "; } else { DUMPBUF << ", "; } dump_func(std::move(tail)...); }
/** timer **/
class Timer {
    double t = 0, paused = 0, tmp;
public:
    Timer() { reset(); }
    static double time() {
#ifdef _MSC_VER
        return __rdtsc() / 2.3e9;
#else
        unsigned long long a, d;
        __asm__ volatile("rdtsc"
            : "=a"(a), "=d"(d));
        return (d << 32 | a) / 2.3e9;
#endif
    }
    void reset() { t = time(); }
    void pause() { tmp = time(); }
    void restart() { paused += time() - tmp; }
    double elapsed_ms() const { return (time() - t - paused) * 1000.0; }
};
/** rand **/
struct Xorshift {
    static constexpr uint64_t M = INT_MAX;
    static constexpr double e = 1.0 / M;
    uint64_t x = 88172645463325252LL;
    Xorshift() {}
    Xorshift(uint64_t seed) { reseed(seed); }
    inline void reseed(uint64_t seed) { x = 0x498b3bc5 ^ seed; for (int i = 0; i < 20; i++) next(); }
    inline uint64_t next() { x = x ^ (x << 7); return x = x ^ (x >> 9); }
    inline int next_int() { return next() & M; }
    inline int next_int(int mod) { return next() % mod; }
    inline int next_int(int l, int r) { return l + next_int(r - l + 1); }
    inline double next_double() { return next_int() * e; }
};
/** shuffle **/
template<typename T> void shuffle_vector(std::vector<T>& v, Xorshift& rnd) { int n = v.size(); for (int i = n - 1; i >= 1; i--) { int r = rnd.next_int(i); std::swap(v[i], v[r]); } }
/** split **/
std::vector<std::string> split(const std::string& str, const std::string& delim) {
    std::vector<std::string> res;
    std::string buf;
    for (const auto& c : str) {
        if (delim.find(c) != std::string::npos) {
            if (!buf.empty()) res.push_back(buf);
            buf.clear();
        }
        else buf += c;
    }
    if (!buf.empty()) res.push_back(buf);
    return res;
}
/** misc **/
template<typename A, size_t N, typename T> inline void Fill(A(&array)[N], const T& val) { std::fill((T*)array, (T*)(array + N), val); } // fill array
template<typename T, typename ...Args> auto make_vector(T x, int arg, Args ...args) { if constexpr (sizeof...(args) == 0)return std::vector<T>(arg, x); else return std::vector(arg, make_vector<T>(x, args...)); }
template<typename T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; }
template<typename T> bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; }
/** using **/
using ll = long long;
using ld = long double;
//using ld = boost::multiprecision::cpp_bin_float_quad;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;
using std::cin, std::cout, std::cerr, std::endl, std::string, std::vector, std::array;



constexpr int N = 100;
constexpr int M = 8;
constexpr int alpha = 5;
constexpr int alpha2 = alpha * alpha;

int xs[N + M + 1];
int ys[N + M + 1];

int C[N + M + 1][N + M + 1];
int nxt[N + M + 1][N + M + 1];

int ps[N + 1];

double lrnd[1 << 16];

struct LogTableInitializer {
    LogTableInitializer() {
        Xorshift rnd;
        for (int i = 0; i < (1 << 16); i++) {
            lrnd[i] = log(rnd.next_double());
        }
    }
} log_table_init_;

inline int dist2(int x1, int y1, int x2, int y2) {
    return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}

void update_dist(int K) {

    // planet - planet
    for (int u = 1; u + 1 <= N; u++) {
        for (int v = u + 1; v <= N; v++) {
            int d2 = (xs[u] - xs[v]) * (xs[u] - xs[v]) + (ys[u] - ys[v]) * (ys[u] - ys[v]);
            C[u][v] = C[v][u] = d2 * alpha * alpha;
        }
    }
    // planet - station
    for (int u = 1; u <= N; u++) {
        for (int v = N + 1; v <= N + M; v++) {
            int d2 = (xs[u] - xs[v]) * (xs[u] - xs[v]) + (ys[u] - ys[v]) * (ys[u] - ys[v]);
            C[u][v] = C[v][u] = d2 * alpha;
        }
    }
    // station - station
    for (int u = N + 1; u + 1 <= N + M; u++) {
        for (int v = u + 1; v <= N + M; v++) {
            int d2 = (xs[u] - xs[v]) * (xs[u] - xs[v]) + (ys[u] - ys[v]) * (ys[u] - ys[v]);
            C[u][v] = C[v][u] = d2;
        }
    }

    // Warshall-Floyd
    for (int u = 1; u <= N + M; u++) {
        for (int v = 1; v <= N + M; v++) {
            nxt[u][v] = v;
        }
    }
    for (int k = 1; k <= N + K; k++) {
        for (int u = 1; u <= N + K; u++) {
            for (int v = 1; v <= N + K; v++) {
                if (chmin(C[u][v], C[u][k] + C[k][v])) {
                    nxt[u][v] = nxt[u][k];
                }
            }
        }
    }

}

int calc_cost() {
    int cost = 0;
    for (int i = 0; i < N; i++) {
        cost += C[ps[i]][ps[i + 1]];
    }
    return cost;
}

inline int calc_diff(int i, int j) {
    int u1 = ps[i], u2 = ps[i + 1], v1 = ps[j], v2 = ps[j + 1];
    return C[u1][v1] + C[u2][v2] - C[u1][u2] - C[v1][v2];
}

inline int calc_diff(int pcost, int u, int dx, int dy) {
    xs[u] += dx; ys[u] += dy;
    int ncost = 0;
    for (int i = 0; i < N; i++) {
        int s = ps[i], t = ps[i + 1];
        while (s != t) {
            int v = nxt[s][t];
            int c = dist2(xs[s], ys[s], xs[v], ys[v]);
            if (s <= N && v <= N) c *= alpha2;
            else if (s <= N || v <= N) c *= alpha;
            ncost += c;
            s = v;
        }
    }
    xs[u] -= dx; ys[u] -= dy;
    return ncost - pcost;
}

inline double get_temp(double stemp, double etemp, double t, double T) {
    return etemp + (stemp - etemp) * (T - t) / T;
}

int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv) {

    Timer timer;

#ifdef HAVE_OPENCV_HIGHGUI
    cv::utils::logging::setLogLevel(cv::utils::logging::LogLevel::LOG_LEVEL_SILENT);
#endif

#if 0
    std::ifstream ifs("../../tools/in/0000.txt");
    std::istream& in = ifs;
    std::ofstream ofs("../../tools/out/0000.txt");
    std::ostream& out = ofs;
#else
    std::istream& in = cin;
    std::ostream& out = cout;
#endif

    Xorshift rnd;

    {
        int buf;
        in >> buf >> buf; // ignore N, M
        for (int i = 1; i <= N; i++) {
            in >> xs[i] >> ys[i];
        }
    }

    update_dist(0);

    for (int i = 0; i < N; i++) {
        ps[i] = i + 1;
    }
    ps[N] = 1;

    int cost = calc_cost();

    {
        double stime = timer.elapsed_ms(), ntime, etime = 200;
        size_t loop = 0;
        while ((ntime = timer.elapsed_ms()) < etime) {
            for (int i = 0; i + 1 < N; i++) {
                for (int j = i + 1; j < N; j++) {
                    loop++;
                    int diff = calc_diff(i, j);
                    double temp = get_temp(1e5, 0, ntime - stime, etime - stime);
                    if (-diff > temp * lrnd[loop & 0xFFFF]) {
                        std::reverse(ps + i + 1, ps + j + 1);
                        cost += diff;
                    }
                    if (!(loop & 0xFFFFFF)) {
                        dump(loop, cost);
                    }
                }
            }
        }
        dump(loop, cost);
    }

    {
        int fixed[N]; Fill(fixed, -1);
        int costs[N] = {};
        int best_score = cost;

        for (int k = 0; k < M; k++) {
            int tmp_score = best_score;
            int bcx = -1, bcy = -1;
            for (int cx = 0; cx <= 1000; cx += 10) {
                for (int cy = 0; cy <= 1000; cy += 10) {
                    int score = 0;
                    for (int i = 0; i < N; i++) {
                        if (fixed[i] != -1) {
                            score += costs[i];
                        }
                        else {
                            int u = ps[i], v = ps[i + 1];
                            int cost2 = (dist2(xs[u], ys[u], cx, cy) + dist2(cx, cy, xs[v], ys[v])) * alpha;
                            score += std::min(C[u][v], cost2);
                        }
                    }
                    if (chmin(tmp_score, score)) {
                        bcx = cx;
                        bcy = cy;
                    }
                }
            }
            {
                int cx = bcx, cy = bcy;
                for (int i = 0; i < N; i++) if (fixed[i] == -1) {
                    int u = ps[i], v = ps[i + 1];
                    int cost2 = (dist2(xs[u], ys[u], cx, cy) + dist2(cx, cy, xs[v], ys[v])) * alpha;
                    if (cost2 < C[u][v]) {
                        fixed[i] = k;
                        costs[i] = cost2;
                    }
                }
            }
            xs[k + N + 1] = bcx;
            ys[k + N + 1] = bcy;
            dump(tmp_score, bcx, bcy);
        }
    }

    update_dist(M);

    {
        cost = calc_cost();
        double stime = timer.elapsed_ms(), ntime, etime = 950;
        size_t loop = 0;
        while ((ntime = timer.elapsed_ms()) < etime) {
#if 1
            if (rnd.next_int(10)) {
                for (int i = 0; i + 1 < N; i++) {
                    for (int j = i + 1; j < N; j++) {
                        loop++;
                        int diff = calc_diff(i, j);
                        double temp = get_temp(2e4, 0, ntime - stime, etime - stime);
                        if (-diff > temp * lrnd[loop & 0xFFFF]) {
                            std::reverse(ps + i + 1, ps + j + 1);
                            cost += diff;
                        }
                        if (!(loop & 0xFFFFFF)) {
                            dump(loop, cost);
                        }
                    }
                }
            }
            else {
                int u = rnd.next_int(N + 1, N + M);
                int dx, dy;
                do {
                    dx = rnd.next_int(-10, 10);
                } while (xs[u] + dx < 0 || xs[u] + dx > 1000);
                do {
                    dy = rnd.next_int(-10, 10);
                } while (ys[u] + dy < 0 || ys[u] + dy > 1000);
                int diff = calc_diff(cost, u, dx, dy);
                if (diff < 0) {
                    xs[u] += dx; ys[u] += dy;
                    cost += diff;
                    update_dist(M);
                }
            }
#else
            loop++;
            int r = rnd.next_int(3000);
            if (r) {
                int i = rnd.next_int(N);
                int j = rnd.next_int(N - 1);
                j += (i == j);
                if (i > j) std::swap(i, j);
                int diff = calc_diff(i, j);
                double temp = get_temp(2e4, 0, ntime - stime, etime - stime);
                double prob = exp(-diff / temp);
                if (rnd.next_double() < prob) {
                    std::reverse(ps + i + 1, ps + j + 1);
                    cost += diff;
                }
            }
            else {
                int u = rnd.next_int(N + 1, N + M);
                int dx, dy;
                do {
                    dx = rnd.next_int(-10, 10);
                } while (xs[u] + dx < 0 || xs[u] + dx > 1000);
                do {
                    dy = rnd.next_int(-10, 10);
                } while (ys[u] + dy < 0 || ys[u] + dy > 1000);
                int diff = calc_diff(cost, u, dx, dy);
                if (diff < 0) {
                    xs[u] += dx; ys[u] += dy;
                    cost += diff;
                    update_dist(M);
                }
            }
            if (!(loop & 0xFFFFF)) {
                dump(loop, cost);
            }
#endif
        }
        dump(loop, cost);
    }

    for (int i = N + 1; i <= N + M; i++) {
        out << xs[i] << ' ' << ys[i] << '\n';
    }

    vector<int> ans;
    for (int i = 0; i < N; i++) {
        int from = ps[i], to = ps[i + 1];
        while (from != to) {
            ans.push_back(from);
            from = nxt[from][to];
        }
    }
    ans.push_back(1);
    out << ans.size() << '\n';
    for (int u : ans) {
        out << (u > N ? 2 : 1) << ' ' << (u > N ? u - N : u) << '\n';
    }
    out << "1 1" << '\n';

    return 0;
}
0