結果

問題 No.1244 Black Segment
ユーザー どらら
提出日時 2020-10-03 12:58:48
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 84 ms / 2,000 ms
コード長 1,188 bytes
コンパイル時間 1,461 ms
コンパイル使用メモリ 172,908 KB
実行使用メモリ 10,836 KB
最終ジャッジ日時 2024-07-18 04:19:12
合計ジャッジ時間 4,681 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

struct Edge {
  int src, dest;
  int dist;
};
bool operator>(const Edge& a, const Edge& b){
  return a.dist > b.dist;
}

vector<Edge> G[100005];
int dp[100005];

int solve(int s, int t){
  rep(i,100005) dp[i] = -1;

  priority_queue<Edge,vector<Edge>,greater<Edge>> q;
  q.push((Edge){-1,s,0});

  while(!q.empty()){
    Edge e=q.top();
    q.pop();
    if(dp[e.dest] != -1) continue;
    dp[e.dest] = e.dist;
    FOR(it,G[e.dest])
      if(dp[it->dest]==-1) q.push((Edge){it->src,it->dest,e.dist+it->dist});
  }

  return dp[t];
}


int main(){
  int N, M, A, B;
  cin >> N >> M >> A >> B;
  int s = 0;
  int t = 100004;

  rep(i,M){
    int l, r;
    cin >> l >> r;
    r++;
    if(l<=A) l = s;
    if(r<=A) r = s;
    if(l>=B+1) l = t;
    if(r>=B+1) r = t;
    G[l].push_back((Edge){l,r,1});
    G[r].push_back((Edge){r,l,1});
  }
  
  cout << solve(s, t) << endl;
  
  return 0;
}

0