結果
問題 | No.2855 Move on Grid |
ユーザー | Daniel K |
提出日時 | 2024-08-25 16:11:16 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,043 ms / 3,000 ms |
コード長 | 5,235 bytes |
コンパイル時間 | 1,890 ms |
コンパイル使用メモリ | 141,964 KB |
実行使用メモリ | 16,600 KB |
最終ジャッジ日時 | 2024-08-25 16:11:50 |
合計ジャッジ時間 | 33,036 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 200 ms
6,816 KB |
testcase_01 | AC | 267 ms
6,940 KB |
testcase_02 | AC | 85 ms
6,944 KB |
testcase_03 | AC | 121 ms
6,944 KB |
testcase_04 | AC | 78 ms
6,940 KB |
testcase_05 | AC | 184 ms
6,944 KB |
testcase_06 | AC | 187 ms
6,940 KB |
testcase_07 | AC | 19 ms
6,944 KB |
testcase_08 | AC | 145 ms
6,940 KB |
testcase_09 | AC | 76 ms
6,944 KB |
testcase_10 | AC | 938 ms
16,464 KB |
testcase_11 | AC | 1,018 ms
16,484 KB |
testcase_12 | AC | 1,015 ms
16,596 KB |
testcase_13 | AC | 990 ms
16,600 KB |
testcase_14 | AC | 939 ms
16,368 KB |
testcase_15 | AC | 995 ms
16,500 KB |
testcase_16 | AC | 1,018 ms
16,592 KB |
testcase_17 | AC | 954 ms
16,452 KB |
testcase_18 | AC | 913 ms
16,456 KB |
testcase_19 | AC | 985 ms
16,436 KB |
testcase_20 | AC | 1,014 ms
14,796 KB |
testcase_21 | AC | 927 ms
14,400 KB |
testcase_22 | AC | 1,039 ms
14,924 KB |
testcase_23 | AC | 1,043 ms
14,948 KB |
testcase_24 | AC | 1,001 ms
14,932 KB |
testcase_25 | AC | 810 ms
15,688 KB |
testcase_26 | AC | 1,041 ms
14,924 KB |
testcase_27 | AC | 876 ms
14,328 KB |
testcase_28 | AC | 942 ms
14,928 KB |
testcase_29 | AC | 999 ms
14,936 KB |
testcase_30 | AC | 822 ms
14,816 KB |
testcase_31 | AC | 816 ms
15,084 KB |
testcase_32 | AC | 824 ms
15,080 KB |
testcase_33 | AC | 794 ms
16,364 KB |
testcase_34 | AC | 807 ms
14,704 KB |
testcase_35 | AC | 760 ms
15,364 KB |
testcase_36 | AC | 410 ms
13,576 KB |
testcase_37 | AC | 839 ms
15,060 KB |
testcase_38 | AC | 800 ms
15,324 KB |
testcase_39 | AC | 847 ms
14,704 KB |
ソースコード
#include <iostream> #include <string> #include <algorithm> #include <vector> #include <list> #include <set> #include <unordered_set> #include <map> #include <unordered_map> #include <cmath> #include <utility> #include <sstream> #include <queue> #include <queue> using namespace std; vector<int> parent; vector<int> rrank; vector<vector<int>> adj_list; vector<int> distances; using weighted_key = tuple<int, int>; unordered_set<int> visited; priority_queue<weighted_key, vector<weighted_key>, std::greater<>> pqueue; int find_set(int v) { if (v == parent[v]) return v; return parent[v] = find_set(parent[v]); } void make_set(int v) { parent[v] = v; rrank[v] = 0; } void union_sets(int a, int b) { a = find_set(a); b = find_set(b); if (a != b) { if (rrank[a] < rrank[b]) swap(a, b); parent[b] = a; if (rrank[a] == rrank[b]) rrank[a]++; } } bool solve(const vector<vector<int>> &grid, const int k, const int desired_minimum) { int n = grid.size(); int m = grid[0].size(); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { make_set(i * m + j); } } vector<pair<int, int>> dirs = {{{-1, 0}, {1, 0}, {0, 1}, {0, -1}}}; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (grid[i][j] < desired_minimum) { continue; } int key = i * m + j; for (const auto &d: dirs) { int nexti = i + d.first; int nextj = j + d.second; if (nexti < 0 || nextj < 0 || nexti >= n || nextj >= m) { continue; } if (grid[nexti][nextj] < desired_minimum) { continue; } int nextkey = nexti * m + nextj; union_sets(key, nextkey); } } } // cerr << "components: "; // for (int i = 0; i < n; ++i) { // for (int j = 0; j < m; ++j) { // cerr << "(" << i << "," << j << "): " << find_set(i * m + j) << ", "; // } // } // cerr << endl; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { adj_list[i * m + j].clear(); } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { int pcur = find_set(i * m + j); for (const auto &d: dirs) { int nexti = i + d.first; int nextj = j + d.second; if (nexti < 0 || nextj < 0 || nexti >= n || nextj >= m) { continue; } int pnext = find_set(nexti * m + nextj); if (pnext == pcur) { continue; } adj_list[pcur].emplace_back(pnext); adj_list[pnext].emplace_back(pcur); } } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { auto &arr = adj_list[i * m + j]; std::sort(arr.begin(), arr.end()); auto last = std::unique(arr.begin(), arr.end()); arr.erase(last, arr.end()); } } // find dist from (0,0) to (n-1, m-1) visited.clear(); while (!pqueue.empty()) { pqueue.pop(); } int start_weight = 0; if (grid[0][0] < desired_minimum) { start_weight = 1; } std::fill(distances.begin(), distances.end(), -1); distances[find_set(0)] = start_weight; pqueue.emplace(start_weight, find_set(0)); int target = find_set(n * m - 1); while (!pqueue.empty()) { int cur_dist = std::get<0>(pqueue.top()); int cur = std::get<1>(pqueue.top()); pqueue.pop(); if (visited.find(cur) != visited.end()) { continue; } visited.emplace(cur); if (cur == target) { break; } for (const auto &next: adj_list[cur]) { if (visited.find(next) != visited.end()) { continue; } //cerr << "from " << cur << " can reach " << next << endl; int next_r = next / m; int next_c = next % m; int next_cost = 0; if (grid[next_r][next_c] < desired_minimum) { next_cost += 1; } int next_dist = cur_dist + next_cost; if (distances[next] == -1 || distances[next] > next_dist) { distances[next] = next_dist; pqueue.emplace(next_dist, next); } } } int dist_to_goal = distances[target]; // cerr << "num steps to go from (0,0) (comp=" << find_set(0) << ") to (n-1,m-1) (comp=" << target << "): " // << dist_to_goal << endl; bool solvable = dist_to_goal <= k; return solvable; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int64_t n, m, k; cin >> n >> m >> k; vector<vector<int>> grid(n, vector<int>(m)); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> grid[i][j]; } } parent.resize(n * m); rrank.resize(n * m); adj_list.resize(n * m); distances.resize(n * m); int lo_ans = 0; int hi_ans = 1000000001; while (lo_ans + 1 < hi_ans) { int mid_ans = lo_ans / 2 + hi_ans / 2 + ((lo_ans % 2) + (hi_ans % 2)) / 2; bool solvable = solve(grid, k, mid_ans); // cerr << "Is it solvable at ans=" << mid_ans << "? " << (solvable ? " yes " : " no ") << "(lo=" << lo_ans << ", hi=" // << hi_ans << ")" << endl; if (solvable) { // this could be the solution, don't rule it out. lo_ans = mid_ans; } else { // ans must be less than mid_ans hi_ans = mid_ans; } } cout << lo_ans << endl; return 0; }