結果

問題 No.1244 Black Segment
ユーザー auaua
提出日時 2020-10-02 22:59:13
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 164 ms / 2,000 ms
コード長 1,667 bytes
コンパイル時間 1,924 ms
コンパイル使用メモリ 178,636 KB
実行使用メモリ 21,164 KB
最終ジャッジ日時 2024-07-17 23:04:09
合計ジャッジ時間 6,356 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
#define vec vector<ll>
#define mat vector<vector<ll> >
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef long double ld;
typedef complex<double> comp;
const ll INF=1e9+7;
const ll inf=INF;
const ll MOD=998244353;
const ll mod=MOD;
const int MAX=200010;
ll n;
vector<vector<pll> >G(200010);
vector<ll> d(200010);
void dijkstra(ll s,vector<vector<pll> >&G,vector<ll> &d){
    priority_queue<pll,vector<pll>,greater<pll> >pq;
    rep(i,n){
        d[i]=inf*inf;
    }
    d[s]=0;
    pq.push(mp(0,s));
    while(!pq.empty()){
        pll k=pq.top();pq.pop();
        ll v=k.second;
        if(d[v]<k.first)continue;
        for(auto e:G[v]){
            if(d[e.first]>d[v]+e.second){
                d[e.first]=d[v]+e.second;
                pq.push(mp(d[e.first],e.first));
            }
        }
    }
}
signed main(){
    ll n_,m,a,b;cin>>n_>>m>>a>>b;
    n=n_*2+4;
    rep(i,m){
        ll l,r;
        cin>>l>>r;
        if(l<a)G[a].pb(mp(l,0));
        G[l].pb(mp(r+1,1));
        G[l].pb(mp(n_+2+r,1));
        G[r+n_+2].pb(mp(l,1));
        G[r+n_+2].pb(mp(l-1+n_+2,1));
    }
    dijkstra(a,G,d);
    ll ans=inf*inf;
    REP(i,b+1,n_+2){
        ans=min(ans,d[i]);
    }
    rep(i,n_+2){
        //cout<<i<<' '<<d[i]<<endl;
    }
    if(ans>inf){
        cout<<-1<<endl;
    }else{
        cout<<ans<<endl;
    }
}
0