結果
問題 | No.5007 Steiner Space Travel |
ユーザー | shibh308 |
提出日時 | 2022-07-30 14:31:01 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 154 ms / 1,000 ms |
コード長 | 7,352 bytes |
コンパイル時間 | 2,604 ms |
実行使用メモリ | 6,952 KB |
スコア | 7,796,670 |
最終ジャッジ日時 | 2022-07-30 14:31:10 |
合計ジャッジ時間 | 8,453 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge15 |
純コード判定しない問題か言語 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 124 ms
4,900 KB |
testcase_01 | AC | 131 ms
4,904 KB |
testcase_02 | AC | 127 ms
4,904 KB |
testcase_03 | AC | 151 ms
4,904 KB |
testcase_04 | AC | 129 ms
4,904 KB |
testcase_05 | AC | 121 ms
4,904 KB |
testcase_06 | AC | 140 ms
6,948 KB |
testcase_07 | AC | 132 ms
4,904 KB |
testcase_08 | AC | 153 ms
4,900 KB |
testcase_09 | AC | 139 ms
4,900 KB |
testcase_10 | AC | 140 ms
4,904 KB |
testcase_11 | AC | 124 ms
4,900 KB |
testcase_12 | AC | 147 ms
4,900 KB |
testcase_13 | AC | 118 ms
6,948 KB |
testcase_14 | AC | 121 ms
4,904 KB |
testcase_15 | AC | 141 ms
4,904 KB |
testcase_16 | AC | 140 ms
4,904 KB |
testcase_17 | AC | 154 ms
4,904 KB |
testcase_18 | AC | 137 ms
4,900 KB |
testcase_19 | AC | 138 ms
4,900 KB |
testcase_20 | AC | 119 ms
4,904 KB |
testcase_21 | AC | 150 ms
4,900 KB |
testcase_22 | AC | 119 ms
6,948 KB |
testcase_23 | AC | 131 ms
4,900 KB |
testcase_24 | AC | 127 ms
4,904 KB |
testcase_25 | AC | 122 ms
6,948 KB |
testcase_26 | AC | 131 ms
6,952 KB |
testcase_27 | AC | 141 ms
4,904 KB |
testcase_28 | AC | 136 ms
4,904 KB |
testcase_29 | AC | 119 ms
4,904 KB |
ソースコード
#include <bits/stdc++.h> // #include "atcoder/all" using namespace std; using i64 = long long; const i64 MOD = 1e9 + 7; const i64 INF = i64(1e18); template <typename T> bool chmin(T& x, T y){ if(x > y){ x = y; return true; } return false; } template <typename T> bool chmax(T& x, T y){ if(x < y){ x = y; return true; } return false; } uint32_t rnd(){ static uint32_t x = 123456789, y = 362436069, z = 521288629, w = 0; uint32_t t = x ^ (x << 11); x = y; y = z; z = w; w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); return w; } double rnd_double(){ return 1.0 * rnd() / numeric_limits<uint32_t>::max(); } // #define OPTIMIZE namespace params{ void load_params(){ ifstream ifs("../params.txt"); assert(ifs.is_open()); } } // doc: https://shibh308.github.io/library/library/lib/functions/tsp.cpp.html template<typename T> vector<int> tsp(int n, vector<vector<T>> dist){ // 始点=終点のTSP n <= 2 とかでバグりそうなので注意 vector<int> v(n); vector<int> inv(n); // 適当に初期解を生成 iota(v.begin(), v.end(), 0); T now_dist = 0; for(int i = 0; i < n; ++i){ inv[v[i]] = i; now_dist += dist[v[i]][v[(i + 1) % n]]; } // 前計算 vector<vector<pair<T,int>>> near_vertexes(n); for(int i = 0; i < n; ++i){ for(int j = 0; j < n; ++j){ near_vertexes[i].emplace_back(dist[i][j], j); } sort(near_vertexes[i].begin(), near_vertexes[i].end()); } auto dist_check = [&](){ auto di = dist[0][0]; for(int i = 0; i < n; ++i){ di += dist[v[i]][v[(i + 1) % n]]; } assert(abs(now_dist - di) <= 1e-6); }; auto update = [&](int i1, int i3){ int i2 = (i1 + 1) % n; int i4 = (i3 + 1) % n; if(i1 == i3 || i2 == i3 || i1 == i4 || i2 == i4) return false; if(dist[v[i1]][v[i2]] + dist[v[i3]][v[i4]] <= dist[v[i1]][v[i3]] + dist[v[i2]][v[i4]]) return false; auto improve = (dist[v[i1]][v[i2]] + dist[v[i3]][v[i4]]) - (dist[v[i1]][v[i3]] + dist[v[i2]][v[i4]]); now_dist -= improve; int d1 = (i2 < i3 ? i3 - i2 : i3 + n - i2); int d2 = (i4 < i1 ? i1 - i4 : i1 + n - i4); if(d1 >= d2){ swap(i1, i3); swap(i2, i4); } int st = i2; int en = i2 < i3 ? i3 : i3 + n; while(st < en){ inv[v[en % n]] = st % n; inv[v[st % n]] = en % n; swap(v[st % n], v[en % n]); ++st, --en; } // バグが怖い時用 // dist_check(); return true; }; auto neighbors_search = [&](){ for(int i = 0; i < n; ++i){ int idx1 = i; int idx2 = (i + 1) % n; int v1 = v[idx1]; int v2 = v[idx2]; for(auto [d, v3] : near_vertexes[v1]){ if(v3 == v2) break; int idx3 = inv[v3]; if(update(idx1, idx3)){ return true; } } for(auto [d, v4] : near_vertexes[v2]){ if(v4 == v1) break; int idx3 = (inv[v4] + n - 1) % n; if(update(idx1, idx3)){ return true; } } } return false; }; auto double_bridge = [&](){ vector<uint32_t> r{rnd() % n, rnd() % n, rnd() % n, rnd() % n}; sort(r.begin(), r.end()); if(r[0] == r[1] || r[1] == r[2] || r[2] == r[3]){ return true; } // (r[3] + 1, r[0]) => (r[2] + 1, r[3]) => (r[1] + 1, r[2]) => (r[0] + 1, r[1]) auto diff = dist[0][0]; diff -= dist[v[r[0]]][v[r[0] + 1]]; diff -= dist[v[r[1]]][v[r[1] + 1]]; diff -= dist[v[r[2]]][v[r[2] + 1]]; diff -= dist[v[r[3]]][v[(r[3] + 1) % n]]; diff += dist[v[r[0]]][v[r[2] + 1]]; diff += dist[v[r[3]]][v[r[1] + 1]]; diff += dist[v[r[2]]][v[r[0] + 1]]; diff += dist[v[r[1]]][v[(r[3] + 1) % n]]; now_dist += diff; vector<int> nex; nex.reserve(n); for(int i = r[3] + 1; i < n; ++i) nex.emplace_back(v[i]); for(int i = 0; i <= r[0]; ++i) nex.emplace_back(v[i]); for(int i = r[2] + 1; i <= r[3]; ++i) nex.emplace_back(v[i]); for(int i = r[1] + 1; i <= r[2]; ++i) nex.emplace_back(v[i]); for(int i = r[0] + 1; i <= r[1]; ++i) nex.emplace_back(v[i]); v = move(nex); return false; }; auto best = make_pair(now_dist, v); for(int i = 0; i < 1000; ++i){ if(i){ while(double_bridge()); } while(neighbors_search()); if(!chmin(best, make_pair(now_dist, v))){ tie(now_dist, v) = best; } } for(int i = 0; i < n; ++i){ if(v[i] == 0){ vector<int> res; for(int j = 0; j < n; ++j){ res.emplace_back(v[(i + j) % n]); } return res; } } // return now_dist; return v; } constexpr int n = 100; constexpr int m = 8; constexpr int alpha = 5; array<i64, n> ax, ay; void read_file(istream& ifs){ int a; ifs >> a >> a; // TODO: input for(int i = 0; i < n; ++i){ ifs >> ax[i] >> ay[i]; } } clock_t st; void solve(){ vector<vector<i64>> a_dist(n, vector<i64>(n)); vector<i64> bx(n), by(n); auto raw_dis = [&](auto p1, auto q1, auto p2, auto q2){ i64 dx = (p1 == 0 ? ax[q1] : bx[q1]) - (p2 == 0 ? ax[q2] : bx[q2]); i64 dy = (p1 == 0 ? ay[q1] : by[q1]) - (p2 == 0 ? ay[q2] : by[q2]); return dx * dx + dy * dy; }; auto dis = [&](auto p1, auto q1, auto p2, auto q2){ int alp = 1; alp *= (p1 == 0 ? 5 : 1); alp *= (p2 == 0 ? 5 : 1); return raw_dis(p1, q1, p2, q2) * alp; }; for(int i = 0; i < n; ++i){ for(int j = 0; j < n; ++j){ a_dist[i][j] = dis(0, i, 0, j); } } bx = {200, 400, 600, 800}; by = {333, 333, 333, 333, 666, 666, 666, 666}; for(int i = 0; i < 8; ++i){ bx[i] = bx[i % 4]; } auto res = tsp(100, a_dist); res.emplace_back(0); vector<pair<int,int>> ans; ans.emplace_back(0, 0); for(int i = 0; i < n; ++i){ i64 d = dis(0, res[i], 0, res[i + 1]); pair<i64,int> pp(d, -1); for(int j = 0; j < m; ++j){ i64 d2 = dis(0, res[i], 1, j) + dis(1, j, 0, res[i + 1]); chmin(pp, make_pair(d2, j)); } if(pp.second != -1){ ans.emplace_back(1, pp.second); } ans.emplace_back(0, res[i + 1]); } for(int i = 0; i < m; ++i){ cout << bx[i] << " " << by[i] << endl; } cout << ans.size() << endl; for(int i = 0; i < ans.size(); ++i){ cout << ans[i].first + 1 << " " << ans[i].second + 1 << endl; } cout << 1 << " " << 1 << endl; } signed main(){ st = clock(); #ifdef OPTIMIZE params::load_params(); #endif #ifndef HAND read_file(cin); #else ifstream ifs("./tools/in/0001.txt"); assert(ifs.is_open()); read_file(ifs); #endif solve(); }