結果
| 問題 |
No.2855 Move on Grid
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-10-01 14:39:19 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 798 ms / 3,000 ms |
| コード長 | 2,130 bytes |
| コンパイル時間 | 1,281 ms |
| コンパイル使用メモリ | 119,504 KB |
| 実行使用メモリ | 15,184 KB |
| 最終ジャッジ日時 | 2024-10-01 14:39:46 |
| 合計ジャッジ時間 | 22,447 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 40 |
ソースコード
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <climits>
using namespace std;
class Di {
public:
vector<long long> distance;
vector<vector<pair<int, long long>>> G;
long long inf = LLONG_MAX;
Di(int n) {
distance.assign(n + 1, inf);
G.resize(n + 1);
}
void connect(int from, int to, long long cost) {
G[from].emplace_back(to, cost);
}
void solve(int from) {
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
distance[from] = 0;
pq.emplace(0, from);
while (!pq.empty()) {
auto [dist, f] = pq.top();
pq.pop();
if (dist > distance[f]) continue;
for (auto &[t, cost] : G[f]) {
long long new_dist = distance[f] + cost;
if (distance[t] > new_dist) {
distance[t] = new_dist;
pq.emplace(new_dist, t);
}
}
}
}
};
int main() {
int H, W, K;
cin >> H >> W >> K;
vector<vector<long long>> map(H, vector<long long>(W));
for (int y = 0; y < H; ++y) {
for (int x = 0; x < W; ++x) {
cin >> map[y][x];
}
}
long long ng = 1LL << 30;
long long ok = 0;
while (ng - ok > 1) {
long long mid = (ng + ok) / 2;
Di di(H * W);
for (int y = 0; y < H; ++y) {
for (int x = 0; x < W; ++x) {
int current = y * W + x + 1;
if (x > 0) di.connect(current - 1, current, map[y][x] < mid);
if (y > 0) di.connect((y - 1) * W + x + 1, current, map[y][x] < mid);
if (x < W - 1) di.connect(current + 1, current, map[y][x] < mid);
if (y < H - 1) di.connect((y + 1) * W + x + 1, current, map[y][x] < mid);
}
}
di.solve(1);
if (di.distance[H * W] + (map[0][0] < mid) <= K) {
ok = mid;
} else {
ng = mid;
}
}
cout << min(ok, 1000000000LL) << endl;
return 0;
}