結果

問題 No.1244 Black Segment
ユーザー risujirohrisujiroh
提出日時 2020-10-02 22:05:21
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 3,060 bytes
コンパイル時間 5,872 ms
コンパイル使用メモリ 383,164 KB
実行使用メモリ 15,624 KB
最終ジャッジ日時 2023-09-24 12:32:17
合計ジャッジ時間 7,548 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,356 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 2 ms
4,356 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 1 ms
4,356 KB
testcase_08 AC 1 ms
4,356 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 1 ms
4,356 KB
testcase_13 AC 3 ms
4,356 KB
testcase_14 AC 26 ms
6,848 KB
testcase_15 AC 3 ms
4,476 KB
testcase_16 AC 27 ms
7,076 KB
testcase_17 AC 3 ms
4,356 KB
testcase_18 AC 46 ms
10,132 KB
testcase_19 AC 64 ms
11,276 KB
testcase_20 AC 67 ms
12,340 KB
testcase_21 AC 50 ms
12,168 KB
testcase_22 AC 73 ms
12,220 KB
testcase_23 AC 73 ms
12,108 KB
testcase_24 AC 70 ms
12,672 KB
testcase_25 AC 70 ms
12,936 KB
testcase_26 AC 61 ms
11,688 KB
testcase_27 AC 58 ms
11,080 KB
testcase_28 AC 70 ms
12,712 KB
testcase_29 AC 59 ms
11,916 KB
testcase_30 AC 71 ms
13,032 KB
testcase_31 AC 62 ms
12,220 KB
testcase_32 AC 60 ms
12,104 KB
testcase_33 AC 65 ms
12,376 KB
testcase_34 AC 91 ms
14,444 KB
testcase_35 AC 74 ms
14,964 KB
testcase_36 AC 75 ms
14,024 KB
testcase_37 AC 76 ms
15,624 KB
testcase_38 AC 73 ms
13,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt")
// #define NDEBUG

#include <bits/extc++.h>
#include <x86intrin.h>

struct rep {
  struct iter {
    int i;
    constexpr void operator++() { ++i; }
    constexpr int operator*() const { return i; }
    friend constexpr bool operator!=(iter a, iter b) { return *a != *b; }
  };
  const int l, r;
  constexpr rep(int _l, int _r) : l(std::min(_l, _r)), r(_r) {}
  constexpr rep(int n) : rep(0, n) {}
  constexpr iter begin() const { return {l}; }
  constexpr iter end() const { return {r}; }
};
struct per {
  struct iter {
    int i;
    constexpr void operator++() { --i; }
    constexpr int operator*() const { return i; }
    friend constexpr bool operator!=(iter a, iter b) { return *a != *b; }
  };
  const int l, r;
  constexpr per(int _l, int _r) : l(std::min(_l, _r)), r(_r) {}
  constexpr per(int n) : per(0, n) {}
  constexpr iter begin() const { return {r - 1}; }
  constexpr iter end() const { return {l - 1}; }
};
template <class T, class U>
constexpr bool chmin(T& a, U&& b) {
  return b < a ? a = std::forward<U>(b), true : false;
}
template <class T, class U>
constexpr bool chmax(T& a, U&& b) {
  return a < b ? a = std::forward<U>(b), true : false;
}

struct graph {
  struct edge {
    int src, dst, cost;

    int operator-(int v) const {
      assert(v == src or v == dst);
      return src ^ dst ^ v;
    }
  };

  int n, m = 0;
  std::vector<edge> edges;
  std::vector<std::vector<std::pair<int, int>>> adj;
  std::function<bool(int)> ignore = [](int) { return false; };

  graph() : n(0) {}
  explicit graph(int _n) : n(_n), adj(n) {}

  int add(const edge& e, bool directed) {
    assert(0 <= e.src), assert(e.src < n);
    assert(0 <= e.dst), assert(e.dst < n);
    edges.push_back(e);
    adj[e.src].emplace_back(m, e.dst);
    if (not directed) adj[e.dst].emplace_back(m, e.src);
    return m++;
  }
};

using namespace std;

template <class T>
pair<vector<T>, vector<int>> dijkstra(const graph& g, int s) {
  vector d(g.n, numeric_limits<T>::max()), pe(g.n, -1);
  priority_queue<pair<T, int>, vector<pair<T, int>>, greater<>> pq;
  pq.emplace(d[s] = 0, s);
  while (not empty(pq)) {
    auto [dv, v] = pq.top();
    pq.pop();
    if (dv > d[v]) continue;
    for (auto [id, u] : g.adj[v])
      if (dv + g.edges[id].cost < d[u]) {
        d[u] = dv + g.edges[id].cost;
        pq.emplace(d[u], u);
        pe[u] = id;
      }
  }
  return {d, pe};
}

int main() {
  using namespace std;
  cin.tie(nullptr)->sync_with_stdio(false);
  int n, m, a, b;
  cin >> n >> m >> a >> b;
  --a;
  int s = n + 1, t = s + 1;
  graph g(t + 1);
  while (m--) {
    int l, r;
    cin >> l >> r;
    --l;
    g.add({l, r, 1}, false);
  }
  for (int v : rep(a + 1)) g.add({s, v}, true);
  for (int v : rep(b, n + 1)) g.add({v, t}, true);
  auto d = dijkstra<int>(g, s).first;
  if (d[t] <= n) {
    cout << d[t] << '\n';
  } else {
    cout << "-1\n";
  }
}
0