結果

問題 No.971 いたずらっ子
ユーザー koi_kotyakoi_kotya
提出日時 2020-01-18 12:16:35
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,498 ms / 2,000 ms
コード長 1,698 bytes
コンパイル時間 1,848 ms
コンパイル使用メモリ 172,932 KB
実行使用メモリ 245,376 KB
最終ジャッジ日時 2024-04-25 02:07:23
合計ジャッジ時間 14,265 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,498 ms
245,376 KB
testcase_01 AC 1,495 ms
245,120 KB
testcase_02 AC 1,073 ms
182,784 KB
testcase_03 AC 1,385 ms
245,248 KB
testcase_04 AC 1,267 ms
231,168 KB
testcase_05 AC 1,424 ms
245,376 KB
testcase_06 AC 1,026 ms
188,416 KB
testcase_07 AC 10 ms
5,376 KB
testcase_08 AC 327 ms
66,688 KB
testcase_09 AC 323 ms
63,872 KB
testcase_10 AC 8 ms
5,504 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 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 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 1 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 0 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> P;

#define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)

const int INF = 1<<30;

template <typename T>
void dijkstra(int s,vector<vector<pair<int,T>>>& edges,vector<T>& dist) {
    priority_queue<pair<T,int>,vector<pair<T,int>>,greater<pair<T,int>>> que;
    dist[s] = 0;
    que.push(make_pair(0,s));
    while (!que.empty()) {
        pair<ll,int> p = que.top();
        que.pop();
        int v =  p.second;
        if (dist[v] < p.first) continue;
        for (pair<int,T>& e : edges[v]) {
            if (dist[e.first] > dist[v]+e.second) {
                dist[e.first] = dist[v]+e.second;
                que.push(make_pair(dist[e.first],e.first));
            }
        }
    }
}

int main() {
    int h,w;
    cin >> h >> w;
    string a[2000];
    for (int i = 0;i < h;++i) cin >> a[i];
    vector<int> dist(h*w,INF);
    vector<vector<P>> edges(h*w);
    for (int i = 0;i < h;++i) for (int j = 1;j < w;++j) {
        if (a[i][j] == 'k') edges[i*w+j-1].push_back(P(i*w+j,i+j+1));
        else edges[i*w+j-1].push_back(P(i*w+j,1));
    }
    for (int i = 1;i < h;++i) for (int j = 0;j < w;++j) {
        if (a[i][j] == 'k') edges[(i-1)*w+j].push_back(P(i*w+j,i+j+1));
        else edges[(i-1)*w+j].push_back(P(i*w+j,1));
    }
    dijkstra<int>(0,edges,dist);
    cout << dist[h*w-1] << endl;
}
0