結果

問題 No.340 雪の足跡
ユーザー kimiyukikimiyuki
提出日時 2016-05-27 17:43:56
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 354 ms / 1,000 ms
コード長 2,966 bytes
コンパイル時間 682 ms
コンパイル使用メモリ 68,776 KB
実行使用メモリ 14,968 KB
最終ジャッジ日時 2023-08-01 08:35:08
合計ジャッジ時間 7,652 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 18 ms
4,380 KB
testcase_15 AC 21 ms
4,380 KB
testcase_16 AC 14 ms
4,380 KB
testcase_17 AC 26 ms
4,376 KB
testcase_18 AC 22 ms
4,380 KB
testcase_19 AC 26 ms
4,380 KB
testcase_20 AC 181 ms
14,952 KB
testcase_21 AC 117 ms
4,376 KB
testcase_22 AC 9 ms
14,888 KB
testcase_23 AC 247 ms
14,900 KB
testcase_24 AC 148 ms
14,904 KB
testcase_25 AC 160 ms
14,844 KB
testcase_26 AC 31 ms
4,380 KB
testcase_27 AC 7 ms
4,376 KB
testcase_28 AC 28 ms
4,376 KB
testcase_29 AC 253 ms
10,020 KB
testcase_30 AC 179 ms
8,312 KB
testcase_31 AC 309 ms
13,976 KB
testcase_32 AC 354 ms
14,964 KB
testcase_33 AC 296 ms
14,912 KB
testcase_34 AC 351 ms
14,960 KB
testcase_35 AC 266 ms
14,944 KB
testcase_36 AC 268 ms
14,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <vector>
#include <set>
#include <queue>
#include <functional>
#include <cstdint>
#include <cassert>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
#define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i))
using namespace std;

template <typename T>
struct binary_indexed_tree { // on monoid
    vector<T> a;
    T unit;
    function<T (T,T)> append; // associative
    template <typename F>
    binary_indexed_tree(size_t n, T a_unit, F a_append) : a(n, a_unit), unit(a_unit), append(a_append) {}
    void point_append(size_t i, T w) { // a[i] += w
        for (size_t j = i+1; j <= a.size(); j += j & -j) a[j-1] = append(a[j-1], w);
    }
    int initial_range_concat(size_t i) { // sum [0, i)
        T acc = unit;
        for (size_t j = i; 0 < j; j -= j & -j) acc = append(acc, a[j-1]);
        return acc;
    }
};

const int dy[4] = { -1, 1, 0, 0 };
const int dx[4] = { 0, 0, 1, -1 };
struct state_t { int dist; int y, x; };
int main() {
    int w, h, n; scanf("%d%d%d", &w, &h, &n);
    vector<binary_indexed_tree<int> > row(h, binary_indexed_tree<int>(w, 0, plus<int>()));
    vector<binary_indexed_tree<int> > col(w, binary_indexed_tree<int>(h, 0, plus<int>()));
    repeat (i,n) {
        int m; scanf("%d", &m);
        vector<int> b(m+1); repeat (i,m+1) scanf("%d", &b[i]);
        repeat (i,m) {
            int y0 = b[i]   / w;
            int y1 = b[i+1] / w;
            int x0 = b[i]   % w;
            int x1 = b[i+1] % w;
            if (y0 == y1) {
                int y = y0;
                int xl = min(x0,x1);
                int xr = max(x0,x1);
                row[y].point_append(xl, +1);
                row[y].point_append(xr, -1);
            } else if (x0 == x1) {
                int x = x0;
                int yl = min(y0,y1);
                int yr = max(y0,y1);
                col[x].point_append(yl, +1);
                col[x].point_append(yr, -1);
            } else {
                assert (false);
            }
        }
    }
    vector<vector<int> > dist(h, vector<int>(w));
    queue<state_t> que;
    dist[0][0] = 1;
    que.push((state_t) { 1, 0, 0 });
    while (not que.empty()) {
        state_t s = que.front(); que.pop();
        repeat (i,4) {
            state_t t;
            t.dist = s.dist + 1;
            t.y = s.y + dy[i];
            t.x = s.x + dx[i];
            if (t.y < 0 or h <= t.y or t.x < 0 or w <= t.x) continue;
            if (i == 0 or i == 1) { // up, down
                if (not col[t.x].initial_range_concat(max(s.y, t.y))) continue;
            } else if (i == 2 or i == 3) { // right, left
                if (not row[t.y].initial_range_concat(max(s.x, t.x))) continue;
            }
            if (dist[t.y][t.x]) continue;
            dist[t.y][t.x] = t.dist;
            que.push(t);
        }
    }
    if (dist[h-1][w-1]) {
        printf("%d\n", dist[h-1][w-1] - 1);
    } else {
        puts("Odekakedekinai..");
    }
    return 0;
}
0