結果

問題 No.614 壊れたキャンパス
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-05-29 06:02:46
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 904 ms / 2,000 ms
コード長 3,437 bytes
コンパイル時間 1,039 ms
コンパイル使用メモリ 130,724 KB
実行使用メモリ 111,268 KB
最終ジャッジ日時 2023-09-04 01:27:17
合計ジャッジ時間 11,268 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 741 ms
70,124 KB
testcase_09 AC 804 ms
63,636 KB
testcase_10 AC 851 ms
111,268 KB
testcase_11 AC 824 ms
65,860 KB
testcase_12 AC 836 ms
65,036 KB
testcase_13 AC 805 ms
64,632 KB
testcase_14 AC 760 ms
71,744 KB
testcase_15 AC 692 ms
93,060 KB
testcase_16 AC 739 ms
74,624 KB
testcase_17 AC 904 ms
109,420 KB
testcase_18 AC 482 ms
48,560 KB
testcase_19 AC 455 ms
53,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


int N, M;
long K, S, T;
int[] A;
long[] B, C;

void main() {
  try {
    for (; ; ) {
      N = readInt();
      M = readInt();
      K = readLong();
      S = readLong();
      T = readLong();
      A = new int[M];
      B = new long[M];
      C = new long[M];
      foreach (i; 0 .. M) {
        A[i] = readInt() - 1;
        B[i] = readLong();
        C[i] = readLong();
      }
      
      auto ys = new long[][N];
      foreach (a; 0 .. N) {
        ys[a] ~= 1;
        ys[a] ~= K;
      }
      ys[0] ~= S;
      ys[N - 1] ~= T;
      foreach (i; 0 .. M) {
        ys[A[i]] ~= B[i];
        ys[A[i] + 1] ~= C[i];
      }
      auto len = new int[N];
      int V;
      auto ids = new int[][N];
      foreach (a; 0 .. N) {
        ys[a] = ys[a].sort.uniq.array;
        len[a] = cast(int)(ys[a].length);
        ids[a] = iota(V, V + len[a]).array;
        V += len[a];
      }
      debug {
        writeln("len = ", len);
        writeln("ys = ", ys);
        writeln("V = ", V);
        writeln("ids = ", ids);
      }
      
      alias Edge = Tuple!(long, int);
      auto graph = new Edge[][V];
      foreach (a; 0 .. N) {
        foreach (j; 0 .. len[a] - 1) {
          const u = ids[a][j];
          const v = ids[a][j + 1];
          const c = ys[a][j + 1] - ys[a][j];
          graph[u] ~= Edge(c, v);
          graph[v] ~= Edge(c, u);
        }
      }
      int getId(int a, long y) {
        return ids[a][ys[a].lowerBound(y)];
      }
      const src = getId(0, S);
      const dst = getId(N - 1, T);
      foreach (i; 0 .. M) {
        const u = getId(A[i], B[i]);
        const v = getId(A[i] + 1, C[i]);
        graph[u] ~= Edge(0, v);
      }
      
      BinaryHeap!(Array!Edge, "a > b") q;
      auto dist = new long[V];
      dist[] = -1;
      dist[src] = 0;
      q.insert(Edge(0, src));
      long ans = -1;
      for (; !q.empty; ) {
        const c = q.front[0];
        const u = q.front[1];
        q.removeFront;
        if (dist[u] == c) {
          foreach (e; graph[u]) {
            const cc = c + e[0];
            const v = e[1];
            if (dist[v] == -1 || dist[v] > cc) {
              dist[v] = cc;
              q.insert(Edge(cc, v));
            }
          }
        }
      }
      writeln(dist[dst]);
    }
  } catch (EOFException e) {
  }
}
0