結果

問題 No.1244 Black Segment
ユーザー umezoumezo
提出日時 2024-01-30 04:28:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 80 ms / 2,000 ms
コード長 1,230 bytes
コンパイル時間 2,757 ms
コンパイル使用メモリ 213,004 KB
実行使用メモリ 16,024 KB
最終ジャッジ日時 2024-01-30 04:29:02
合計ジャッジ時間 7,354 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
9,472 KB
testcase_01 AC 10 ms
9,472 KB
testcase_02 AC 11 ms
9,472 KB
testcase_03 AC 10 ms
9,472 KB
testcase_04 AC 11 ms
9,472 KB
testcase_05 AC 11 ms
9,472 KB
testcase_06 AC 10 ms
9,472 KB
testcase_07 AC 11 ms
9,472 KB
testcase_08 AC 11 ms
9,472 KB
testcase_09 AC 10 ms
9,472 KB
testcase_10 AC 11 ms
9,472 KB
testcase_11 AC 11 ms
9,472 KB
testcase_12 AC 11 ms
9,472 KB
testcase_13 AC 11 ms
9,600 KB
testcase_14 AC 35 ms
13,952 KB
testcase_15 AC 12 ms
9,600 KB
testcase_16 AC 36 ms
14,080 KB
testcase_17 AC 11 ms
9,600 KB
testcase_18 AC 39 ms
10,752 KB
testcase_19 AC 56 ms
13,388 KB
testcase_20 AC 66 ms
12,796 KB
testcase_21 AC 45 ms
12,288 KB
testcase_22 AC 59 ms
12,676 KB
testcase_23 AC 65 ms
12,416 KB
testcase_24 AC 57 ms
13,040 KB
testcase_25 AC 58 ms
13,120 KB
testcase_26 AC 52 ms
11,776 KB
testcase_27 AC 49 ms
11,648 KB
testcase_28 AC 54 ms
12,940 KB
testcase_29 AC 61 ms
12,032 KB
testcase_30 AC 80 ms
12,928 KB
testcase_31 AC 50 ms
12,416 KB
testcase_32 AC 54 ms
12,288 KB
testcase_33 AC 56 ms
12,416 KB
testcase_34 AC 76 ms
14,592 KB
testcase_35 AC 59 ms
16,024 KB
testcase_36 AC 71 ms
15,128 KB
testcase_37 AC 67 ms
14,832 KB
testcase_38 AC 67 ms
13,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

#include <bits/stdc++.h>
using namespace std;

const ll INF=1LL<<60;

struct Edge{
  int to;
  ll w;
  Edge(int to,ll w) : to(to),w(w) {}
};

using Graph=vector<vector<Edge>>;
using pli=pair<ll,int>;

template<class T> bool chmin(T& a,T b){
  if(a>b){
    a=b;
    return true;
  }
  return false;
}

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int n,m,a,b;
  cin>>n>>m>>a>>b;
  
  Graph G(100100);
  rep(i,m){
    int l,r;
    cin>>l>>r;
    G[l].push_back(Edge(r+1,1));
    G[r+1].push_back(Edge(l,1));
  }
  
  vector<ll> dist(100100,INF);
  int s=0,t=100010;
  dist[s]=0;
  for(int i=1;i<=a;i++) G[s].push_back(Edge(i,0));
  for(int i=b+1;i<=100001;i++) G[i].push_back(Edge(t,0));
  
  priority_queue<pli,vector<pli>,greater<pli>> que;
  que.push({dist[s],s});
  
  while(!que.empty()){
    int v=que.top().second;
    ll d=que.top().first;
    que.pop();
    
    if(d>dist[v]) continue;
    
    for(auto e:G[v]){
      if(chmin(dist[e.to],dist[v]+e.w)){
        que.push({dist[e.to],e.to});
      }
    }
  }
  if(dist[t]==INF) cout<<-1<<endl;
  else cout<<dist[t]<<endl;

  return 0;
}
0