結果

問題 No.614 壊れたキャンパス
ユーザー yuppe19 😺yuppe19 😺
提出日時 2017-12-14 18:35:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,486 bytes
コンパイル時間 1,584 ms
コンパイル使用メモリ 113,084 KB
実行使用メモリ 292,756 KB
最終ジャッジ日時 2024-05-08 10:10:19
合計ジャッジ時間 13,707 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,884 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 677 ms
76,760 KB
testcase_09 AC 797 ms
44,664 KB
testcase_10 AC 369 ms
54,904 KB
testcase_11 AC 1,387 ms
241,920 KB
testcase_12 AC 1,394 ms
243,704 KB
testcase_13 AC 1,351 ms
242,248 KB
testcase_14 AC 686 ms
76,904 KB
testcase_15 AC 354 ms
43,912 KB
testcase_16 AC 493 ms
53,580 KB
testcase_17 AC 152 ms
64,364 KB
testcase_18 TLE -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
using i64 = long long;

constexpr i64 inf = 987654321987654321LL;
struct edge { int to, cost; };

int main(void) {
  int N, M, K, S, T; scanf("%d%d%d%d%d", &N, &M, &K, &S, &T);
  --S, --T;
  // G[i] := { (i棟の始点の階, i+1棟の終点の階)のリスト }
  vector<vector<pair<int, int>>> G(N);
//  vector<pair<int, int>> corr0(M), corr1(M); // corr0[i] := 渡り廊下始点の (棟, 階)
//                                             // corr1[i] := 渡り廊下終点の (棟, 階)
  vector<map<int, int>> corr0_rev(N), corr1_rev(N);
  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);
    pair<int, int> pr0 = make_pair(a, b),
                   pr1 = make_pair(a+1, c);
//    corr0[i] = pr0;
//    corr1[i] = pr1;
    corr0_rev[a]  [b] = i;
    corr1_rev[a+1][c] = i;
  }
  vector<vector<edge>> H(M+2);
  // a棟 -> a+1棟
  for(int a=0; a<N-1; ++a) {
    for(auto pr0 : G[a]) {
      int b, c; tie(b, c) = pr0; // a棟 b階 -> a+1棟 c階への渡り廊下
      int node0 = corr0_rev[a][b];
      for(auto pr1 : G[a+1]) { // a+1棟 p階 -> a+2棟 q階への渡り廊下
        int p, q; tie(p, q) = pr1;
        int node1 = corr0_rev[a+1][p];
        H[node0].push_back(edge{node1, abs(c - p)});
      }
    }
  }
  for(auto pr1 : G[0]) {
    int p, q; tie(p, q) = pr1; // 0棟 p階 -> 1棟 q階への渡り廊下
    int node1 = corr0_rev[0][p];
    H[M].push_back(edge{node1, abs(S - p)});
  }
  for(auto pr0 : G[N-2]) {
    int b, c; tie(b, c) = pr0; // N-2棟 b階 -> N-1棟 c階への渡り廊下
    int node0 = corr0_rev[N-2][b];
    H[node0].push_back(edge{M+1, abs(c - T)});
  }

  // コーナーケース
  if(N == 1) {
    H[M].push_back(edge{M+1, abs(S - T)});
  }

  vector<i64> cost(M+2, inf); // cost[M+2]
  cost[M] = 0;
  priority_queue<pair<i64, int>, vector<pair<i64, int>>, greater<pair<i64, int>>> pq;
  vector<bool> seen(M+2, false); // seen[M+2]
  // コスト、点番号
  pq.emplace(0, M);
  while(!pq.empty()) {
    i64 _, v; tie(_, v) = pq.top(); pq.pop();
    if(seen[v]) { continue; }
    seen[v] = true;
    for(edge e : H[v]) {
      if(cost[e.to] > cost[v] + e.cost) {
        cost[e.to] = cost[v] + e.cost;
        pq.emplace(cost[e.to], e.to);
      }
    }
  }
  i64 res = cost[M+1];
  if(res == inf) { res = -1; }
  printf("%lld\n", res);
  return 0;
}
0