結果

問題 No.5007 Steiner Space Travel
ユーザー assy1028assy1028
提出日時 2022-08-01 14:31:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 972 ms / 1,000 ms
コード長 9,499 bytes
コンパイル時間 1,299 ms
実行使用メモリ 4,284 KB
スコア 8,789,236
最終ジャッジ日時 2022-08-01 14:31:47
合計ジャッジ時間 33,181 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 971 ms
4,196 KB
testcase_01 AC 972 ms
4,016 KB
testcase_02 AC 972 ms
4,280 KB
testcase_03 AC 972 ms
4,076 KB
testcase_04 AC 972 ms
4,228 KB
testcase_05 AC 972 ms
4,000 KB
testcase_06 AC 971 ms
4,280 KB
testcase_07 AC 972 ms
4,008 KB
testcase_08 AC 972 ms
4,004 KB
testcase_09 AC 972 ms
4,276 KB
testcase_10 AC 972 ms
4,284 KB
testcase_11 AC 972 ms
4,184 KB
testcase_12 AC 971 ms
4,028 KB
testcase_13 AC 972 ms
4,004 KB
testcase_14 AC 972 ms
4,016 KB
testcase_15 AC 972 ms
4,192 KB
testcase_16 AC 972 ms
4,200 KB
testcase_17 AC 972 ms
4,004 KB
testcase_18 AC 972 ms
4,084 KB
testcase_19 AC 972 ms
4,188 KB
testcase_20 AC 972 ms
4,084 KB
testcase_21 AC 972 ms
4,280 KB
testcase_22 AC 971 ms
4,108 KB
testcase_23 AC 972 ms
4,104 KB
testcase_24 AC 972 ms
4,116 KB
testcase_25 AC 972 ms
4,124 KB
testcase_26 AC 972 ms
4,280 KB
testcase_27 AC 972 ms
4,116 KB
testcase_28 AC 972 ms
4,180 KB
testcase_29 AC 972 ms
4,008 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=23456789,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_dist_from_index(const pair<int, int>& p0, const pair<int, int>& p1, const vector<pair<int, int>>& pos) {
    if (p0.first == 1 && p1.first == 1) {
        return alpha2 * dist[p0.second][p1.second];
    } else if (p0.first == 1 && p1.first == 2) {
        return alpha * calc_dist(p0.second, pos[p1.second]);
    } else if (p0.first == 2 && p1.first == 1) {
        return alpha * calc_dist(p1.second, pos[p0.second]);
    } else {
        return calc_dist(pos[p0.second], pos[p1.second]);
    }
}

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

int calc_diff_insert(const int i, const pair<int, int>& p, const vector<pair<int, int>>& order, const vector<pair<int, int>>& pos) {
    const auto i0 = order[i-1], i1 = order[i];
    const int d_cur  = calc_dist_from_index(i0, i1, pos);
    const int d_next = calc_dist_from_index(i0, p, pos) + calc_dist_from_index(p, i1, pos);
    return d_cur - d_next;
}

int calc_diff_erase(const int i, const vector<pair<int, int>>& order, const vector<pair<int, int>>& pos) {
    const auto i0 = order[i-1], i1 = order[i], i2 = order[i+1];
    const int d_cur  = calc_dist_from_index(i0, i1, pos) + calc_dist_from_index(i1, i2, pos);
    const int d_next = calc_dist_from_index(i0, i2, pos);
    return d_cur - d_next;
}

int calc_diff_swap(const int i, const int j, const vector<pair<int, int>>& order, const vector<pair<int, int>>& pos) {
    return calc_diff_erase(i, order, pos) + calc_diff_insert(j, order[i], order, pos);
}

int calc_dist_via_station(const int i, const vector<pair<int, int>>& order, const vector<pair<int, int>>& pos) {
    const int L = (int)order.size();
    int d = 0;
    for (int j = 0; j < L; j++) {
        if (order[j].first == 2 && order[j].second == i) {
            d += calc_dist_from_index(order[j-1], order[j], pos);
            d += calc_dist_from_index(order[j], order[j+1], pos);
        }
    }
    return d;
}

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<pair<int, int>> pos;
    int p[n];
    for (int i = 0; i < n; i++) p[i] = i;
    for (int i = 0; i < m; i++) {
        const int j = rand(n-i);
        const int idx = p[j];
        pos.push_back(make_pair(a[idx], b[idx]));
        swap(p[j], p[n-i-1]);
    }

    vector<pair<int, int>> order;
    for (int i = 0; i < n; i++) {
        order.push_back(make_pair(1, i));
        for (int j = 0; j < m; j++) {
            if (pos[j].first == a[i] && pos[j].second == b[i]) {
                order.push_back(make_pair(2, j));
                break;
            }
        }
    }
    order.push_back(make_pair(1, 0));

    vector<int> count_p(n, 1);
    vector<int> count_s(m, 1);

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

    int iter;
    double T_init = 100000;
    double T_end = 0.01;
    double T = T_init;
    double TIME_LIMIT = 970;
    int move = 0;
    int delta = 100;
    for (iter = 1; ; iter++) {
        const int t = rand(11);
        const int L = (int)order.size();
        if (t < 3) {
            // 2-opt
            int i, j;
            i = rand(1, L-1);
            while (i == (j = rand(1, L-1)));
            if (i > j) swap(i, j);
            const int diff = calc_diff_2_opt(i, j, order, pos);
            if (diff > 0 || exp(diff/T) > rand_double()) {
                reverse(order.begin()+i, order.begin()+j+1);
                move++;
            }
        } else if (t < 4) {
            // insert
            const int ps = (rand(5) == 0 ? 1 : 2);
            const int idx = (ps == 1 ? rand(n) : rand(m));
            const pair<int, int> p = make_pair(ps, idx);
            const int i = rand(1, L);

            const int diff = calc_diff_insert(i, p, order, pos);
            if (diff > 0 || exp(diff/T) > rand_double()) {
                order.insert(order.begin()+i, p);
                move++;
                if (ps == 1) {
                    count_p[idx]++;
                } else {
                    count_s[idx]++;
                }
            }
        } else if (t < 7) {
            // erase
            const int i = rand(1, L-1);
            const int ps = order[i].first;
            const int idx = order[i].second;
            if ((ps == 1 && count_p[idx] < 2) ||
                (ps == 2 && count_s[idx] < 2)) {
                continue;
            }

            const int diff = calc_diff_erase(i, order, pos);
            if (diff > 0 || exp(diff/T) > rand_double()) {
                order.erase(order.begin()+i);
                if (ps == 1) {
                    count_p[idx]--;
                } else {
                    count_s[idx]--;
                }
            }
        } else if (t < 8) {
            // swap
            int i, j;
            i = rand(1, L-1);
            while (true) {
                j = rand(1, L-1);
                if (abs(i - j) > 1) break;
            }
            
            const int diff = calc_diff_swap(i, j, order, pos);
            if (diff > 0 || exp(diff/T) > rand_double()) {
                swap(order[i], order[j]);
            }
        } else {
            // move
            const int i = rand(m);
            const int dx = rand(1, delta) * (rand(2) == 0 ? 1 : -1);
            const int dy = rand(1, delta) * (rand(2) == 0 ? 1 : -1);

            const int d_cur = calc_dist_via_station(i, order, pos);
            pos[i].first += dx;
            pos[i].second += dy;
            const int d_next = calc_dist_via_station(i, order, pos);
            const double diff = (double)(d_cur - d_next) / count_s[i];
            if (diff > 0 || exp(diff/T) > rand_double()) {
                move++;
            } else {
                pos[i].first -= dx;
                pos[i].second -= dy;
            }
        }
        if (iter % 1000 == 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;
            T *= 0.9975;
            delta = (1000 - (int)et) / 5;
        }
    }
    cerr << "iter: " << iter << endl;
    cerr << "move: " << move << endl;
    cerr << "T: " << T << endl;

    return make_pair(pos, order);
}

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+1 << endl;
    }
}

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

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