結果

問題 No.1244 Black Segment
ユーザー tokusakuraitokusakurai
提出日時 2020-10-02 22:47:20
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 123 ms / 2,000 ms
コード長 2,140 bytes
コンパイル時間 2,407 ms
コンパイル使用メモリ 210,292 KB
実行使用メモリ 11,408 KB
最終ジャッジ日時 2023-09-24 22:15:08
合計ジャッジ時間 6,227 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 4 ms
4,380 KB
testcase_14 AC 53 ms
5,992 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 52 ms
6,216 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 61 ms
8,056 KB
testcase_19 AC 86 ms
9,120 KB
testcase_20 AC 93 ms
9,548 KB
testcase_21 AC 81 ms
9,596 KB
testcase_22 AC 98 ms
9,564 KB
testcase_23 AC 104 ms
9,604 KB
testcase_24 AC 102 ms
9,992 KB
testcase_25 AC 99 ms
10,096 KB
testcase_26 AC 94 ms
9,352 KB
testcase_27 AC 92 ms
8,872 KB
testcase_28 AC 102 ms
9,952 KB
testcase_29 AC 93 ms
9,628 KB
testcase_30 AC 98 ms
10,100 KB
testcase_31 AC 92 ms
9,708 KB
testcase_32 AC 98 ms
9,408 KB
testcase_33 AC 95 ms
9,688 KB
testcase_34 AC 123 ms
11,356 KB
testcase_35 AC 98 ms
11,336 KB
testcase_36 AC 98 ms
10,932 KB
testcase_37 AC 100 ms
11,408 KB
testcase_38 AC 100 ms
10,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const double pi = acos(-1.0);
const double EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

template<typename T>
struct Weighted_Graph{
    struct edge{
        int to; T cost;
        edge(int to, T cost) : to(to), cost(cost) {}
    };

    vector<vector<edge>> es;
    const T INF_T;
    const int n;

    Weighted_Graph(int n) : INF_T(numeric_limits<T>::max()), n(n){
        es.resize(n);
    }

    void add_edge(int from, int to, T cost, bool directed = false){
        es[from].eb(to, cost);
        if(!directed) es[to].eb(from, cost);
    }

    T dijkstra(int s, int t){
        vector<T> d(n, INF_T);
        using P = pair<T, int>;
        priority_queue<P, vector<P>, greater<P> > que;
        que.emplace(d[s] = 0, s);
        while(!que.empty()){
            T p; int i;
            tie(p, i) = que.top(); que.pop();
            if(p > d[i]) continue;
            for(auto &e: es[i]){
                if(chmin(d[e.to], d[i]+e.cost)) que.emplace(d[e.to], e.to);
            }
        }
        return (d[t] == INF_T? -1 : d[t]);
    }
};

int main(){
    int N, M, A, B;
    cin >> N >> M >> A >> B;
    Weighted_Graph<int> G(N+3);
    int s = N+1, t = N+2;
    rep2(i, 0, A-1) G.add_edge(s, i, 0, true);
    rep2(i, B, N) G.add_edge(i, t, 0, true);
    rep(i, M){
        int l, r; cin >> l >> r; l--;
        G.add_edge(l, r, 1);
    }
    cout << G.dijkstra(s, t) << endl;
}
0