結果

問題 No.614 壊れたキャンパス
ユーザー pekempeypekempey
提出日時 2017-12-14 01:28:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 182 ms / 2,000 ms
コード長 1,402 bytes
コンパイル時間 2,130 ms
コンパイル使用メモリ 91,828 KB
実行使用メモリ 38,196 KB
最終ジャッジ日時 2023-08-21 02:52:28
合計ジャッジ時間 4,896 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 117 ms
13,368 KB
testcase_09 AC 163 ms
12,288 KB
testcase_10 AC 182 ms
28,424 KB
testcase_11 AC 108 ms
12,060 KB
testcase_12 AC 125 ms
12,080 KB
testcase_13 AC 111 ms
12,316 KB
testcase_14 AC 111 ms
13,380 KB
testcase_15 AC 131 ms
20,772 KB
testcase_16 AC 121 ms
15,496 KB
testcase_17 AC 136 ms
38,196 KB
testcase_18 AC 92 ms
10,844 KB
testcase_19 AC 91 ms
10,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>

using namespace std;

int main() {
  int n, m, k, s, t;
  cin >> n >> m >> k >> s >> t;

  vector<int> a(m), b(m), c(m);
  vector<vector<int>> g(n), gg(n);
  g[0].push_back(s);
  g[n - 1].push_back(t);
  for (int i = 0; i < m; i++) {
    scanf("%d %d %d", &a[i], &b[i], &c[i]);
    g[a[i] - 1].push_back(b[i]);
    g[a[i]    ].push_back(c[i]);
    gg[a[i] - 1].push_back(i);
  }
  vector<vector<long long>> dp(n);
  for (int i = 0; i < n; i++) {
    sort(g[i].begin(), g[i].end());
    g[i].erase(unique(g[i].begin(), g[i].end()), g[i].end());
    dp[i].resize(g[i].size(), 1e18);
  }
  auto index = [&](vector<int> &a, int v) { return lower_bound(a.begin(), a.end(), v) - a.begin(); };
  int S = index(g[0], s);
  int T = index(g[n - 1], t);
  dp[0][S] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j + 1 < g[i].size(); j++) {
      dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + g[i][j + 1] - g[i][j]);
    }
    for (int j = (int)g[i].size() - 2; j >= 0; j--) {
      dp[i][j] = min(dp[i][j], dp[i][j + 1] + g[i][j + 1] - g[i][j]);
    }
    for (int j : gg[i]) {
      int u = index(g[i], b[j]);
      int v = index(g[i + 1], c[j]);
      dp[i + 1][v] = min(dp[i + 1][v], dp[i][u]);
    }
  }
  if (dp[n - 1][T] >= 1e17) {
    cout << -1 << endl;
  } else {
    cout << dp[n - 1][T] << endl;
  }
}
0