結果

問題 No.1244 Black Segment
ユーザー 👑 tute7627tute7627
提出日時 2020-02-11 12:35:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 130 ms / 2,000 ms
コード長 1,958 bytes
コンパイル時間 2,668 ms
コンパイル使用メモリ 174,732 KB
実行使用メモリ 13,756 KB
最終ジャッジ日時 2024-04-17 02:09:31
合計ジャッジ時間 6,193 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 4 ms
5,376 KB
testcase_02 AC 4 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 4 ms
5,376 KB
testcase_11 AC 4 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 6 ms
5,376 KB
testcase_14 AC 65 ms
9,556 KB
testcase_15 AC 6 ms
5,376 KB
testcase_16 AC 61 ms
8,440 KB
testcase_17 AC 5 ms
5,376 KB
testcase_18 AC 63 ms
9,860 KB
testcase_19 AC 88 ms
11,220 KB
testcase_20 AC 102 ms
11,504 KB
testcase_21 AC 93 ms
11,164 KB
testcase_22 AC 104 ms
11,404 KB
testcase_23 AC 120 ms
11,956 KB
testcase_24 AC 103 ms
11,764 KB
testcase_25 AC 99 ms
11,320 KB
testcase_26 AC 110 ms
11,672 KB
testcase_27 AC 129 ms
12,288 KB
testcase_28 AC 107 ms
11,712 KB
testcase_29 AC 113 ms
12,028 KB
testcase_30 AC 104 ms
11,432 KB
testcase_31 AC 119 ms
12,032 KB
testcase_32 AC 130 ms
12,032 KB
testcase_33 AC 121 ms
12,032 KB
testcase_34 AC 70 ms
11,616 KB
testcase_35 AC 73 ms
10,396 KB
testcase_36 AC 90 ms
11,784 KB
testcase_37 AC 121 ms
13,756 KB
testcase_38 AC 109 ms
13,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0