結果

問題 No.5007 Steiner Space Travel
ユーザー assy1028assy1028
提出日時 2022-07-30 17:28:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 993 ms / 1,000 ms
コード長 11,295 bytes
コンパイル時間 1,582 ms
実行使用メモリ 4,268 KB
スコア 8,543,729
最終ジャッジ日時 2022-07-30 17:28:49
合計ジャッジ時間 33,979 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
純コード判定しない問題か言語
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 991 ms
4,028 KB
testcase_01 AC 989 ms
4,168 KB
testcase_02 AC 988 ms
4,080 KB
testcase_03 AC 991 ms
4,216 KB
testcase_04 AC 990 ms
4,084 KB
testcase_05 AC 990 ms
4,080 KB
testcase_06 AC 991 ms
3,944 KB
testcase_07 AC 987 ms
4,172 KB
testcase_08 AC 989 ms
4,124 KB
testcase_09 AC 988 ms
3,944 KB
testcase_10 AC 990 ms
3,944 KB
testcase_11 AC 989 ms
4,028 KB
testcase_12 AC 988 ms
4,020 KB
testcase_13 AC 988 ms
4,172 KB
testcase_14 AC 988 ms
4,224 KB
testcase_15 AC 989 ms
4,032 KB
testcase_16 AC 989 ms
4,120 KB
testcase_17 AC 987 ms
3,936 KB
testcase_18 AC 988 ms
4,120 KB
testcase_19 AC 991 ms
3,936 KB
testcase_20 AC 990 ms
4,076 KB
testcase_21 AC 991 ms
4,052 KB
testcase_22 AC 987 ms
4,124 KB
testcase_23 AC 993 ms
4,056 KB
testcase_24 AC 989 ms
4,136 KB
testcase_25 AC 988 ms
4,268 KB
testcase_26 AC 988 ms
4,056 KB
testcase_27 AC 989 ms
3,948 KB
testcase_28 AC 989 ms
4,056 KB
testcase_29 AC 987 ms
4,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
#include <numeric>
#include <cmath>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <complex>
#include <string.h>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <iomanip>
#include <sys/time.h>
#include <tuple>
#include <random>
using namespace std;

#define endl '\n'
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef complex<double> comp;
typedef vector< vector<ld> > matrix;
struct pairhash {
public:
    template<typename T, typename U>
    size_t operator()(const pair<T, U> &x) const {
    size_t seed = hash<T>()(x.first);
    return hash<U>()(x.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
    }
};
const int INF = 1e9 + 9;
//const ll INF = 1e18 + 9;
const ll MOD = 1e9 + 7;
//const ll MOD = 998244353;
const double EPS = 1e-8;
const double PI = acos(-1);

int n, m;
int a[110], b[110];

int dist[110][110];

const int alpha = 5;
const int alpha2 = 25;

double get_elapsed_time(struct timeval *begin, struct timeval *end) {
    return (end->tv_sec - begin->tv_sec) * 1000
         + (end->tv_usec - begin->tv_usec) / 1000.0;
}

unsigned long xor128(void) {
    static unsigned long x=123456789,y=362436069,z=521288629,w=88675123;
    unsigned long t;
    t=(x^(x<<11));x=y;y=z;z=w; return( w=(w^(w>>19))^(t^(t>>8)) );
}

// [0, k)
int rand(int k) {
    return xor128() % k;
}

// [a, b)
int rand(int a, int b) {
    return a + xor128() % (b - a);
}

// [0, 1)
double rand_double() {
    return (double)rand(INF) / INF;
}

int calc_dist(const int i, const int j) {
    return (a[i] - a[j]) * (a[i] - a[j]) + (b[i] - b[j]) * (b[i] - b[j]);
}

int calc_dist(const int i, const pair<int, int>& p) {
    return (a[i] - p.first) * (a[i] - p.first) + (b[i] - p.second) * (b[i] - p.second);
}

int calc_dist(const pair<int, int>& p0, const pair<int, int>& p1) {
    return (p0.first - p1.first) * (p0.first - p1.first) + (p0.second - p1.second) * (p0.second - p1.second);
}

int calc_diff(const int i, const int j, const vector<int>& order) {
    const int i0 = order[i], i1 = order[i-1];
    const int j0 = order[j], j1 = order[j+1];
    const int d_cur  = dist[i1][i0] + dist[j0][j1];
    const int d_next = dist[i1][j0] + dist[i0][j1];
    return d_cur - d_next;
}

int calc_diff_s(const int i, const int j, const vector<int>& order, const vector<int>& order_rev, const vector<int>& exist) {
    const int order_i = order_rev[i], order_j = order_rev[j];
    const int i_prev = order[max(0, order_i-1)];
    const int i_next = order[order_i+1];
    const int j_prev = order[max(0, order_j-1)];
    const int j_next = order[order_j+1];
    int d_cur = 0;
    d_cur += (exist[i_prev] >= 0 ? dist[i_prev][i] : alpha * dist[i_prev][i]);
    d_cur += (exist[i_next] >= 0 ? dist[i][i_next] : alpha * dist[i][i_next]);
    d_cur += (exist[j_prev] >= 0 ? alpha * dist[j_prev][j] : alpha2 * dist[j_prev][j]);
    d_cur += (exist[j_next] >= 0 ? alpha * dist[j][j_next] : alpha2 * dist[j][j_next]);
    int d_next = 0;
    d_next += (exist[i_prev] >= 0 ? alpha * dist[i_prev][i] : alpha2 * dist[i_prev][i]);
    d_next += (exist[i_next] >= 0 ? alpha * dist[i][i_next] : alpha2 * dist[i][i_next]);
    d_next += (exist[j_prev] >= 0 ? dist[j_prev][j] : alpha * dist[j_prev][j]);
    d_next += (exist[j_next] >= 0 ? dist[j][j_next] : alpha * dist[j][j_next]);
    return d_cur - d_next;
}

int calc_tour_dist(const vector<int>& order, const vector<pair<int, int>>& stations) {
    int d = 0;
    for (int i = 0; i < n; i++) {
        int d0 = alpha2 * dist[order[i]][order[i+1]];
        for (const auto& p : stations) {
            d0 = min(d0, alpha * (calc_dist(order[i], p) + calc_dist(order[i+1], p)));
        }
        for (int j = 0; j < m; j++) {
            for (int k = 0; k < m; k++) {
                if (j == k) continue;
                d0 = min(d0, alpha * calc_dist(order[i], stations[j]) + calc_dist(stations[j], stations[k]) + alpha * calc_dist(order[i+1], stations[k]));
            }
        }
        d += d0;
    }
    return d;
}

int calc_tour_dist(const vector<int>& order, const pair<int, int>& p) {
    int d = 0;
    for (int i = 0; i < n; i++) {
        int d0 = alpha2 * dist[order[i]][order[i+1]];
        d0 = min(d0, alpha * (calc_dist(order[i], p) + calc_dist(order[i+1], p)));
        d += d0;
    }
    return d;
}

vector<pair<int, int>> make_tour(const vector<int>& order, const vector<pair<int, int>>& stations) {
    vector<pair<int, int>> res;
    res.push_back(make_pair(1, 1));
    for (int i = 0; i < n; i++) {
        int s0 = -1, s1 = -1;
        int d0 = alpha2 * dist[order[i]][order[i+1]];
        for (int j = 0; j < m; j++) {
            const auto& p = stations[j];
            const int d1 = alpha * (calc_dist(order[i], p) + calc_dist(order[i+1], p));
            if (d0 > d1) {
                d0 = d1;
                s0 = j;
            }
        }
        for (int j = 0; j < m; j++) {
            for (int k = 0; k < m; k++) {
                if (j == k) continue;
                const int d1 = alpha * calc_dist(order[i], stations[j]) + calc_dist(stations[j], stations[k]) + alpha * calc_dist(order[i+1], stations[k]);
                if (d0 > d1) {
                    d0 = d1;
                    s0 = j;
                    s1 = k;
                }
            }
        }
        if (s0 < 0) {
            res.push_back(make_pair(1, order[i+1]+1));
        } else if (s1 < 0) {
            res.push_back(make_pair(2, s0+1));
            res.push_back(make_pair(1, order[i+1]+1));
        } else {
            res.push_back(make_pair(2, s0+1));
            res.push_back(make_pair(2, s1+1));
            res.push_back(make_pair(1, order[i+1]+1));
        }
    }
    return res;
}

void init() {
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            dist[i][j] = calc_dist(i, j);
}

pair<vector<pair<int, int>>, vector<pair<int, int>>> solve() {
    init();
    vector<int> order;
    for (int i = 0; i < n; i++) order.push_back(i);
    order.push_back(0);

    struct timeval t1, t2;
    gettimeofday(&t1, NULL);

    int iter;
    double T_init = 10;
    double T_end = 0.01;
    double T = T_init;
    double TIME_LIMIT = 100;
    int move = 0;
    for (iter = 1; ; iter++) {
        int i, j;
        i = rand(1, n);
        while (i == (j = rand(1, n)));
        if (i > j) swap(i, j);

        const int diff = calc_diff(i, j, order);
        if (diff > 0 || exp(diff/T) > rand_double()) {
            reverse(order.begin()+i, order.begin()+j+1);
        }

         if (iter % 100 == 0) {
            gettimeofday(&t2, NULL);
            const double et = get_elapsed_time(&t1, &t2);
            if (et > TIME_LIMIT) break;
            T = (T_end - T_init) / TIME_LIMIT * et + T_init;
        }
    }
    vector<int> order_rev(n);
    int d_plain = 0;
    for (int i = 0; i < n; i++) {
        order_rev[order[i]] = i;
        d_plain += alpha2 * dist[order[i]][order[i+1]];
    }

    vector<pair<int, pair<int, int>>> vec;
    for (int x = 10; x < 1000; x+=10) {
        for (int y = 10; y < 1000; y+=10) {
            const int d = calc_tour_dist(order, make_pair(x, y));
            vec.push_back(make_pair(d, make_pair(x, y)));
        }
    }
    sort(ALL(vec));
    vector<pair<int, int>> cand;
    const int K = 100;
    for (const auto& p : vec) {
        const auto& pos = p.second;
        bool near = false;
        for (const auto& q : cand) {
            const int d = calc_dist(pos, q);
            if (d < 5000) {
                near = true;
                break;
            }
        }
        if (near) continue;
        cand.push_back(pos);
        if ((int)cand.size() >= K) break;
    }

    int p[n];
    for (int i = 0; i < K; i++) p[i] = i;
    vector<pair<int, int>> stations;
    vector<int> s_pos;
    vector<int> exist(K, -1);
    for (int i = 0; i < m; i++) {
        const int j = rand(n-i);
        const int idx = p[j];
        stations.push_back(cand[idx]);
        s_pos.push_back(idx);
        exist[p[j]] = i;
        swap(p[j], p[n-i-1]);
    }

    gettimeofday(&t1, NULL);
    T_init = 100000;
    T_end = 0.01;
    T = T_init;
    TIME_LIMIT = 500;
    move = 0;
    for (iter = 1; ; iter++) {
        const int s_idx = rand(m);
        const int i = s_pos[s_idx];
        int j;
        while (true) {
            j = rand(K);
            if (exist[j] < 0) break;
        }

        const int d_cur = calc_tour_dist(order, stations);
        //stations[s_idx].first = a[j];
        //stations[s_idx].second = b[j];
        stations[s_idx] = cand[j];
        const int d_next = calc_tour_dist(order, stations);
        const int diff = d_cur - d_next;
        if (diff > 0 || exp(diff/T) > rand_double()) {
            s_pos[s_idx] = j;
            exist[i] = -1;
            exist[j] = s_idx;
            move++;
        } else {
            stations[s_idx] = cand[i];
            //stations[s_idx].first = a[i];
            //stations[s_idx].second = b[i];
        }

         if (iter % 100 == 0) {
            gettimeofday(&t2, NULL);
            const double et = get_elapsed_time(&t1, &t2);
            if (et > TIME_LIMIT) break;
            T = (T_end - T_init) / TIME_LIMIT * et + T_init;
        }
    }
    cerr << "iter: " << iter << endl;
    cerr << "move: " << move << endl;

    gettimeofday(&t1, NULL);
    T_init = 200;
    T_end = 0.01;
    T = T_init;
    TIME_LIMIT = 380;
    move = 0;
    for (iter = 1; ; iter++) {
        const int s_idx = rand(m);
        const int dx = rand(1, 20) * (rand(2) == 0 ? -1 : 1);
        const int dy = rand(1, 20) * (rand(2) == 0 ? -1 : 1);

        const int d_cur = calc_tour_dist(order, stations);
        stations[s_idx].first += dx;
        stations[s_idx].second += dy;
        const int d_next = calc_tour_dist(order, stations);
        const int diff = d_cur - d_next;
        if (diff > 0 || exp(diff/T) > rand_double()) {
            move++;
        } else {
            stations[s_idx].first -= dx;
            stations[s_idx].second -= dy;
        }

         if (iter % 100 == 0) {
            gettimeofday(&t2, NULL);
            const double et = get_elapsed_time(&t1, &t2);
            if (et > TIME_LIMIT) break;
            T = (T_end - T_init) / TIME_LIMIT * et + T_init;
        }
    }
    cerr << "iter: " << iter << endl;
    cerr << "move: " << move << endl;

    vector<pair<int, int>> res = make_tour(order, stations);

    return make_pair(stations, res);
}

void input() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
}

void output(const pair<vector<pair<int, int>>, vector<pair<int, int>>>& res) {
    for (const auto& s : res.first) {
        cout << s.first << " " << s.second << endl;
    }
    cout << (int)res.second.size() << endl;
    for (const auto& s : res.second) {
        cout << s.first << " " << s.second << endl;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);

    input();
    const auto& res = solve();
    output(res);
}
0