結果
問題 | No.971 いたずらっ子 |
ユーザー | milanis48663220 |
提出日時 | 2020-06-04 01:30:44 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 763 ms / 2,000 ms |
コード長 | 1,496 bytes |
コンパイル時間 | 1,102 ms |
コンパイル使用メモリ | 89,920 KB |
実行使用メモリ | 38,912 KB |
最終ジャッジ日時 | 2024-11-07 11:45:49 |
合計ジャッジ時間 | 7,758 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 21 |
ソースコード
#include <iostream> #include <algorithm> #include <iomanip> #include <vector> #include <queue> #include <set> #include <map> using namespace std; typedef long long ll; typedef pair<int, int> Pii; typedef pair<ll, Pii> P; int H, W; char a[2000][2000]; ll dist[2000][2000]; const ll INF = 1e15; int dh[2] = {1, 0}; int dw[2] = {0, 1}; bool in_field(int h, int w){ return h >= 0 && h < H && w >= 0 && w < W; } void dijkstra(){ priority_queue<P, vector<P>, greater<P>> que; for(int i = 0; i < H; i++){ for(int j = 0; j < W; j++){ dist[i][j] = INF; } } dist[0][0] = 0; que.push(P(0, Pii(0, 0))); while(!que.empty()){ P p = que.top();que.pop(); Pii v = p.second; int h = v.first, w = v.second; if(dist[h][w] < p.first) continue; for(int i = 0; i < 2; i++){ int h_to = h+dh[i], w_to = w+dw[i]; if(!in_field(h_to, w_to)) continue; ll cost = (a[h_to][w_to] == '.' ? 1 : (2+h+w)); if(dist[h][w] + cost < dist[h_to][w_to]){ dist[h_to][w_to] = dist[h][w] + cost; que.push(P(dist[h_to][w_to], Pii(h_to, w_to))); } } } } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; cin >> H >> W; for(int i = 0; i < H; i++){ for(int j = 0; j < W; j++){ cin >> a[i][j]; } } dijkstra(); cout << dist[H-1][W-1] << endl; }