結果

問題 No.614 壊れたキャンパス
ユーザー yuppe19 😺yuppe19 😺
提出日時 2017-12-14 14:21:05
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,626 bytes
コンパイル時間 1,241 ms
コンパイル使用メモリ 95,220 KB
実行使用メモリ 153,796 KB
最終ジャッジ日時 2024-05-08 09:45:36
合計ジャッジ時間 13,973 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 WA -
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 WA -
testcase_08 AC 382 ms
18,816 KB
testcase_09 AC 381 ms
21,204 KB
testcase_10 AC 92 ms
20,352 KB
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 AC 373 ms
19,072 KB
testcase_15 AC 72 ms
13,056 KB
testcase_16 AC 182 ms
20,608 KB
testcase_17 AC 105 ms
36,068 KB
testcase_18 TLE -
testcase_19 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:10:27: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   10 |   int N, M, K, S, T; scanf("%d%d%d%d%d", &N, &M, &K, &S, &T);
      |                      ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:19:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   19 |     int a, b, c; scanf("%d%d%d", &a, &b, &c);
      |                  ~~~~~^~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
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<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);
  }
  if(G[0].size() == 0 || G[N-2].size() == 0) {
    puts("-1");
    return 0;
  }
  while(!pq.empty()) {
    // 今 p棟 q階にいる。
    i64 cc; int p, q; tie(cc, p, q) = pq.top(); pq.pop();
    if(!cost[p].count(q)) { continue; }
    i64 _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);
      }
    }
  }
  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