結果

問題 No.5007 Steiner Space Travel
ユーザー snow39snow39
提出日時 2022-08-02 11:59:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 953 ms / 1,000 ms
コード長 9,600 bytes
コンパイル時間 1,449 ms
実行使用メモリ 3,800 KB
スコア 8,392,560
最終ジャッジ日時 2022-08-02 12:00:32
合計ジャッジ時間 32,997 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 953 ms
3,564 KB
testcase_01 AC 953 ms
3,624 KB
testcase_02 AC 953 ms
3,564 KB
testcase_03 AC 953 ms
3,560 KB
testcase_04 AC 953 ms
3,604 KB
testcase_05 AC 952 ms
3,564 KB
testcase_06 AC 953 ms
3,676 KB
testcase_07 AC 953 ms
3,712 KB
testcase_08 AC 953 ms
3,728 KB
testcase_09 AC 953 ms
3,716 KB
testcase_10 AC 952 ms
3,712 KB
testcase_11 AC 953 ms
3,720 KB
testcase_12 AC 953 ms
3,628 KB
testcase_13 AC 953 ms
3,744 KB
testcase_14 AC 953 ms
3,736 KB
testcase_15 AC 953 ms
3,676 KB
testcase_16 AC 952 ms
3,688 KB
testcase_17 AC 953 ms
3,556 KB
testcase_18 AC 953 ms
3,728 KB
testcase_19 AC 953 ms
3,800 KB
testcase_20 AC 953 ms
3,756 KB
testcase_21 AC 952 ms
3,564 KB
testcase_22 AC 953 ms
3,564 KB
testcase_23 AC 953 ms
3,680 KB
testcase_24 AC 953 ms
3,724 KB
testcase_25 AC 953 ms
3,656 KB
testcase_26 AC 953 ms
3,740 KB
testcase_27 AC 953 ms
3,760 KB
testcase_28 AC 953 ms
3,724 KB
testcase_29 AC 953 ms
3,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <chrono>
#include <iostream>
#include <random>
#include <string>
#include <tuple>
#include <vector>

#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
template <class T>
void chmin(T &a, const T &b) {
    if (a > b) a = b;
}
template <class T>
void chmax(T &a, const T &b) {
    if (a < b) a = b;
}

constexpr int N = 100;
constexpr int M = 8;
constexpr int ALPHA = 5;

constexpr int PLANET = 1;
constexpr int STATION = 2;

class Timer {
public:
    chrono::time_point<chrono::system_clock> start;

    Timer() {}

    void init() {
        start = chrono::system_clock::now();
    }

    int get_ms() {
        auto cur = chrono::system_clock::now();
        return chrono::duration_cast<chrono::milliseconds>(cur - start).count();
    }
};

struct Coordinate {
    int x;
    int y;

    Coordinate(int x = -1, int y = -1) : x(x), y(y) {}

    bool operator==(const Coordinate &rhs) const {
        return x == rhs.x && y == rhs.y;
    }
    bool operator!=(const Coordinate &rhs) const {
        return x != rhs.x || y != rhs.y;
    }

    int get_manhattan_dist(const Coordinate &other) const {
        int dx = abs(x - other.x);
        int dy = abs(y - other.y);
        return dx + dy;
    }

    int get_euclid_dist_squared(const Coordinate &other) const {
        int dx = abs(x - other.x);
        int dy = abs(y - other.y);
        return dx * dx + dy * dy;
    }
};

class DisjointSet {
public:
    int N;
    vector<int> par, rank;
    vector<int> sz;

    DisjointSet() {}
    DisjointSet(int N) : N(N), par(N), rank(N), sz(N) {
        for (int i = 0; i < N; i++) make_set(i);
    }

    bool same(int x, int y) {
        return leader(x) == leader(y);
    }

    void unite(int x, int y) {
        if (same(x, y)) return;
        link(leader(x), leader(y));
    }

    int leader(int x) {
        if (x != par[x]) par[x] = leader(par[x]);
        return par[x];
    }

    int size(int x) {
        return sz[leader(x)];
    }

private:
    void make_set(int x) {
        par[x] = x;
        rank[x] = 0;
        sz[x] = 1;
    }

    void link(int x, int y) {
        if (rank[x] > rank[y]) swap(x, y);

        par[x] = y;
        sz[y] += sz[x];
        if (rank[x] == rank[y]) {
            ++rank[y];
        }
    }
};

struct Planet {
    int id;
    Coordinate pos;
    Planet() {}
    Planet(int id, Coordinate pos) : id(id), pos(pos) {}
};

struct Station {
    int id;
    Coordinate pos;
    Station() {}
    Station(int id, Coordinate pos) : id(id), pos(pos) {}
};

struct Input {
    vector<Planet> planets;
    Input(vector<Planet> planets) : planets(planets) {}
};

struct Output {
    int type;
    int id;
    Output(int type, int id) : type(type), id(id) {}
};

Timer timer;

Input read_input() {
    // Timer timer;
    timer.init();

    int n, m;
    cin >> n >> m;
    vector<Planet> planets;
    for (int i = 0; i < N; i++) {
        int a, b;
        cin >> a >> b;
        planets.emplace_back(i, Coordinate(a, b));
    }

    Input input(planets);
    return input;
}

void output(vector<Station> stations, vector<Output> outputs) {
    for (auto stat : stations) cout << stat.pos.x << " " << stat.pos.y << "\n";

    cout << outputs.size() << "\n";
    for (auto out : outputs) cout << out.type << " " << out.id + 1 << "\n";
}

int calc_score(const vector<Output> &outputs, const vector<Planet> &planets,
               const vector<Station> &stations) {
    int sum_dist = 0;
    for (size_t i = 0; i + 1 < outputs.size(); i++) {
        Coordinate from =
            (outputs[i].type == PLANET ? planets[outputs[i].id].pos : stations[outputs[i].id].pos);
        Coordinate to = (outputs[i + 1].type == PLANET ? planets[outputs[i + 1].id].pos
                                                       : stations[outputs[i + 1].id].pos);
        int coef = 1;
        if (outputs[i].type == PLANET) coef *= ALPHA;
        if (outputs[i + 1].type == PLANET) coef *= ALPHA;
        int dist = coef * from.get_euclid_dist_squared(to);
        sum_dist += dist;
    }
    return round(1e9 / (1000 + sqrt(sum_dist)));
}

pair<vector<vector<int>>, vector<vector<int>>> init_dist(const vector<Planet> &planets,
                                                         const vector<Station> &stations) {
    vector<Coordinate> objects;
    rep(i, N) objects.emplace_back(planets[i].pos);
    rep(i, M) objects.emplace_back(stations[i].pos);

    const int V = objects.size();
    const int inf = 1e9;
    vector<vector<int>> dist(V, vector<int>(V, inf));
    vector<vector<int>> nxt(V, vector<int>(V, inf));
    rep(i, V) rep(j, V) {
        int coef = 1;
        if (i < N) coef *= 5;
        if (j < N) coef *= 5;
        int cost = coef * objects[i].get_euclid_dist_squared(objects[j]);
        dist[i][j] = cost;
        nxt[i][j] = j;
    }

    rep(k, V) rep(i, V) rep(j, V) {
        if (dist[i][j] > dist[i][k] + dist[k][j]) {
            dist[i][j] = dist[i][k] + dist[k][j];
            nxt[i][j] = nxt[i][k];
        }
    }

    return make_pair(dist, nxt);
}

vector<Station> locate_stations_by_k_means(const vector<Planet> &planets) {
    vector<int> cluster(N, 0);
    random_device seed_gen;
    mt19937 rng(seed_gen());
    rep(i, N) cluster[i] = rng() % M;

    vector<double> cx(M, 0), cy(M, 0);
    for (int it = 0; it < 100; it++) {
        for (auto &x : cx) x = 0;
        for (auto &y : cy) y = 0;

        vector<int> cluster_size(M, 0);
        rep(i, N) {
            const int id = cluster[i];
            cluster_size[id]++;
            cx[id] += planets[i].pos.x;
            cy[id] += planets[i].pos.y;
        }
        rep(i, M) {
            if (cluster_size[i] == 0) {
                cx[i] = rng() % 1000;
                cy[i] = rng() % 1000;
            } else {
                cx[i] /= cluster_size[i];
                cy[i] /= cluster_size[i];
            }
        }

        rep(i, N) {
            const int inf = 1e9;
            int min_dist = inf;
            int min_cluster = 0;
            rep(j, M) {
                int dist = planets[i].pos.get_euclid_dist_squared(Coordinate(cx[j], cy[j]));
                if (dist < min_dist) {
                    min_dist = dist;
                    min_cluster = j;
                }
            }
            cluster[i] = min_cluster;
        }
    }

    vector<Station> stations(M);
    rep(i, M) stations[i] = Station(i, Coordinate(round(cx[i]), round(cy[i])));
    return stations;
}

vector<Output> solve_TSP_by_2_opt(const vector<Planet> &planets, const vector<Station> &stations) {
    const auto [dist, nxt] = init_dist(planets, stations);

    vector<int> visit_order;
    rep(i, N) visit_order.push_back(i);
    visit_order.push_back(0);
    const int V = visit_order.size();

    const int TIME_LIMIT = 950;
    random_device seed_gen;
    mt19937 rng(seed_gen());
    while (1) {
        if (timer.get_ms() > TIME_LIMIT) break;

        int a = 1 + rng() % (V - 2);
        int b = 1 + rng() % (V - 2);
        while (abs(a - b) <= 1) b = 1 + rng() % (V - 2);
        if (a > b) swap(a, b);

        int bef_dist =
            dist[visit_order[a]][visit_order[a + 1]] + dist[visit_order[b]][visit_order[b + 1]];
        int aft_dist =
            dist[visit_order[a]][visit_order[b]] + dist[visit_order[a + 1]][visit_order[b + 1]];
        if (bef_dist > aft_dist) {
            reverse(visit_order.begin() + a + 1, visit_order.begin() + b + 1);
        }
    }

    vector<Output> outputs;
    outputs.emplace_back(PLANET, 0);
    rep(i, V - 1) {
        int a = visit_order[i];
        int b = visit_order[i + 1];
        int now = a;
        while (nxt[now][b] != b) {
            if (nxt[now][b] < N)
                outputs.emplace_back(PLANET, nxt[now][b]);
            else
                outputs.emplace_back(STATION, nxt[now][b] - N);
            now = nxt[now][b];
        }
        if (b < N)
            outputs.emplace_back(PLANET, b);
        else
            outputs.emplace_back(STATION, b - N);
    }
    return outputs;
}

void solve(const Input &input) {
    /*
    int best_score = 0;
    vector<Station> best_stations;
    vector<Output> best_outputs;

    const int TIME_LIMIT = 940;
    random_device seed_gen;
    mt19937 rng(seed_gen());
    while (1) {
        if (timer.get_ms() > TIME_LIMIT) break;

        vector<Station> tmp_stations(M);
        rep(i, M) {
            const int id = rng() % N;
            tmp_stations[i] = Station(id, input.planets[id].pos);
            // tmp_stations[i] = Station(i, Coordinate(rng() % 1000, rng() % 1000));
        }
        vector<Output> tmp_outputs = solve_TSP_by_2_opt(input.planets, tmp_stations);
        int tmp_score = calc_score(tmp_outputs, input.planets, tmp_stations);
        if (best_score < tmp_score) {
            swap(best_score, tmp_score);
            swap(best_stations, tmp_stations);
            swap(best_outputs, tmp_outputs);
        }
    }
    output(best_stations, best_outputs);
    cerr << "best_score: " << best_score << endl;
    */

    vector<Station> stations = locate_stations_by_k_means(input.planets);

    /*
    vector<Station> stations;
    rep(i, M) stations.emplace_back(i, input.planets[i].pos);
    */

    vector<Output> outputs = solve_TSP_by_2_opt(input.planets, stations);

    output(stations, outputs);

    cerr << "end time: " << timer.get_ms() << "[ms]" << endl;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    Input input = read_input();
    solve(input);

    return 0;
}
0