結果

問題 No.614 壊れたキャンパス
ユーザー yuppe19 😺yuppe19 😺
提出日時 2017-12-14 19:12:11
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 2,773 bytes
コンパイル時間 1,293 ms
コンパイル使用メモリ 91,148 KB
実行使用メモリ 814,312 KB
最終ジャッジ日時 2024-05-08 10:13:18
合計ジャッジ時間 8,183 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 335 ms
61,460 KB
testcase_09 AC 160 ms
28,884 KB
testcase_10 AC 217 ms
36,480 KB
testcase_11 AC 755 ms
226,836 KB
testcase_12 AC 744 ms
228,624 KB
testcase_13 AC 725 ms
227,156 KB
testcase_14 AC 350 ms
61,316 KB
testcase_15 AC 185 ms
27,560 KB
testcase_16 AC 269 ms
39,408 KB
testcase_17 AC 157 ms
59,576 KB
testcase_18 MLE -
testcase_19 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:13:27: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   13 |   int N, M, K, S, T; scanf("%d%d%d%d%d", &N, &M, &K, &S, &T);
      |                      ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:28:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   28 |     int a, b, c; scanf("%d%d%d", &a, &b, &c);
      |                  ~~~~~^~~~~~~~~~~~~~~~~~~~~~

ソースコード

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<vector<int>> cor0(N);
  vector<vector<int>> mat0(N), mat1(N);
  // 始点
  // cor0[a][p] = i 
  // mat0[a][p] = b ... a棟 b階は ノードi a棟で p番目
  // 終点
  // cor1[a+1][p] = i 
  // mat1[a+1][p] = b ... a棟 b階は ノードi a棟で p番目
  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);
    cor0[a].push_back(i);
    mat0[a].push_back(b);
//    cor1[a+1].push_back(i);
    mat1[a+1].push_back(c);
  }
  vector<vector<edge>> H(M+2);
  // a棟 -> a+1棟
  // a棟   b階 -> a+1棟 c階への渡り廊下
  // a+1棟 x階 -> a+2棟 y階への渡り廊下
  for(int a=0; a<N-1; ++a) {
    for(int i=0, n=cor0[a].size(); i<n; ++i) {
      int node0 = cor0[a][i];
//      int b     = mat0[a][i];
      int c     = mat1[a+1][i];
      for(int j=0, m=cor0[a+1].size(); j<m; ++j) {
        int node1 = cor0[a+1][j];
        int x     = mat0[a+1][j];
//        int y     = mat1[a+2][j];
        H[node0].push_back(edge{node1, abs(c - x)});
      }
    }
  }
  // 0棟 p階 -> 1棟 q階への渡り廊下
  for(int i=0, n=cor0[0].size(); i<n; ++i) {
    int node1 = cor0[0][i];
    int p     = mat0[0][i];
    H[M].push_back(edge{node1, abs(S - p)});
  }
  // N-2棟 b階 -> N-1棟 c階への渡り廊下
  for(int i=0, n=cor0[N-2].size(); i<n; ++i) {
    int node0 = cor0[N-2][i];
    int c     = mat1[N-1][i];
    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