結果

問題 No.614 壊れたキャンパス
ユーザー yuppe19 😺yuppe19 😺
提出日時 2017-12-14 15:42:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,301 bytes
コンパイル時間 1,004 ms
コンパイル使用メモリ 102,496 KB
実行使用メモリ 53,448 KB
最終ジャッジ日時 2024-05-08 10:04:38
合計ジャッジ時間 8,469 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,752 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 220 ms
15,104 KB
testcase_09 AC 125 ms
16,152 KB
testcase_10 AC 107 ms
22,016 KB
testcase_11 AC 779 ms
14,208 KB
testcase_12 AC 787 ms
14,336 KB
testcase_13 AC 787 ms
14,208 KB
testcase_14 AC 223 ms
14,976 KB
testcase_15 AC 75 ms
13,940 KB
testcase_16 AC 143 ms
18,688 KB
testcase_17 AC 132 ms
53,448 KB
testcase_18 TLE -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <tuple>
#include <vector>
using namespace std;
using i64 = long long;

constexpr i64 inf = 987654321987654321LL;

int main(void) {
  int N, M, K, S, T; scanf("%d%d%d%d%d", &N, &M, &K, &S, &T);
  --S, --T;
  vector<unordered_map<int, i64>> dp(N); // dp[階] := 最小コスト
  dp[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) {
    int a, b, c; scanf("%d%d%d", &a, &b, &c);
    --a, --b, --c;
    G[a].emplace_back(b, c);
  }

  for(int i=0; i<N; ++i) { // 棟 i
    for(auto&& pr : G[i]) {
      int src, dst; tie(src, dst) = pr; // 棟 i の src階から 棟 i+1 の dst階へは無料
      for(auto&& kv : dp[i]) {
        int k; i64 v; tie(k, v) = kv; // 棟 i の k 階は最小コスト v (dp[i][k] = v)
        if(dp[i+1].count(dst)) {
          dp[i+1][dst] = min(dp[i+1][dst], v + abs(k - src));
        } else {
          dp[i+1][dst] = v + abs(k - src);
        }
      }
    }
  }

  i64 mini = inf;
  for(auto&& kv : dp[N-1]) {
    int k; i64 v; tie(k, v) = kv;
    mini = min(mini, kv.second + abs(T - kv.first));
  }
  if(mini == inf) { mini = -1; }
  printf("%lld\n", mini);
  return 0;
}
0