結果

問題 No.1244 Black Segment
ユーザー risujirohrisujiroh
提出日時 2020-10-02 22:05:21
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 67 ms / 2,000 ms
コード長 3,060 bytes
コンパイル時間 4,461 ms
コンパイル使用メモリ 386,248 KB
最終ジャッジ日時 2025-01-15 00:20:11
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

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";
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0