結果

問題 No.614 壊れたキャンパス
ユーザー yuppe19 😺yuppe19 😺
提出日時 2017-12-14 14:39:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,557 bytes
コンパイル時間 1,242 ms
コンパイル使用メモリ 109,808 KB
実行使用メモリ 156,084 KB
最終ジャッジ日時 2024-05-08 09:50:18
合計ジャッジ時間 13,979 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
12,324 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 373 ms
18,816 KB
testcase_09 AC 374 ms
21,904 KB
testcase_10 AC 94 ms
20,308 KB
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 AC 372 ms
18,944 KB
testcase_15 AC 73 ms
13,184 KB
testcase_16 AC 182 ms
20,736 KB
testcase_17 AC 108 ms
36,224 KB
testcase_18 TLE -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;
using i64 = long long;

constexpr i64 inf = 987654321987654321LL;
int N, M, K, S, T;
int a, b, c;
i64 cc; int p, q;
i64 _cost;
i64 mini;
int k; i64 v;

int main(void) {
  scanf("%d%d%d%d%d", &N, &M, &K, &S, &T);
  --S, --T;
  priority_queue<tuple<i64, int, int>, vector<tuple<i64, int, int>>, greater<tuple<i64, int, int>>> pq;
  // コスト, 棟, 階
  pq.emplace(0, 0, S);
  vector<map<int, i64>> cost(N); // cost[棟][階] := 最小コスト
  cost[0][S] = 0;
  vector<vector<pair<int, int>>> G(N, vector<pair<int, int>>()); // G[i] := {(i棟の出次階, i+1棟の入次階)のリスト}
  for(int i=0; i<M; ++i) {
    scanf("%d%d%d", &a, &b, &c);
    --a, --b, --c;
    G[a].emplace_back(b, c);
  }
  while(!pq.empty()) {
    // 今 p棟 q階にいる。
    tie(cc, p, q) = pq.top(); pq.pop();
    if(!cost[p].count(q)) { continue; }
    _cost = cost[p][q];
    if(_cost < cc) { continue; }
    // 同棟の src 階に移動し、
    // 渡り廊下で p+1棟 dst階に移動する
    for(auto&& pr : G[p]) {
      int src, dst; tie(src, dst) = pr;
      if(!cost[p+1].count(dst) || cost[p+1][dst] > _cost + abs(q - src)) {
        cost[p+1][dst] = _cost + abs(q - src);
        pq.emplace(cost[p+1][dst], p+1, dst);
      }
    }
  }
  mini = inf;
  for(auto&& kv : cost[N-1]) {
    tie(k, v) = kv; // k階まででコストvかかった
    mini = min(mini, v + abs(T - k));
  }
  if(mini == inf) { mini = -1; }
  printf("%lld\n", mini);
  return 0;
}
0