結果
| 問題 |
No.1244 Black Segment
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-10-02 22:47:20 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 123 ms / 2,000 ms |
| コード長 | 2,140 bytes |
| コンパイル時間 | 2,200 ms |
| コンパイル使用メモリ | 206,876 KB |
| 最終ジャッジ日時 | 2025-01-15 00:55:34 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 36 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const double pi = acos(-1.0);
const double EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};
template<typename T>
struct Weighted_Graph{
struct edge{
int to; T cost;
edge(int to, T cost) : to(to), cost(cost) {}
};
vector<vector<edge>> es;
const T INF_T;
const int n;
Weighted_Graph(int n) : INF_T(numeric_limits<T>::max()), n(n){
es.resize(n);
}
void add_edge(int from, int to, T cost, bool directed = false){
es[from].eb(to, cost);
if(!directed) es[to].eb(from, cost);
}
T dijkstra(int s, int t){
vector<T> d(n, INF_T);
using P = pair<T, int>;
priority_queue<P, vector<P>, greater<P> > que;
que.emplace(d[s] = 0, s);
while(!que.empty()){
T p; int i;
tie(p, i) = que.top(); que.pop();
if(p > d[i]) continue;
for(auto &e: es[i]){
if(chmin(d[e.to], d[i]+e.cost)) que.emplace(d[e.to], e.to);
}
}
return (d[t] == INF_T? -1 : d[t]);
}
};
int main(){
int N, M, A, B;
cin >> N >> M >> A >> B;
Weighted_Graph<int> G(N+3);
int s = N+1, t = N+2;
rep2(i, 0, A-1) G.add_edge(s, i, 0, true);
rep2(i, B, N) G.add_edge(i, t, 0, true);
rep(i, M){
int l, r; cin >> l >> r; l--;
G.add_edge(l, r, 1);
}
cout << G.dijkstra(s, t) << endl;
}