結果

問題 No.1244 Black Segment
ユーザー FukucchiFukucchi
提出日時 2020-10-04 00:16:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 79 ms / 2,000 ms
コード長 6,626 bytes
コンパイル時間 1,952 ms
コンパイル使用メモリ 138,068 KB
実行使用メモリ 13,608 KB
最終ジャッジ日時 2023-09-25 12:30:47
合計ジャッジ時間 5,492 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 30 ms
8,192 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 29 ms
7,796 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 42 ms
10,040 KB
testcase_19 AC 57 ms
11,044 KB
testcase_20 AC 71 ms
11,520 KB
testcase_21 AC 53 ms
11,744 KB
testcase_22 AC 68 ms
11,432 KB
testcase_23 AC 79 ms
12,472 KB
testcase_24 AC 70 ms
12,100 KB
testcase_25 AC 68 ms
11,804 KB
testcase_26 AC 68 ms
12,168 KB
testcase_27 AC 72 ms
12,280 KB
testcase_28 AC 72 ms
12,696 KB
testcase_29 AC 65 ms
12,260 KB
testcase_30 AC 67 ms
12,356 KB
testcase_31 AC 66 ms
12,252 KB
testcase_32 AC 70 ms
12,292 KB
testcase_33 AC 68 ms
12,264 KB
testcase_34 AC 56 ms
13,036 KB
testcase_35 AC 55 ms
13,060 KB
testcase_36 AC 65 ms
13,156 KB
testcase_37 AC 62 ms
13,608 KB
testcase_38 AC 55 ms
13,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3") //コンパイラ最適化用

// #define _GLIBCXX_DEBUG //配列に[]でアクセス時のエラー表示
#include <algorithm> //sort,二分探索,など
#include <bitset>    //固定長bit集合
#include <cassert>   //assert(p)で,not pのときにエラー
#include <cctype>
#include <chrono> //実行時間計測
#include <climits>
#include <cmath>   //pow,logなど
#include <complex> //複素数
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional> //sortのgreater
#include <iomanip>    //setprecision(浮動小数点の出力の誤差)
#include <ios>        // std::left, std::right
#include <iostream>   //入出力
#include <iterator>   //集合演算(積集合,和集合,差集合など)
#include <map>
#include <numeric> //iota(整数列の生成),gcdとlcm,accumulate
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility> //pair
#include <vector>
using namespace std;
typedef long long LL;
typedef long double LD;

#define ALL(x) x.begin(), x.end()
const long long INF = 1e12;
const int MOD = 1e9 + 7;
// const int MOD=998244353;
//略記
#define UMAP unordered_map
#define USET unordered_set
#define FF first
#define SS second
#define int long long
#define LD long double
#define PII pair<int, int>
#define PB push_back
#define MP make_pair
#define SZ(x) (int)((x).size())
#define VI vector<int>
#define VVI vector<vector<int>>
#define VLL vector<LL>
#define VVLL vector<vector<LL>>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPD(i, n) for (int i = (int)(n) - (int)1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < (int)(b); i++)
#define FORD(i, a, b) for (int i = (int)(b) - (int)1; i >= (int)a; i--)

const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1},
          Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};

int in() {
    int x;
    cin >> x;
    return x;
}
// https://qiita.com/Lily0727K/items/06cb1d6da8a436369eed#c%E3%81%A7%E3%81%AE%E5%AE%9F%E8%A3%85
void print() { cout << "\n"; }

template <class Head, class... Tail> void print(Head &&head, Tail &&... tail) {
    cout << head;
    if (sizeof...(tail) != 0)
        cout << " ";
    print(forward<Tail>(tail)...);
}

template <class T> void print(vector<T> &vec) {
    for (auto &a : vec) {
        cout << a;
        if (&a != &vec.back())
            cout << " ";
    }
    cout << "\n";
}

template <class T> void print(set<T> &set) {
    for (auto &a : set) {
        cout << a << " ";
    }
    cout << "\n";
}

template <class T> void print(vector<vector<T>> &df) {
    for (auto &vec : df) {
        print(vec);
    }
}
long long power(long long x, long long n) {
    // O(logn)
    // https://algo-logic.info/calc-pow/#toc_id_1_2
    long long ret = 1;
    while (n > 0) {
        if (n & 1)
            ret *= x; // n の最下位bitが 1 ならば x^(2^i) をかける
        x *= x;
        n >>= 1; // n を1bit 左にずらす
    }
    return ret;
}
long long comb(int n, int k) {
    vector<vector<long long>> v(n + 1, vector<long long>(n + 1, 0));

    for (int i = 0; i < SZ(v); i++) {
        v[i][0] = 1;
        v[i][i] = 1;
    }
    for (int k = 1; k < SZ(v); k++) {
        for (int j = 1; j < k; j++) {
            v[k][j] = (v[k - 1][j - 1] + v[k - 1][j]);
        }
    }
    return v[n][k];
}
void add(long long &a, long long b) {
    a += b;
    if (a >= MOD)
        a -= MOD;
}
template <class T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

__attribute__((constructor)) void faster_io() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
}

int n, m, a, b;
/*
    ダイクストラ法で、ある頂点から各頂点への最短距離を求めるアルゴリズム
    https://algo-logic.info/dijkstra/
    https://atcoder.jp/contests/joi2008yo/submissions/10576774

*/

struct Edge {
    long long to;
    long long cost;
};
using Graph = vector<vector<Edge>>;

/* dijkstra(G,s,dis,prev)
    入力:グラフ G, 開始点 s, 距離を格納する dis, 最短経路の前の点を記録するprev
    計算量:O(|E|log|V|)
    副作用:dis, prevが書き換えられる
*/
void dijkstra(const Graph &G, int s, vector<long long> &dis,
              vector<int> &prev) {
    int N = G.size();
    dis.resize(N, INF);
    prev.resize(N, -1);
    priority_queue<PII, vector<PII>, greater<PII>>
        pq; // the least element is top. first:cost, second: node
    dis[s] = 0;
    pq.emplace(dis[s], s);
    while (!pq.empty()) {
        PII p = pq.top(); // the least element
        pq.pop();
        int v = p.second;
        if (dis[v] < p.first) {
            continue;
        }
        for (auto &e : G[v]) {
            if (dis[e.to] > dis[v] + e.cost) {
                dis[e.to] = dis[v] + e.cost;
                prev[e.to] = v; // vからtoに来たこと記録
                pq.emplace(dis[e.to], e.to);
            }
            // if
            // (dis[e.to]==dis[v]+1)として,今見ている頂点が最短経路上にあるか判定できる
        }
    }
}

/* get_path(prev, t)
    入力:dijkstra で得た prev, ゴール t
    出力: t への最短路のパス
*/
vector<int> get_path(const vector<int> &prev, int t) {
    vector<int> path;
    for (int cur = t; cur != -1; cur = prev[cur]) {
        path.push_back(cur);
    }
    reverse(path.begin(), path.end());
    return path;
}

signed main() {
    cin >> n >> m >> a >> b;
    a--;

    Graph G(n + 1);
    for (int i = 0; i < m; i++) {
        int s, t, d = 1;
        cin >> s >> t;
        s--;
        if (s <= a and b <= t) {
            // print("a", a, b);
            G[a].push_back({b, d});
            G[b].push_back({a, d});

        } else if (s <= a) {
            // print("b", a, t);

            G[a].push_back({t, d});
            G[t].push_back({a, d});

        } else if (b <= t) {
            // print("c", s, b);

            G[s].push_back({b, d});
            G[b].push_back({s, d});

        } else {
            // print("d", s, t);

            G[s].push_back({t, d});
            G[t].push_back({s, d});
        }
    }
    /* print(G);
    exit(0); */

    int ans = INF;
    vector<long long> dis;
    vector<int> prev;

    dijkstra(G, a, dis, prev);

    if (dis[b] == INF) {
        print(-1);
    } else {
        print(dis[b]);
    }

    // print(ans);
}
0