結果

問題 No.340 雪の足跡
ユーザー ctyl_0ctyl_0
提出日時 2016-02-19 18:20:51
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 365 ms / 1,000 ms
コード長 1,958 bytes
コンパイル時間 900 ms
コンパイル使用メモリ 94,900 KB
実行使用メモリ 69,620 KB
最終ジャッジ日時 2024-09-22 12:20:48
合計ジャッジ時間 5,803 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <sstream>
#include <string>
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define var auto
#define mod 1000000007
#define inf 2147483647
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;

template <typename T>
inline void output(T a, int p) {
    if(p){
        cout << fixed << setprecision(p)  << a << "\n";
    }
    else{
        cout << a << "\n";
    }
}
// end of template

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    // source code
    int W, H, N;
    cin >> W >> H >> N;
    vector<int> R(W * H, 0), U(W * H, 0), D(W * H, mod); // Right, Up の最大距離, dist
    vector<vector<int>> L(W * H); // 辺リスト
    rep(i, N){
        int M;
        cin >> M;
        int pre, now;
        cin >> pre;
        rep(i, M){
            cin >> now;
            if (abs(pre - now) < W) R[min(pre, now)] = max(R[min(pre, now)], abs(pre - now));
            else U[min(pre, now)] = max(U[min(pre, now)], abs(pre / W - now / W));
            pre = now;
        }
    }
    
    rep(y, H) rep(x, W){
        int c = x + y * W;
        if (x) R[c] = max(R[c], R[c - 1] - 1);
        if (y) U[c] = max(U[c], U[c - W] - 1);
        if (R[c]) L[c].pb(c + 1), L[c + 1].pb(c);
        if (U[c]) L[c].pb(c + W), L[c + W].pb(c);
    }
    
    D[0] = 0;
    queue<int> q;
    q.push(0);
    while (!q.empty()) {
        int t = q.front();
        q.pop();
        rep(i, L[t].size()){
            if (D[L[t][i]] > D[t] + 1) {
                q.push(L[t][i]);
                D[L[t][i]] = D[t] + 1;
            }
        }
    }
    
    if (D[W * H - 1] != mod) {
        output(D[W * H - 1], 0);
    }
    else{
        output("Odekakedekinai..", 0);
    }
    return 0;
}
0