結果
問題 | No.5007 Steiner Space Travel |
ユーザー | shibh308 |
提出日時 | 2022-07-30 14:40:06 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 529 ms / 1,000 ms |
コード長 | 8,221 bytes |
コンパイル時間 | 2,851 ms |
実行使用メモリ | 3,964 KB |
スコア | 8,773,339 |
最終ジャッジ日時 | 2022-07-30 14:40:31 |
合計ジャッジ時間 | 20,291 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge12 |
純コード判定しない問題か言語 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 504 ms
3,828 KB |
testcase_01 | AC | 516 ms
3,876 KB |
testcase_02 | AC | 506 ms
3,960 KB |
testcase_03 | AC | 504 ms
3,828 KB |
testcase_04 | AC | 521 ms
3,852 KB |
testcase_05 | AC | 503 ms
3,748 KB |
testcase_06 | AC | 504 ms
3,832 KB |
testcase_07 | AC | 525 ms
3,960 KB |
testcase_08 | AC | 504 ms
3,876 KB |
testcase_09 | AC | 504 ms
3,888 KB |
testcase_10 | AC | 526 ms
3,880 KB |
testcase_11 | AC | 503 ms
3,748 KB |
testcase_12 | AC | 504 ms
3,800 KB |
testcase_13 | AC | 526 ms
3,808 KB |
testcase_14 | AC | 504 ms
3,804 KB |
testcase_15 | AC | 504 ms
3,808 KB |
testcase_16 | AC | 529 ms
3,780 KB |
testcase_17 | AC | 504 ms
3,860 KB |
testcase_18 | AC | 504 ms
3,828 KB |
testcase_19 | AC | 527 ms
3,860 KB |
testcase_20 | AC | 503 ms
3,964 KB |
testcase_21 | AC | 504 ms
3,960 KB |
testcase_22 | AC | 528 ms
3,880 KB |
testcase_23 | AC | 504 ms
3,756 KB |
testcase_24 | AC | 504 ms
3,756 KB |
testcase_25 | AC | 504 ms
3,768 KB |
testcase_26 | AC | 504 ms
3,884 KB |
testcase_27 | AC | 505 ms
3,880 KB |
testcase_28 | AC | 504 ms
3,856 KB |
testcase_29 | AC | 504 ms
3,960 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); auto calc_ans = [&](){ vector<pair<int,int>> ans; ans.emplace_back(0, 0); i64 ds = 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)); } ds += pp.first; if(pp.second != -1){ ans.emplace_back(1, pp.second); } ans.emplace_back(0, res[i + 1]); } return make_pair(ans, ds); }; auto best = calc_ans(); bool fl = true; while(fl){ for(int _ = 0; _ < 100; ++_){ if(double(clock() - st) / CLOCKS_PER_SEC >= 0.5){ fl = false; break; } int idx = rnd() & 7; int dx = rnd() % 200 - 100; int dy = rnd() % 200 - 100; bx[idx] += dx; by[idx] += dy; auto now = calc_ans(); auto diff = best.second - now.second; if(diff >= 0){ best = now; } else{ bx[idx] -= dx; by[idx] -= dy; } } } auto ans = calc_ans().first; 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(); }