結果
| 問題 | 
                            No.1244 Black Segment
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2020-10-03 01:00:14 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 49 ms / 2,000 ms | 
| コード長 | 944 bytes | 
| コンパイル時間 | 1,706 ms | 
| コンパイル使用メモリ | 175,356 KB | 
| 実行使用メモリ | 9,344 KB | 
| 最終ジャッジ日時 | 2024-07-18 01:54:36 | 
| 合計ジャッジ時間 | 4,239 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 36 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr char newl = '\n';
int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, m, a, b;
    cin >> n >> m >> a >> b;
    --a;
    vector< vector<int> > g(n + 1);
    for (int i = 0; i < m; i++) {
        int l, r;
        cin >> l >> r;
        --l;
        g[l].push_back(r);
        g[r].push_back(l);
    }
    queue<int> q;
    vector<int> d(n + 1, -1);
    for (int i = 0; i <= a; i++) {
        d[i] = 0;
        q.push(i);
    }
    while (!q.empty()) {
        int cur = q.front();
        q.pop();
        for (int nex : g[cur]) {
            if (d[nex] != -1) continue;
            d[nex] = d[cur] + 1;
            q.push(nex);
        }
    }
    int ans = 1e9;
    for (int i = b; i <= n; i++) {
        ans = min(ans, (d[i] == -1 ? 1000000000 : d[i]));
    }
    cout << (ans >= 1000000000 ? -1 : ans) << newl;
    return 0;
}