結果

問題 No.614 壊れたキャンパス
ユーザー yuppe19 😺yuppe19 😺
提出日時 2017-12-14 14:55:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,650 bytes
コンパイル時間 1,318 ms
コンパイル使用メモリ 110,516 KB
実行使用メモリ 814,468 KB
最終ジャッジ日時 2023-08-21 04:27:41
合計ジャッジ時間 11,390 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 351 ms
14,644 KB
testcase_09 AC 230 ms
19,240 KB
testcase_10 AC 106 ms
21,668 KB
testcase_11 AC 1,135 ms
14,044 KB
testcase_12 AC 1,155 ms
14,092 KB
testcase_13 AC 1,139 ms
14,208 KB
testcase_14 AC 357 ms
14,664 KB
testcase_15 AC 82 ms
13,516 KB
testcase_16 AC 203 ms
18,424 KB
testcase_17 AC 148 ms
53,192 KB
testcase_18 MLE -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <unordered_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 src, dst;
int dif;

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<unordered_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);
  }
  mini = inf;
  while(!pq.empty()) {
    // 今 p棟 q階にいる。
    tie(cc, p, q) = pq.top(); pq.pop();
    if(!cost[p].count(q)) { continue; }
    _cost = cost[p][q];
    if(p == N-1) {
      mini = min(mini, _cost + abs(T - q));
    }
    if(_cost < cc) { continue; }
    // 同棟の src 階に移動し、
    // 渡り廊下で p+1棟 dst階に移動する
    for(auto&& pr : G[p]) {
      dif = abs(q - pr.first);
      if(!cost[p+1].count(pr.second) || cost[p+1][pr.second] > _cost + dif) {
        cost[p+1][pr.second] = _cost + dif;
        pq.emplace(cost[p+1][pr.second], p+1, pr.second);
      }
    }
  }
//  mini = inf;
//  for(auto&& kv : cost[N-1]) {
//    mini = min(mini, kv.second + abs(T - kv.first));
//  }
  if(mini == inf) { mini = -1; }
  printf("%lld\n", mini);
  return 0;
}
0