結果

問題 No.340 雪の足跡
ユーザー rickythetarickytheta
提出日時 2016-01-29 23:43:43
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 527 ms / 1,000 ms
コード長 3,521 bytes
コンパイル時間 1,564 ms
コンパイル使用メモリ 167,728 KB
実行使用メモリ 23,288 KB
最終ジャッジ日時 2023-10-21 17:34:05
合計ジャッジ時間 8,671 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,668 KB
testcase_01 AC 2 ms
5,668 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
5,668 KB
testcase_04 AC 2 ms
5,672 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
5,668 KB
testcase_07 AC 2 ms
5,672 KB
testcase_08 AC 2 ms
5,672 KB
testcase_09 AC 2 ms
5,668 KB
testcase_10 AC 2 ms
5,672 KB
testcase_11 AC 4 ms
5,684 KB
testcase_12 AC 3 ms
5,708 KB
testcase_13 AC 4 ms
5,728 KB
testcase_14 AC 24 ms
8,396 KB
testcase_15 AC 29 ms
8,396 KB
testcase_16 AC 21 ms
6,224 KB
testcase_17 AC 38 ms
8,400 KB
testcase_18 AC 31 ms
8,400 KB
testcase_19 AC 38 ms
8,400 KB
testcase_20 AC 311 ms
23,276 KB
testcase_21 AC 199 ms
5,684 KB
testcase_22 AC 7 ms
20,796 KB
testcase_23 AC 359 ms
23,272 KB
testcase_24 AC 296 ms
23,272 KB
testcase_25 AC 330 ms
23,280 KB
testcase_26 AC 57 ms
5,844 KB
testcase_27 AC 12 ms
5,672 KB
testcase_28 AC 51 ms
8,392 KB
testcase_29 AC 431 ms
18,704 KB
testcase_30 AC 299 ms
16,636 KB
testcase_31 AC 448 ms
22,932 KB
testcase_32 AC 527 ms
23,288 KB
testcase_33 AC 413 ms
23,288 KB
testcase_34 AC 524 ms
23,288 KB
testcase_35 AC 391 ms
23,288 KB
testcase_36 AC 382 ms
23,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
typedef pair<int,int> pii;
#define REP(i,n) for(ll i=0;i<n;++i)
#define REPR(i,n) for(ll i=1;i<n;++i)
#define FOR(i,a,b) for(ll i=a;i<b;++i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define MOD (ll)(1e9+7)
#define ADD(a,b) a=((a)+(b))%MOD
#define FIX(a) ((a)%MOD+MOD)%MOD

#define V_MAX 1000010
#define INF 100000000

int g[V_MAX];
int lt[V_MAX][4];
// right, up, left, down
int dir[4] = {1,2,4,8};
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};

int w,h;

int gp(int x,int y){
  return x+y*w;
}

void add_edge(int src,int dst){
  if(src>dst)swap(src,dst);
  int sx,sy,tx,ty;
  sx = src%w;
  sy = src/w;
  tx = dst%w;
  ty = dst/w;
  if(sx==tx){
    FOR(i,sy,ty){
      if(lt[gp(sx,i)][1]!=gp(sx,i)){
        i = lt[gp(sx,i)][1]/w;
        if(i>=ty)break;
      }
      g[sx+i*w] |= dir[1];
      g[sx+(i+1)*w] |= dir[3];
      lt[sx+i*w][1] = max(lt[sx+i*w][1],dst);
    }
  }else{
    FOR(i,sx,tx){
      if(lt[gp(i,sy)][0]!=gp(i,sy)){
        i = lt[gp(i,sy)][0]%w;
        if(i>=tx)break;
      }
      g[sy*w+i] |= dir[0];
      g[sy*w+i+1] |= dir[2];
      lt[sy*w+i][0] = max(lt[sy*w+i][0],dst);
    }
  }
}

int main(){
  // w,h<1e3
  // |V| = wh < 1e6
  // n < 1e3
  // m < 1e3
  // |E| = nm < 1e6
  int n;
  cin>>w>>h>>n;
  if(w==1 && h==1){
    cout<<0<<endl;
    return 0;
  }
  REP(i,w*h)REP(j,4)lt[i][j]=i;
  while(n--){
    int m;
    cin>>m;
    int bef;
    cin>>bef;
    REP(i,m){
      int cur;
      cin>>cur;
      add_edge(bef,cur);
      bef = cur;
    }
  }
  queue<pii> Q;
  int s = 0;
  int t = w*h-1;
  Q.push(make_pair(0,0));
  int res = -1;
  vector<bool> used(w*h,false);
  while(!Q.empty()){
    pii P = Q.front(); Q.pop();
    // int pos = P.second;
    int pos = P.first;
    if(used[pos])continue;
    used[pos] = true;
    // int wei = INF-P.first;
    int wei = P.second;
    int x = pos%w;
    int y = pos/w;
    // FOR(i,0,2){
    //   int dst = g[pos][i];
    //   int tx = x+dx[i];
    //   int ty = y+dy[i];
    //   int tpos = gp(tx,ty);
    //   int cnt = 1;
    //   while(0<=tx && tx<w && 0<=ty && ty<h && tpos <= dst && !used[tpos]){
    //     if(tpos==t){
    //       cout<<(wei+1)<<endl;
    //       return 0;
    //     }
    //     Q.push(make_pair(INF-wei-cnt,tpos));
    //     tx += dx[i];
    //     ty += dy[i];
    //     tpos = gp(tx,ty);
    //     ++cnt;
    //   }
    // }
    // FOR(i,2,4){
    //   int dst = g[pos][i];
    //   int tx = x+dx[i];
    //   int ty = y+dy[i];
    //   int tpos = gp(tx,ty);
    //   int cnt = 1;
    //   while(0<=tx && tx<w && 0<=ty && ty<h && tpos >= dst && !used[tpos]){
    //     if(tpos==t){
    //       cout<<(wei+1)<<endl;
    //       return 0;
    //     }
    //     Q.push(make_pair(INF-wei-cnt,tpos));
    //     tx += dx[i];
    //     ty += dy[i];
    //     tpos = gp(tx,ty);
    //     ++cnt;
    //   }
    // }
    REP(i,4){
      if(!(g[pos] & dir[i]))continue;
      int tx = x+dx[i];
      int ty = y+dy[i];
      if(tx<0 || tx>=w)continue;
      if(ty<0 || ty>=h)continue;
      int tpos = ty*w+tx;
      if(tpos<0 || tpos>=w*h)continue;
      if(used[tpos])continue;
      if(tpos == t){
        cout<<(wei+1)<<endl;
        return 0;
      }
      Q.push(make_pair(tpos,wei+1));
    }
  }
  cout<<"Odekakedekinai.."<<endl;
  return 0;
}
0