結果

問題 No.5007 Steiner Space Travel
ユーザー assy1028assy1028
提出日時 2022-07-30 16:33:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 985 ms / 1,000 ms
コード長 9,898 bytes
コンパイル時間 1,493 ms
実行使用メモリ 4,096 KB
スコア 8,280,290
最終ジャッジ日時 2022-07-30 16:34:22
合計ジャッジ時間 33,701 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 982 ms
4,000 KB
testcase_01 AC 983 ms
3,820 KB
testcase_02 AC 985 ms
3,820 KB
testcase_03 AC 983 ms
3,956 KB
testcase_04 AC 982 ms
4,096 KB
testcase_05 AC 982 ms
3,924 KB
testcase_06 AC 982 ms
4,088 KB
testcase_07 AC 982 ms
3,808 KB
testcase_08 AC 984 ms
4,088 KB
testcase_09 AC 983 ms
3,816 KB
testcase_10 AC 983 ms
3,924 KB
testcase_11 AC 983 ms
3,956 KB
testcase_12 AC 982 ms
3,848 KB
testcase_13 AC 983 ms
3,924 KB
testcase_14 AC 981 ms
3,952 KB
testcase_15 AC 982 ms
3,820 KB
testcase_16 AC 983 ms
4,092 KB
testcase_17 AC 984 ms
3,808 KB
testcase_18 AC 983 ms
4,040 KB
testcase_19 AC 983 ms
4,092 KB
testcase_20 AC 983 ms
4,000 KB
testcase_21 AC 982 ms
3,940 KB
testcase_22 AC 984 ms
3,956 KB
testcase_23 AC 982 ms
3,856 KB
testcase_24 AC 983 ms
3,992 KB
testcase_25 AC 982 ms
3,944 KB
testcase_26 AC 984 ms
4,076 KB
testcase_27 AC 984 ms
4,008 KB
testcase_28 AC 985 ms
3,816 KB
testcase_29 AC 984 ms
3,992 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;
}

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);
    for (int i = 0; i < n; i++) {
        order_rev[order[i]] = i;
    }

    int p[n];
    for (int i = 0; i < n; i++) p[i] = i;
    vector<int> s_pos;
    vector<int> exist(n, -1);
    for (int i = 0; i < m; i++) {
        const int j = rand(n-i);
        s_pos.push_back(p[j]);
        exist[p[j]] = i;
        swap(p[j], p[n-i-1]);
    }
    gettimeofday(&t1, NULL);
    T_init = 20000;
    T_end = 0.01;
    T = T_init;
    TIME_LIMIT = 100;
    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(n);
            if (exist[j] < 0) break;
        }

        const int diff = calc_diff_s(i, j, order, order_rev, exist);
        if (diff > 0 || exp(diff/T) > rand_double()) {
            s_pos[s_idx] = j;
            exist[i] = -1;
            exist[j] = s_idx;
            move++;
        }

         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>> stations;
    for (int i = 0; i < m; i++) {
        const int idx = s_pos[i];
        stations.push_back(make_pair(a[idx], b[idx]));
    }

    gettimeofday(&t1, NULL);
    T_init = 200;
    T_end = 0.01;
    T = T_init;
    TIME_LIMIT = 780;
    move = 0;
    for (iter = 1; ; iter++) {
        const int s_idx = rand(m);
        const int dx = rand(1, 10) * (rand(2) == 0 ? -1 : 1);
        const int dy = rand(1, 10) * (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