結果

問題 No.971 いたずらっ子
ユーザー roarisroaris
提出日時 2020-01-17 21:58:40
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 806 ms / 2,000 ms
コード長 1,280 bytes
コンパイル時間 1,498 ms
コンパイル使用メモリ 170,492 KB
実行使用メモリ 42,200 KB
最終ジャッジ日時 2023-08-07 07:54:46
合計ジャッジ時間 8,734 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 806 ms
42,200 KB
testcase_01 AC 799 ms
42,104 KB
testcase_02 AC 597 ms
41,884 KB
testcase_03 AC 646 ms
41,944 KB
testcase_04 AC 605 ms
41,720 KB
testcase_05 AC 643 ms
41,936 KB
testcase_06 AC 494 ms
39,456 KB
testcase_07 AC 7 ms
6,092 KB
testcase_08 AC 162 ms
25,340 KB
testcase_09 AC 155 ms
25,368 KB
testcase_10 AC 5 ms
5,680 KB
testcase_11 AC 2 ms
5,440 KB
testcase_12 AC 2 ms
5,404 KB
testcase_13 AC 2 ms
5,456 KB
testcase_14 AC 10 ms
40,396 KB
testcase_15 AC 2 ms
5,408 KB
testcase_16 AC 4 ms
12,072 KB
testcase_17 AC 2 ms
5,404 KB
testcase_18 AC 1 ms
5,484 KB
testcase_19 AC 2 ms
5,452 KB
testcase_20 AC 2 ms
5,396 KB
testcase_21 AC 2 ms
5,472 KB
testcase_22 AC 2 ms
5,764 KB
testcase_23 AC 2 ms
5,444 KB
testcase_24 AC 2 ms
5,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
#define int long long
#define INF 10000000000000

int H, W;
char a[2100][2100];
int dist[2100][2100];

void dijkstra() {
    for (int i=0; i<H; i++) for (int j=0; j<W; j++) dist[i][j] = INF;
    dist[0][0] = 0;
    priority_queue<P, vector<P>, greater<P>> pq;
    pq.push(P(0, 0));
    
    while (pq.size()) {
        P p = pq.top(); pq.pop();
        int d = p.first;
        int v = p.second;
        int x = v/W, y = v%W;
        
        if (dist[x][y]<d) continue;
        if (x<H-1) {
            int w;
            if (a[x+1][y]=='k') w = x+y+2;
            else w = 1;
            
            if (dist[x+1][y]>dist[x][y]+w) {
                dist[x+1][y] = dist[x][y]+w;
                pq.push(P(dist[x+1][y], W*(x+1)+y));
            }
        }
        if (y<W-1) {
            int w;
            if (a[x][y+1]=='k') w = x+y+2;
            else w = 1;
            
            if (dist[x][y+1]>dist[x][y]+w) {
                dist[x][y+1] = dist[x][y]+w;
                pq.push(P(dist[x][y+1], W*x+y+1));
            }
        }
    }
}

signed main() {
    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;
}
0