結果

問題 No.1607 Kth Maximum Card
ユーザー LayCurseLayCurse
提出日時 2021-07-16 21:33:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 809 ms / 3,500 ms
コード長 6,271 bytes
コンパイル時間 2,561 ms
コンパイル使用メモリ 219,660 KB
実行使用メモリ 24,476 KB
最終ジャッジ日時 2023-09-20 13:12:00
合計ジャッジ時間 10,824 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
9,480 KB
testcase_01 AC 2 ms
9,496 KB
testcase_02 AC 3 ms
9,524 KB
testcase_03 AC 3 ms
9,488 KB
testcase_04 AC 2 ms
9,484 KB
testcase_05 AC 3 ms
9,580 KB
testcase_06 AC 3 ms
9,640 KB
testcase_07 AC 3 ms
9,588 KB
testcase_08 AC 587 ms
24,476 KB
testcase_09 AC 348 ms
22,100 KB
testcase_10 AC 701 ms
24,388 KB
testcase_11 AC 46 ms
12,584 KB
testcase_12 AC 340 ms
19,972 KB
testcase_13 AC 40 ms
10,292 KB
testcase_14 AC 50 ms
12,492 KB
testcase_15 AC 280 ms
20,180 KB
testcase_16 AC 38 ms
10,336 KB
testcase_17 AC 13 ms
9,724 KB
testcase_18 AC 142 ms
15,756 KB
testcase_19 AC 83 ms
13,212 KB
testcase_20 AC 128 ms
15,528 KB
testcase_21 AC 134 ms
15,604 KB
testcase_22 AC 809 ms
24,460 KB
testcase_23 AC 642 ms
24,440 KB
testcase_24 AC 114 ms
12,996 KB
testcase_25 AC 67 ms
12,360 KB
testcase_26 AC 86 ms
12,648 KB
testcase_27 AC 123 ms
14,988 KB
testcase_28 AC 98 ms
12,756 KB
testcase_29 AC 211 ms
17,652 KB
testcase_30 AC 778 ms
22,392 KB
testcase_31 AC 189 ms
17,600 KB
testcase_32 AC 66 ms
14,192 KB
testcase_33 AC 64 ms
14,208 KB
testcase_34 AC 70 ms
14,180 KB
testcase_35 AC 72 ms
14,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("inline")
#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;
  }
}
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(int x){
  int s=0;
  int m=0;
  char f[10];
  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');
  }
}
struct graph{
  int N;
  int*es;
  int**edge;
  void setEdge(int N__, int M, int A[], int B[], void **mem = &wmem){
    int i;
    N = N__;
    walloc1d(&es, N, mem);
    walloc1d(&edge, N, mem);
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      es[A[i]]++;
      es[B[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];
      edge[B[i]][es[B[i]]++] = A[i];
    }
  }
}
;
template<class T> struct wgraph{
  int N;
  int*es;
  int**edge;
  T**cost;
  graph g;
  void setEdge(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]]++;
      es[B[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];
      edge[B[i]][es[B[i]]] = A[i];
      cost[A[i]][es[A[i]]++] = C[i];
      cost[B[i]][es[B[i]]++] = C[i];
    }
    g.N = N;
    g.es = es;
    g.edge = edge;
  }
  template<class S> void getDist01(int root, S res[], void *mem = wmem){
    int i;
    int j;
    int k;
    int nd;
    int*q;
    int qs;
    int qe;
    char*don;
    walloc1d(&q, 2*N, &mem);
    walloc1d(&don, N, &mem);
    for(i=(0);i<(N);i++){
      res[i] = -1;
    }
    for(i=(0);i<(N);i++){
      don[i] = 0;
    }
    qs = qe = N;
    q[qe++] = root;
    res[root] = 0;
    while(qs < qe){
      i = q[qs++];
      if(don[i]){
        continue;
      }
      don[i] = 1;
      for(j=(0);j<(es[i]);j++){
        k = edge[i][j];
        nd = res[i] + cost[i][j];
        if(res[k]==-1 || res[k] > nd){
          res[k] = nd;
          if(cost[i][j]==0){
            q[--qs] = k;
          }
          else{
            q[qe++] = k;
          }
        }
      }
    }
  }
  int getDist01(int a, int b, void *mem = wmem){
    int i;
    int j;
    int k;
    int nd;
    int*res;
    int*q;
    int qs;
    int qe;
    char*don;
    walloc1d(&res, N, &mem);
    walloc1d(&q, 2*N, &mem);
    walloc1d(&don, N, &mem);
    for(i=(0);i<(N);i++){
      res[i] = -1;
    }
    for(i=(0);i<(N);i++){
      don[i] = 0;
    }
    qs = qe = N;
    q[qe++] = a;
    res[a] = 0;
    while(qs < qe){
      i = q[qs++];
      if(i==b){
        return res[b];
      }
      if(don[i]){
        continue;
      }
      don[i] = 1;
      for(j=(0);j<(es[i]);j++){
        k = edge[i][j];
        nd = res[i] + cost[i][j];
        if(res[k]==-1 || res[k] > nd){
          res[k] = nd;
          if(cost[i][j]==0){
            q[--qs] = k;
          }
          else{
            q[qe++] = k;
          }
        }
      }
    }
    return -1;
  }
}
;
int N;
int M;
int K;
int A[200000];
int B[200000];
int C[200000];
int d[200000];
wgraph<int> g;
int main(){
  wmem = memarr;
  int res;
  void*mem = wmem;
  rd(N);
  rd(M);
  rd(K);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(M);Lj4PdHRW++){
      rd(A[Lj4PdHRW]);A[Lj4PdHRW] += (-1);
      rd(B[Lj4PdHRW]);B[Lj4PdHRW] += (-1);
      rd(C[Lj4PdHRW]);
    }
  }
  int e98WHCEY;
  int cTE1_r3A;
  int RZTsC2BF;
  e98WHCEY = 0;
  cTE1_r3A = 300000;
  while(e98WHCEY < cTE1_r3A){
    int i;
    if((e98WHCEY + cTE1_r3A)%2==0){
      RZTsC2BF = (e98WHCEY + cTE1_r3A) / 2;
    }
    else{
      RZTsC2BF = (e98WHCEY + cTE1_r3A - 1) / 2;
    }
    wmem = mem;
    for(i=(0);i<(M);i++){
      if(C[i] > RZTsC2BF){
        d[i] =1;
      }
      else{
        d[i] =0;
      }
    }
    g.setEdge(N,M,A,B,d);
    if(g.getDist01(0,N-1) < K){
      cTE1_r3A = RZTsC2BF;
    }
    else{
      e98WHCEY = RZTsC2BF + 1;
    }
  }
  res =cTE1_r3A;
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay version 20210708-1

// --- original code ---
// int N, M, K, A[2d5], B[], C[], d[];
// wgraph<int> g;
// {
//   int res;
//   void *mem = wmem;
//   rd(N,M,K,(A--,B--,C)(M));
//   res = bsearch_min[int,x,0,3d5][
//     wmem = mem;
//     rep(i,M) d[i] = if[C[i] > x, 1, 0];
//     g.setEdge(N,M,A,B,d);
//   ](g.getDist01(0,N-1) < K);
//   wt(res);
// }
0