結果
問題 |
No.1244 Black Segment
|
ユーザー |
![]() |
提出日時 | 2024-01-30 04:28:55 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 127 ms / 2,000 ms |
コード長 | 1,230 bytes |
コンパイル時間 | 2,252 ms |
コンパイル使用メモリ | 204,352 KB |
最終ジャッジ日時 | 2025-02-19 00:44:54 |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 36 |
ソースコード
#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; }