結果

問題 No.1244 Black Segment
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-10-02 22:52:52
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 120 ms / 2,000 ms
コード長 1,677 bytes
コンパイル時間 1,898 ms
コンパイル使用メモリ 179,636 KB
実行使用メモリ 12,096 KB
最終ジャッジ日時 2023-09-24 22:25:55
合計ジャッジ時間 5,710 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 4 ms
4,376 KB
testcase_14 AC 53 ms
6,032 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 53 ms
6,128 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 65 ms
8,464 KB
testcase_19 AC 90 ms
9,232 KB
testcase_20 AC 96 ms
9,576 KB
testcase_21 AC 86 ms
9,708 KB
testcase_22 AC 103 ms
9,840 KB
testcase_23 AC 103 ms
10,004 KB
testcase_24 AC 104 ms
10,088 KB
testcase_25 AC 106 ms
10,048 KB
testcase_26 AC 94 ms
9,388 KB
testcase_27 AC 93 ms
8,804 KB
testcase_28 AC 105 ms
10,416 KB
testcase_29 AC 96 ms
9,820 KB
testcase_30 AC 105 ms
10,212 KB
testcase_31 AC 97 ms
9,652 KB
testcase_32 AC 103 ms
9,308 KB
testcase_33 AC 101 ms
9,608 KB
testcase_34 AC 120 ms
11,152 KB
testcase_35 AC 109 ms
12,096 KB
testcase_36 AC 107 ms
11,408 KB
testcase_37 AC 104 ms
10,852 KB
testcase_38 AC 100 ms
10,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.10.02 22:52:45
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;




template<typename T>
struct Dijkstra {
private:
    int V;
    struct edge { int to; T cost; };
    vector<vector<edge>> G;
public:
    const T inf = numeric_limits<T>::max();
    
    
    vector<T> d; 
    Dijkstra(int V) : V(V) {
        G.resize(V);
    }
    
    
    void add_edge(int from, int to, T weight, bool directed = false) {
        G[from].push_back({to,weight});
        if (!directed) G[to].push_back({from,weight});
    }
    void build(int s) {
        d.assign(V, inf); 
        typedef tuple<T, int> P; 
        priority_queue<P, vector<P>, greater<P>> pq;
        d[s] = 0; 
        pq.push(P(d[s], s)); 
        while (!pq.empty()) {
            P p = pq.top(); pq.pop();
            int v = get<1>(p);
            
            if (d[v] < get<0>(p)) continue; 
            for (const edge &e : G[v])
            {
                
                if (d[e.to] > d[v] + e.cost) {
                    d[e.to] = d[v] + e.cost;
                    pq.push(P(d[e.to], e.to));
                }
            }
        }
    }
};
int main() {
    int n,m,a,b;cin >> n >> m >> a >> b;
    Dijkstra<int> g(n+1);
    for (int i = 0; i < m; i++) {
        int l,r;cin >> l >> r;
        g.add_edge(l-1,r,1);
    }
    for (int i = 0; i < a; i++) {
        g.add_edge(i+1,i,0,true);
    }
    for (int i = b; i < n; i++) {
        g.add_edge(i+1,i,0,true);
    }
    g.build(a-1);
    if (g.d[b] == g.inf) {
        cout << -1 << endl;
    }
    else {
        cout << g.d[b] << endl;
    }
    return 0;
}
0