結果
| 問題 |
No.1244 Black Segment
|
| コンテスト | |
| ユーザー |
tute7627
|
| 提出日時 | 2020-02-11 12:35:33 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 87 ms / 2,000 ms |
| コード長 | 1,958 bytes |
| コンパイル時間 | 2,129 ms |
| コンパイル使用メモリ | 180,596 KB |
| 実行使用メモリ | 13,628 KB |
| 最終ジャッジ日時 | 2024-10-08 11:14:43 |
| 合計ジャッジ時間 | 4,921 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 36 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
#define ALL(a) (a).begin(),(a).end()
#define ALLR(a) (a).rbegin(),(a).rend()
#define rep(i,n,m) for(int i = (n); i < (int)(m); i++)
#define rrep(i,n,m) for(int i = (m) - 1; i >= (int)(n); i--)
using ll = long long;
using ld = long double;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const ll INF = 1e9+1;
template<typename T>
bool chmin(T &a,T b){if(a>b){a=b;return true;}else return false;}
template<typename T>
bool chmax(T &a,T b){if(a<b){a=b;return true;}else return false;}
template<typename T>
vector<ll>dx={1,0,-1,0,1,1,-1,-1};
vector<ll>dy={0,1,0,-1,1,-1,1,-1};
template<typename T>
vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
template<typename... Ts>
auto make_v(size_t a,Ts... ts){
return vector<decltype(make_v(ts...))>(a,make_v(ts...));
}
ll MAX_V = 2e5 + 5;
struct edge {
ll to,cost;
edge(ll x,ll y):to(x),cost(y){};
};
//<最短距離, 頂点の番号>
using P = pair<ll, ll>;
vector<ll> dist(MAX_V); //各頂点への最短距離
void dijkstra(ll s,vector<vector<edge>>&g) {
priority_queue<P, vector<P>, greater<P> > que;
fill(dist.begin(),dist.begin()+g.size(), INF);
dist[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top();
que.pop();
ll v = p.second;
if (dist[v] < p.first) continue;
for (ll i=0; i<g[v].size(); i++) {
struct edge e = g[v][i];
if (dist[e.to] > dist[v] + e.cost) {
dist[e.to] = dist[v] + e.cost;
que.push(P(dist[e.to], e.to));
}
}
}
}
int main(){
int n,m,a,b;cin>>n>>m>>a>>b;
a--;b--;
vector<vector<edge>>g(n+1);
rep(i,0,m){
int l,r;
cin>>l>>r;
l--;
chmax(l,a);
chmin(l,b+1);
chmax(r,a);
chmin(r,b+1);
g[l].emplace_back(r,1);
g[r].emplace_back(l,1);
}
dijkstra(a,g);
if(dist[b+1]!=INF)cout<<dist[b+1]<<endl;
else cout<<-1<<endl;
return 0;
}
tute7627