結果

問題 No.1244 Black Segment
ユーザー rniya
提出日時 2020-10-02 22:21:49
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 740 bytes
コンパイル時間 1,462 ms
コンパイル使用メモリ 176,076 KB
実行使用メモリ 9,216 KB
最終ジャッジ日時 2024-07-17 21:45:01
合計ジャッジ時間 3,512 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF=1e9;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N,M,A,B; cin >> N >> M >> A >> B; --A,B;
    vector<vector<int>> G(N+1);
    for (int i=0;i<M;++i){
        int L,R; cin >> L >> R; --L;
        L=max(L,A); L=min(L,B);
        R=max(R,A); R=min(R,B);
        G[L].emplace_back(R);
        G[R].emplace_back(L);
    }

    queue<int> que;
    vector<int> dp(N+2,INF);
    dp[A]=0; que.emplace(A);
    while(!que.empty()){
        int v=que.front(); que.pop();
        for (int u:G[v]){
            if (dp[v]+1<dp[u]){
                dp[u]=dp[v]+1;
                que.emplace(u);
            }
        }
    }

    cout << (dp[B]==INF?-1:dp[B]) << '\n';
}
0