結果

問題 No.971 いたずらっ子
ユーザー roarisroaris
提出日時 2020-01-17 21:58:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 868 ms / 2,000 ms
コード長 1,280 bytes
コンパイル時間 1,695 ms
コンパイル使用メモリ 168,960 KB
実行使用メモリ 40,448 KB
最終ジャッジ日時 2024-04-25 02:01:06
合計ジャッジ時間 8,952 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 837 ms
40,448 KB
testcase_01 AC 868 ms
40,448 KB
testcase_02 AC 627 ms
39,040 KB
testcase_03 AC 715 ms
40,448 KB
testcase_04 AC 643 ms
38,528 KB
testcase_05 AC 694 ms
40,448 KB
testcase_06 AC 542 ms
36,096 KB
testcase_07 AC 8 ms
5,376 KB
testcase_08 AC 177 ms
17,920 KB
testcase_09 AC 166 ms
17,408 KB
testcase_10 AC 5 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 11 ms
15,616 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 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