結果
問題 |
No.2855 Move on Grid
|
ユーザー |
|
提出日時 | 2023-10-27 01:28:35 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 927 ms / 3,000 ms |
コード長 | 1,922 bytes |
コンパイル時間 | 2,097 ms |
コンパイル使用メモリ | 208,700 KB |
最終ジャッジ日時 | 2025-02-17 14:04:16 |
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 40 |
ソースコード
#include <bits/stdc++.h> using namespace std; template <typename T> vector<long long> dijkstra(const vector<vector<array<long long, 2>>> &G, T x){ const long long INF = 0x1fffffffffffffff; vector<long long> cost((int) G.size(), INF); using P = pair<long long, long long>; priority_queue<P, vector<P>, greater<>> q; cost[x] = 0; q.emplace(0, x); while(q.size()){ auto [c, at] = q.top(); q.pop(); if(c > cost[at]) continue; for(auto& [to, t] : G[at]){ if(cost[to] > c + t){ cost[to] = c + t; q.emplace(cost[to], to); } } } return cost; } vector<int> dx = {0, 1, 0, -1}; vector<int> dy = {1, 0, -1, 0}; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n, m, k; cin >> n >> m >> k; vector<vector<int>> a(n, vector<int>(m)); for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ cin >> a[i][j]; } } int ok = 1, ng = 1000000000 + 1; while(abs(ok - ng) > 1){ int mid = (ok + ng) / 2; vector<vector<array<long long, 2>>> G(n * m + 1); for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ for(int dir = 0; dir < 4; dir++){ int nx = i + dx[dir], ny = j + dy[dir]; if(0 <= nx && nx < n && 0 <= ny && ny < m){ if(a[nx][ny] < mid){ G[j * n + i].push_back({ny * n + nx, 1}); }else{ G[j * n + i].push_back({ny * n + nx, 0}); } } } } } vector<long long> cost = dijkstra(G, 0); if(cost[n * m - 1] + (a[0][0] < mid) <= k){ ok = mid; }else{ ng = mid; } } cout << ok << endl; }