結果

問題 No.614 壊れたキャンパス
ユーザー simansiman
提出日時 2023-06-14 21:30:13
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,565 bytes
コンパイル時間 5,463 ms
コンパイル使用メモリ 113,792 KB
実行使用メモリ 114,476 KB
最終ジャッジ日時 2023-09-05 07:09:58
合計ジャッジ時間 15,938 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,268 KB
testcase_01 AC 4 ms
8,160 KB
testcase_02 AC 5 ms
8,200 KB
testcase_03 WA -
testcase_04 AC 5 ms
8,252 KB
testcase_05 AC 3 ms
8,100 KB
testcase_06 AC 4 ms
8,104 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 1,249 ms
64,888 KB
testcase_10 AC 270 ms
21,552 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 192 ms
16,764 KB
testcase_16 WA -
testcase_17 AC 889 ms
114,476 KB
testcase_18 AC 479 ms
52,256 KB
testcase_19 AC 493 ms
47,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

struct Edge {
  int a;
  int b;
  int c;

  Edge(int a = -1, int b = -1, int c = -1) {
    this->a = a;
    this->b = b;
    this->c = c;
  }

  bool operator<(const Edge &e) const {
    return b < e.b;
  }
};

vector<Edge> G[200010];

int main() {
  int N, M, K, S, T;
  cin >> N >> M >> K >> S >> T;

  for (int i = 0; i < M; ++i) {
    int a, b, c;
    cin >> a >> b >> c;

    G[a].push_back(Edge(a, b, c));
  }

  set<int> P[N + 1];
  map<int, map<int, ll>> min_cost;
  map<int, map<int, bool>> visited;

  for (int i = 1; i <= N - 1; ++i) {
    sort(G[i].begin(), G[i].end());
  }

  for (int i = 1; i < N; ++i) {
    if (G[i].empty()) {
      cout << -1 << endl;
      return 0;
    }

    if (i == 1) {
      for (Edge &e : G[i]) {
        min_cost[i][e.b] = abs(S - e.b);
        visited[i][e.b] = true;
      }
    } else {
      {
        int cur_pos = *P[i].begin();
        ll cur_cost = min_cost[i][cur_pos];

        for (Edge &e : G[i]) {
          ll new_cost = cur_cost + abs(e.b - cur_pos);

          if (not visited[i][e.b] || min_cost[i][e.b] > new_cost) {
            min_cost[i][e.b] = new_cost;
            cur_pos = e.b;
            cur_cost = new_cost;
            visited[i][e.b] = true;
          }
        }
      }

      {
        int cur_pos = *P[i].rbegin();
        ll cur_cost = min_cost[i][cur_pos];
        reverse(G[i].begin(), G[i].end());

        for (Edge &e : G[i]) {
          ll new_cost = cur_cost + abs(e.b - cur_pos);

          if (not visited[i][e.b] || min_cost[i][e.b] > new_cost) {
            min_cost[i][e.b] = new_cost;
            cur_pos = e.b;
            cur_cost = new_cost;
            visited[i][e.b] = true;
          }
        }

        reverse(G[i].begin(), G[i].end());
      }
    }

    for (Edge &e : G[i]) {
      // fprintf(stderr, "i: %d, b: %d, cost: %lld\n", i, e.b, min_cost[i][e.b]);
      if (not visited[i + 1][e.c] || min_cost[i + 1][e.c] > min_cost[i][e.b]) {
        min_cost[i + 1][e.c] = min_cost[i][e.b];
        visited[i + 1][e.c] = true;
      }
      P[i + 1].insert(e.c);

      // fprintf(stderr, "%d: k: %d, cost: %lld\n", i + 1, e.c, min_cost[i + 1][e.c]);
    }
  }

  ll ans = LLONG_MAX;

  for (int k : P[N]) {
    ll cost = min_cost[N][k] + abs(k - T);
    ans = min(ans, cost);
  }

  cout << ans << endl;

  return 0;
}
0