結果

問題 No.614 壊れたキャンパス
ユーザー yuppe19 😺yuppe19 😺
提出日時 2017-12-14 15:04:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 1,595 bytes
コンパイル時間 1,308 ms
コンパイル使用メモリ 114,360 KB
実行使用メモリ 814,080 KB
最終ジャッジ日時 2024-05-08 10:02:41
合計ジャッジ時間 10,514 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 337 ms
14,976 KB
testcase_09 AC 209 ms
18,968 KB
testcase_10 AC 101 ms
21,880 KB
testcase_11 AC 1,106 ms
14,380 KB
testcase_12 AC 1,092 ms
14,412 KB
testcase_13 AC 1,084 ms
14,504 KB
testcase_14 AC 348 ms
14,976 KB
testcase_15 AC 75 ms
13,808 KB
testcase_16 AC 187 ms
18,560 KB
testcase_17 AC 139 ms
53,380 KB
testcase_18 MLE -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <queue>
#include <unordered_set>
using namespace std;
using u64 = unsigned long long;

constexpr u64 inf = 987654321987654321ULL;
int N, M, K, S, T;
int a, b, c;
u64 cc; int p, q;
u64 _cost;
u64 mini;
int k; u64 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<u64, int, int>, vector<tuple<u64, int, int>>, greater<tuple<u64, int, int>>> pq;
  // コスト, 棟, 階
  pq.emplace(0, 0, S);
  vector<unordered_map<int, u64>> 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(_cost < cc) { continue; }
    if(p == N-1) {
      mini = min(mini, _cost + abs(T - q));
    }
    // 同棟の 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);
      }
    }
  }
  if(mini == inf) {
    puts("-1");
  } else {
    printf("%llu\n", mini);
  }
  return 0;
}
0