結果

問題 No.614 壊れたキャンパス
ユーザー yuppe19 😺yuppe19 😺
提出日時 2017-12-14 12:52:10
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,855 bytes
コンパイル時間 1,702 ms
コンパイル使用メモリ 107,612 KB
実行使用メモリ 58,496 KB
最終ジャッジ日時 2024-05-08 09:40:32
合計ジャッジ時間 8,450 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,624 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 WA -
testcase_04 AC 2 ms
5,376 KB
testcase_05 WA -
testcase_06 AC 2 ms
5,376 KB
testcase_07 WA -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
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:22:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   22 |     int a, b, c; scanf("%d%d%d", &a, &b, &c);
      |                  ~~~~~^~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <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;
  vector<map<int, int>> G(N);// G[棟][階] := 右棟の階
  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<int>> outdegs(N, vector<int>()); // outdegs[i] = {i棟において、出次のある階のリスト}
  vector<set<int>> seen(N);
  for(int i=0; i<M; ++i) {
    int a, b, c; scanf("%d%d%d", &a, &b, &c);
    --a, --b, --c;
    G[a][b] = c;
    outdegs[a].push_back(b);
  }
  if(outdegs[0].size() == 0 || outdegs[N-2].size() == 0) {
    puts("-1");
    return 0;
  }
  while(!pq.empty()) {
    // 今 p棟 q階にいる。
    i64 _; int p, q; tie(_, p, q) = pq.top(); pq.pop();
    if(seen[p].count(q)) { continue; }
    seen[p].insert(q);
    // 同棟の x 階に移動する
    for(int& x : outdegs[p]) {
      if(!cost[p][x] || cost[p][x] > cost[p][q] + abs(q - x)) {
        cost[p][x] = cost[p][q] + abs(q - x);
        pq.emplace(cost[p][x], p, x);
      }
      // 渡り廊下で p+1棟 dst階に移動する
      int dst = G[p][x];
      if(!cost[p+1].count(dst) || cost[p+1][dst] > cost[p][q] + abs(q - x)) {
        cost[p+1][dst] = cost[p][q] + abs(q - x);
        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