結果

問題 No.1244 Black Segment
ユーザー ShibuyapShibuyap
提出日時 2021-02-06 07:12:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 111 ms / 2,000 ms
コード長 1,669 bytes
コンパイル時間 2,403 ms
コンパイル使用メモリ 206,556 KB
実行使用メモリ 14,140 KB
最終ジャッジ日時 2023-09-15 11:44:52
合計ジャッジ時間 6,057 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
8,860 KB
testcase_01 AC 5 ms
8,792 KB
testcase_02 AC 5 ms
8,936 KB
testcase_03 AC 5 ms
8,996 KB
testcase_04 AC 5 ms
9,020 KB
testcase_05 AC 5 ms
8,924 KB
testcase_06 AC 5 ms
8,936 KB
testcase_07 AC 5 ms
8,760 KB
testcase_08 AC 5 ms
8,924 KB
testcase_09 AC 5 ms
8,760 KB
testcase_10 AC 4 ms
8,784 KB
testcase_11 AC 5 ms
8,828 KB
testcase_12 AC 4 ms
8,820 KB
testcase_13 AC 7 ms
8,912 KB
testcase_14 AC 55 ms
11,436 KB
testcase_15 AC 7 ms
8,960 KB
testcase_16 AC 55 ms
11,644 KB
testcase_17 AC 7 ms
9,212 KB
testcase_18 AC 57 ms
11,520 KB
testcase_19 AC 82 ms
12,348 KB
testcase_20 AC 89 ms
12,536 KB
testcase_21 AC 74 ms
12,428 KB
testcase_22 AC 90 ms
12,512 KB
testcase_23 AC 90 ms
12,792 KB
testcase_24 AC 91 ms
12,848 KB
testcase_25 AC 88 ms
12,824 KB
testcase_26 AC 85 ms
12,424 KB
testcase_27 AC 83 ms
11,960 KB
testcase_28 AC 91 ms
12,616 KB
testcase_29 AC 86 ms
12,652 KB
testcase_30 AC 94 ms
13,036 KB
testcase_31 AC 87 ms
12,284 KB
testcase_32 AC 88 ms
12,352 KB
testcase_33 AC 91 ms
12,684 KB
testcase_34 AC 111 ms
14,104 KB
testcase_35 AC 92 ms
14,140 KB
testcase_36 AC 92 ms
13,512 KB
testcase_37 AC 96 ms
13,984 KB
testcase_38 AC 91 ms
13,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}
struct edge{ int to, cost; };
const int INF = 1001001001;
const int MAX_N = 200005;
const int MAX_V = 200005;
vector<edge> G[MAX_V];
int d[MAX_V];

void dijkstra(int s){
    // greater<P>を指定することでfirstが小さい順に取り出せるようにする
    priority_queue<P, vector<P>, greater<P> > que;
    fill(d, d+MAX_V, INF);
    d[s] = 0;
    que.push(P(0, s));

    while(!que.empty()){
        P p = que.top();
        que.pop();
        int v = p.second;
        if(p.first > d[v]) continue;
        for(int i=0; i<G[v].size(); i++){
            edge e = G[v][i];
            if(d[e.to] > d[v] + e.cost){
                d[e.to] = d[v] + e.cost;
                que.push(P(d[e.to], e.to));
            }
        }
    }
}

int main() {
    int n, m, a, b;
    cin >> n >> m >> a >> b;

    srep(i,1,a+1){
        edge e;
        e.to = i;
        e.cost = 0;
        G[0].push_back(e);
    }
    srep(i,b+1,n+2){
        edge e;
        e.to = n+2;
        e.cost = 0;
        G[i].push_back(e);
    }

    rep(i,m){
        int from,to,cost;
        cin >> from >> to;
        cost = 1;
        edge e1, e2;
        e1.to = to+1; e1.cost = cost;
        e2.to = from; e2.cost = cost;
        G[from].push_back(e1);
        G[to+1].push_back(e2);
    }

    dijkstra(0);
    int ans = d[n+2];
    if(ans >= INF) ans = -1;
    cout << ans << endl;
    return 0;
}

0