結果

問題 No.614 壊れたキャンパス
ユーザー yuppe19 😺yuppe19 😺
提出日時 2017-12-14 14:08:27
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 1,569 bytes
コンパイル時間 1,055 ms
コンパイル使用メモリ 99,304 KB
実行使用メモリ 546,472 KB
最終ジャッジ日時 2024-05-08 09:44:04
合計ジャッジ時間 11,271 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,624 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 410 ms
14,976 KB
testcase_09 AC 223 ms
18,828 KB
testcase_10 AC 99 ms
21,760 KB
testcase_11 AC 1,437 ms
14,248 KB
testcase_12 AC 1,443 ms
14,284 KB
testcase_13 AC 1,411 ms
14,508 KB
testcase_14 AC 415 ms
14,848 KB
testcase_15 AC 73 ms
13,936 KB
testcase_16 AC 212 ms
18,560 KB
testcase_17 AC 143 ms
53,436 KB
testcase_18 MLE -
testcase_19 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:11:27: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   11 |   int N, M, K, S, T; scanf("%d%d%d%d%d", &N, &M, &K, &S, &T);
      |                      ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:20:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   20 |     int a, b, c; scanf("%d%d%d", &a, &b, &c);
      |                  ~~~~~^~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

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

int main(void) {
  constexpr i64 inf = 987654321987654321LL;
  int N, M, K, S, T; 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) {
    int a, b, c; scanf("%d%d%d", &a, &b, &c);
    --a, --b, --c;
    G[a].emplace_back(b, c);
  }
  while(!pq.empty()) {
    // 今 p棟 q階にいる。
    i64 cc; int p, q; tie(cc, p, q) = pq.top(); pq.pop();
    if(!cost[p].count(q)) { continue; }
    if(cost[p][q] < cc) { continue; }
    // 同棟の src 階に移動する
    for(auto&& pr : G[p]) {
      int src, dst; tie(src, dst) = pr;
      // 渡り廊下で p+1棟 dst階に移動する
      if(!cost[p+1].count(dst) || cost[p+1][dst] > cost[p][q] + abs(q - src)) {
        cost[p+1][dst] = cost[p][q] + abs(q - src);
        pq.emplace(cost[p+1][dst], p+1, dst);
      }
    }
  }
  i64 mini = inf;
  for(auto&& kv : cost[N-1]) {
    int k; i64 v; 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