結果

問題 No.2387 Yokan Factory
ユーザー 👑 chro_96chro_96
提出日時 2023-07-21 22:39:29
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 1,253 ms / 5,000 ms
コード長 2,975 bytes
コンパイル時間 550 ms
コンパイル使用メモリ 31,512 KB
実行使用メモリ 14,464 KB
最終ジャッジ日時 2023-10-21 22:40:25
合計ジャッジ時間 6,637 ms
ジャッジサーバーID
(参考情報)
judge10 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
14,464 KB
testcase_01 AC 5 ms
14,464 KB
testcase_02 AC 5 ms
14,400 KB
testcase_03 AC 6 ms
14,464 KB
testcase_04 AC 5 ms
14,464 KB
testcase_05 AC 5 ms
14,400 KB
testcase_06 AC 5 ms
14,464 KB
testcase_07 AC 5 ms
14,464 KB
testcase_08 AC 6 ms
14,464 KB
testcase_09 AC 6 ms
14,464 KB
testcase_10 AC 6 ms
14,400 KB
testcase_11 AC 5 ms
14,464 KB
testcase_12 AC 6 ms
14,464 KB
testcase_13 AC 5 ms
14,400 KB
testcase_14 AC 6 ms
14,464 KB
testcase_15 AC 224 ms
14,464 KB
testcase_16 AC 114 ms
14,464 KB
testcase_17 AC 276 ms
14,464 KB
testcase_18 AC 814 ms
14,464 KB
testcase_19 AC 135 ms
14,464 KB
testcase_20 AC 69 ms
14,464 KB
testcase_21 AC 360 ms
14,464 KB
testcase_22 AC 80 ms
14,464 KB
testcase_23 AC 1,253 ms
14,464 KB
testcase_24 AC 86 ms
14,464 KB
testcase_25 AC 79 ms
14,400 KB
testcase_26 AC 125 ms
14,400 KB
testcase_27 AC 71 ms
14,400 KB
testcase_28 AC 6 ms
14,464 KB
testcase_29 AC 6 ms
14,464 KB
testcase_30 AC 7 ms
14,464 KB
testcase_31 AC 6 ms
14,464 KB
testcase_32 AC 6 ms
14,464 KB
testcase_33 AC 6 ms
14,464 KB
testcase_34 AC 7 ms
14,464 KB
testcase_35 AC 7 ms
14,400 KB
testcase_36 AC 6 ms
14,464 KB
testcase_37 AC 6 ms
14,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

#define INF 1000000000000000001LL

typedef struct list {
  int v;
  long long a;
  long long b;
  struct list *n;
} list;

void upheap (long long q[][2], int idx) {
  int pidx = (idx-1)/2;
  
  if (idx > 0 && q[pidx][0] > q[idx][0]) {
    long long s = q[idx][0];
    q[idx][0] = q[pidx][0];
    q[pidx][0] = s;
    s = q[idx][1];
    q[idx][1] = q[pidx][1];
    q[pidx][1] = s;
    upheap(q, pidx);
  }
  
  return;
}

void downheap (long long q[][2], int idx, int size) {
  long long ch[2] = { INF, INF };
  int chidx = -1;
  
  if (2*idx+1 >= size) {
    return;
  }
  
  ch[0] = q[2*idx+1][0];
  if (2*idx+2 < size) {
    ch[1] = q[2*idx+2][1];
  }
  
  if (ch[1] < ch[0] && ch[1] < q[idx][0]) {
    chidx = 2*idx+2;
  } else if (ch[0] < q[idx][0]) {
    chidx = 2*idx+1;
  }
  
  if (chidx > 0) {
    long long s = q[idx][0];
    q[idx][0] = q[chidx][0];
    q[chidx][0] = s;
    s = q[idx][1];
    q[idx][1] = q[chidx][1];
    q[chidx][1] = s;
    downheap(q, chidx, size);
  }
}

void dequeue (long long q[][2], int *size, long long *res) {
  res[0] = q[0][0];
  res[1] = q[0][1];
  
  if (*size > 0) {
    *size -= 1;
    q[0][0] = q[*size][0];
    q[0][1] = q[*size][1];
    downheap(q, 0, *size);
  }
  
  return;
}

int main () {
  int n = 0;
  int m = 0;
  long long x = 0LL;
  int u = 0;
  int v = 0;
  long long a = 0LL;
  long long b = 0LL;
  
  int res = 0;
  
  long long ans[2] = { 0LL, 1000000001LL };
  
  list pool[200000] = {};
  int used = 0;
  
  list *e[100000] = {};
  long long d[100000] = {};
  
  long long q[300000][2] = {};
  
  res = scanf("%d", &n);
  res = scanf("%d", &m);
  res = scanf("%lld", &x);
  for (int i = 0; i < m; i++) {
    res = scanf("%d", &u);
    res = scanf("%d", &v);
    res = scanf("%lld", &a);
    res = scanf("%lld", &b);
    u--;
    v--;
    pool[used].v = v;
    pool[used].a = a;
    pool[used].b = b;
    pool[used].n = e[u];
    e[u] = pool+used;
    used++;
    pool[used].v = u;
    pool[used].a = a;
    pool[used].b = b;
    pool[used].n = e[v];
    e[v] = pool+used;
    used++;
  }
  
  while (ans[1]-ans[0] > 1LL) {
    long long nxt = (ans[0]+ans[1])/2LL;
    int qsize = 0;
    for (int i = 0; i < n; i++) {
      d[i] = x+1LL;
    }
    d[0] = 0LL;
    q[qsize][0] = 0LL;
    q[qsize][1] = 0LL;
    upheap(q, qsize);
    qsize++;
    while (qsize > 0) {
      long long tmp[2] = {};
      dequeue(q, &qsize, tmp);
      if (tmp[0] == d[(int)tmp[1]]) {
        list *l = e[(int)tmp[1]];
        while (l != NULL) {
          if (l->b >= nxt && d[l->v] > tmp[0]+l->a) {
            d[l->v] = tmp[0]+l->a;
            q[qsize][0] = d[l->v];
            q[qsize][1] = (long long)(l->v);
            upheap(q, qsize);
            qsize++;
          }
          l = l->n;
        }
      }
    }
    if (d[n-1] > x) {
      ans[1] = nxt;
    } else {
      ans[0] = nxt;
    }
  }
  
  if (ans[0] < 1LL) {
    printf("-1\n");
  } else {
    printf("%lld\n", ans[0]);
  }
  
  return 0;
}
0