結果

問題 No.1283 Extra Fee
ユーザー LayCurseLayCurse
提出日時 2020-11-06 21:32:16
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 173 ms / 2,000 ms
コード長 10,062 bytes
コンパイル時間 2,711 ms
コンパイル使用メモリ 213,952 KB
実行使用メモリ 92,104 KB
最終ジャッジ日時 2023-08-10 05:50:06
合計ジャッジ時間 6,512 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
15,724 KB
testcase_01 AC 4 ms
15,852 KB
testcase_02 AC 5 ms
15,896 KB
testcase_03 AC 4 ms
15,600 KB
testcase_04 AC 4 ms
15,780 KB
testcase_05 AC 4 ms
15,696 KB
testcase_06 AC 4 ms
15,900 KB
testcase_07 AC 4 ms
15,672 KB
testcase_08 AC 4 ms
15,608 KB
testcase_09 AC 4 ms
15,608 KB
testcase_10 AC 4 ms
15,668 KB
testcase_11 AC 11 ms
22,072 KB
testcase_12 AC 13 ms
22,276 KB
testcase_13 AC 9 ms
19,940 KB
testcase_14 AC 28 ms
30,744 KB
testcase_15 AC 44 ms
39,724 KB
testcase_16 AC 12 ms
22,060 KB
testcase_17 AC 151 ms
87,612 KB
testcase_18 AC 157 ms
87,764 KB
testcase_19 AC 168 ms
89,928 KB
testcase_20 AC 155 ms
87,816 KB
testcase_21 AC 161 ms
87,820 KB
testcase_22 AC 137 ms
83,680 KB
testcase_23 AC 132 ms
92,004 KB
testcase_24 AC 135 ms
92,004 KB
testcase_25 AC 171 ms
91,864 KB
testcase_26 AC 167 ms
92,104 KB
testcase_27 AC 170 ms
91,912 KB
testcase_28 AC 165 ms
91,940 KB
testcase_29 AC 173 ms
91,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void*wmem;
char memarr[96000000];
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
template<class T> inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){
  walloc1d(arr, x2-x1, mem);
  (*arr) -= x1;
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline int rd_int(void){
  int x;
  rd(x);
  return x;
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
template<class S> inline void arrInsert(const int k, int &sz, S a[], const S aval){
  int i;
  sz++;
  for(i=sz-1;i>k;i--){
    a[i] = a[i-1];
  }
  a[k] = aval;
}
template<class S, class T> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval){
  int i;
  sz++;
  for(i=sz-1;i>k;i--){
    a[i] = a[i-1];
  }
  for(i=sz-1;i>k;i--){
    b[i] = b[i-1];
  }
  a[k] = aval;
  b[k] = bval;
}
template<class S, class T, class U> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval){
  int i;
  sz++;
  for(i=sz-1;i>k;i--){
    a[i] = a[i-1];
  }
  for(i=sz-1;i>k;i--){
    b[i] = b[i-1];
  }
  for(i=sz-1;i>k;i--){
    c[i] = c[i-1];
  }
  a[k] = aval;
  b[k] = bval;
  c[k] = cval;
}
template<class S, class T, class U, class V> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval, V d[], const V dval){
  int i;
  sz++;
  for(i=sz-1;i>k;i--){
    a[i] = a[i-1];
  }
  for(i=sz-1;i>k;i--){
    b[i] = b[i-1];
  }
  for(i=sz-1;i>k;i--){
    c[i] = c[i-1];
  }
  for(i=sz-1;i>k;i--){
    d[i] = d[i-1];
  }
  a[k] = aval;
  b[k] = bval;
  c[k] = cval;
  d[k] = dval;
}
template <class T> struct DijkstraHeap{
  int*hp;
  int*place;
  int size;
  char*visited;
  T*val;
  void malloc(int N){
    hp = (int*)std::malloc(N*sizeof(int));
    place = (int*)std::malloc(N*sizeof(int));
    visited = (char*)std::malloc(N*sizeof(char));
    val = (T*)std::malloc(N*sizeof(T));
  }
  void free(){
    std::free(hp);
    std::free(place);
    std::free(visited);
    std::free(val);
  }
  void walloc(int N, void **mem=&wmem){
    walloc1d(&hp, N, mem);
    walloc1d(&place, N, mem);
    walloc1d(&visited, N, mem);
    walloc1d(&val, N, mem);
  }
  void init(int N){
    int i;
    size = 0;
    for(i=(0);i<(N);i++){
      place[i]=-1;
    }
    for(i=(0);i<(N);i++){
      visited[i]=0;
    }
  }
  void up(int n){
    int m;
    while(n){
      m=(n-1)/2;
      if(val[hp[m]]<=val[hp[n]]){
        break;
      }
      swap(hp[m],hp[n]);
      swap(place[hp[m]],place[hp[n]]);
      n=m;
    }
  }
  void down(int n){
    int m;
    for(;;){
      m=2*n+1;
      if(m>=size){
        break;
      }
      if(m+1<size&&val[hp[m]]>val[hp[m+1]]){
        m++;
      }
      if(val[hp[m]]>=val[hp[n]]){
        break;
      }
      swap(hp[m],hp[n]);
      swap(place[hp[m]],place[hp[n]]);
      n=m;
    }
  }
  void change(int n, T v){
    if(visited[n]||(place[n]>=0&&val[n]<=v)){
      return;
    }
    val[n]=v;
    if(place[n]==-1){
      place[n]=size;
      hp[size++]=n;
      up(place[n]);
    }
    else{
      up(place[n]);
    }
  }
  int pop(void){
    int res=hp[0];
    place[res]=-1;
    size--;
    if(size){
      hp[0]=hp[size];
      place[hp[0]]=0;
      down(0);
    }
    visited[res]=1;
    return res;
  }
}
;
struct graph{
  int N;
  int*es;
  int**edge;
  void setDirectEdge(int N__, int M, int A[], int B[], void **mem = &wmem){
    int i;
    N = N__;
    walloc1d(&es, N, mem);
    walloc1d(&edge, N, mem);
    walloc1d(&edge[0], M, mem);
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      es[A[i]]++;
    }
    for(i=(0);i<(N);i++){
      walloc1d(&edge[i], es[i], mem);
    }
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      edge[A[i]][es[A[i]]++] = B[i];
    }
  }
  void getDist(int root, int res[], void *mem = wmem){
    int i;
    int j;
    int k;
    int*q;
    int s;
    int z;
    walloc1d(&q, N, &mem);
    for(i=(0);i<(N);i++){
      res[i]=-1;
    }
    res[root]=0;
    s=0;
    z=1;
    q[0]=root;
    while(z){
      i=q[s++];
      z--;
      for(j=(0);j<(es[i]);j++){
        k=edge[i][j];
        if(res[k]>=0){
          continue;
        }
        res[k]=res[i]+1;
        q[s+z++]=k;
      }
    }
  }
  int getDist(int a, int b, void *mem = wmem){
    int i;
    int j;
    int k;
    int*q;
    int s;
    int z;
    int*d;
    if(a==b){
      return 0;
    }
    walloc1d(&d, N, &mem);
    walloc1d(&q, N, &mem);
    for(i=(0);i<(N);i++){
      d[i] = -1;
    }
    d[a] = 0;
    s = 0;
    z = 1;
    q[0] = a;
    while(z){
      i = q[s++];
      z--;
      for(j=(0);j<(es[i]);j++){
        k = edge[i][j];
        if(d[k] >= 0){
          continue;
        }
        d[k] = d[i] + 1;
        if(k==b){
          return d[k];
        }
        q[s+z++] = k;
      }
    }
    return -1;
  }
}
;
template<class T> struct wgraph{
  int N;
  int*es;
  int**edge;
  T**cost;
  graph g;
  void setDirectEdge(int N__, int M, int A[], int B[], T C[], void **mem = &wmem){
    int i;
    N = N__;
    walloc1d(&es, N, mem);
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      es[A[i]]++;
    }
    walloc1d(&edge, N, mem);
    for(i=(0);i<(N);i++){
      walloc1d(&edge[i], es[i], mem);
    }
    walloc1d(&cost, N, mem);
    for(i=(0);i<(N);i++){
      walloc1d(&cost[i], es[i], mem);
    }
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      edge[A[i]][es[A[i]]] = B[i];
      cost[A[i]][es[A[i]]++] = C[i];
    }
    g.N = N;
    g.es = es;
    g.edge = edge;
  }
  template<class S> void getDist(int root, S res[], S unreachable = -1, void *mem = wmem){
    int i;
    int j;
    DijkstraHeap<S> hp;
    hp.walloc(N, &mem);
    hp.init(N);
    hp.change(root,0);
    while(hp.size){
      i = hp.pop();
      for(j=(0);j<(es[i]);j++){
        hp.change(edge[i][j], hp.val[i]+cost[i][j]);
      }
    }
    for(i=(0);i<(N);i++){
      res[i] = (hp.visited[i] ? hp.val[i] : unreachable);
    }
  }
}
;
struct dimcomp3{
  int B;
  int C;
  dimcomp3(){
  }
  ;
  dimcomp3(int b, int c){
    B = b;
    C = c;
  }
  dimcomp3(int a, int b, int c){
    B = b;
    C = c;
  }
  inline void set(int b, int c){
    B = b;
    C = c;
  }
  inline void set(int a, int b, int c){
    B = b;
    C = c;
  }
  inline int mask(int a, int b, int c){
    return (a * B + b) * C + c;
  }
  inline int operator()(int a, int b, int c){
    return (a * B + b) * C + c;
  }
  inline void para(int mask, int &a, int &b, int &c){
    a = mask / (B*C);
    b = mask % (B*C) / C;
    c = mask % C;
  }
  inline void operator()(int mask, int &a, int &b, int &c){
    a = mask / (B*C);
    b = mask % (B*C) / C;
    c = mask % C;
  }
}
;
int N;
int cost[500][500];
int n;
int m;
int a[3000000];
int b[3000000];
int c[3000000];
long long dist[1000000];
wgraph<int> g;
int main(){
  int Lj4PdHRW;
  wmem = memarr;
  int i;
  int j;
  int k;
  int ni;
  int nj;
  int d;
  int dx[4] = {-1, 1, 0, 0};
  int dy[4] = {0, 0, -1, 1};
  long long res;
  rd(N);
  dimcomp3 dm(2,N,N);
  int KL2GvlyY = rd_int();
  for(Lj4PdHRW=(0);Lj4PdHRW<(KL2GvlyY);Lj4PdHRW++){
    rd(i);i += (-1);
    rd(j);j += (-1);
    rd(k);
    cost[i][j] = k;
  }
  n = 2*N*N;
  for(i=(0);i<(N);i++){
    for(j=(0);j<(N);j++){
      for(d=(0);d<(4);d++){
        ni = i + dx[d];
        nj = j + dy[d];
        if(ni < 0 || nj < 0 || ni >= N || nj >= N){
          continue;
        }
        for(k=(0);k<(2);k++){
          arrInsert(m, m, a, dm(k,i,j), b, dm(k,ni,nj), c, 1+cost[ni][nj]);
        }
        arrInsert(m, m, a, dm(0,i,j), b, dm(1,ni,nj), c, 1);
      }
    }
  }
  g.setDirectEdge(n, m, a, b, c);
  g.getDist(0, dist);
  res = dist[dm(1,N-1,N-1)];
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay varsion 20201102-1

// --- original code ---
// int N, cost[500][500];
// int n, m, a[3d6], b[3d6], c[3d6];
// ll dist[1d6];
// wgraph<int> g;
// {
//   int i, j, k, ni, nj, d;
//   int dx[4] = {-1, 1, 0, 0};
//   int dy[4] = {0, 0, -1, 1};
//   ll res;
//   rd(N);
//   dimcomp3 dm(2,N,N);
//   REP(rd_int()){
//     rd(i--,j--,k);
//     cost[i][j] = k;
//   }
//   n = 2*N*N;
//   rep(i,N) rep(j,N) rep(d,4){
//     ni = i + dx[d];
//     nj = j + dy[d];
//     if(ni < 0 || nj < 0 || ni >= N || nj >= N) continue;
//     rep(k,2) arrInsert(m, m, a, dm(k,i,j), b, dm(k,ni,nj), c, 1+cost[ni][nj]);
//     arrInsert(m, m, a, dm(0,i,j), b, dm(1,ni,nj), c, 1);
//   }
//   g.setDirectEdge(n, m, a, b, c);
//   g.getDist(0, dist);
//   res = dist[dm(1,N-1,N-1)];
//   wt(res);
// }
0