結果

問題 No.1244 Black Segment
ユーザー MisterMister
提出日時 2020-11-04 06:06:42
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 1,849 bytes
コンパイル時間 795 ms
コンパイル使用メモリ 85,944 KB
実行使用メモリ 11,112 KB
最終ジャッジ日時 2023-09-29 15:30:57
合計ジャッジ時間 4,960 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 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 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 23 ms
7,160 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 24 ms
7,232 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 25 ms
7,800 KB
testcase_19 AC 38 ms
9,124 KB
testcase_20 AC 39 ms
9,528 KB
testcase_21 AC 36 ms
9,544 KB
testcase_22 AC 39 ms
9,752 KB
testcase_23 AC 39 ms
9,468 KB
testcase_24 AC 43 ms
9,916 KB
testcase_25 AC 39 ms
9,848 KB
testcase_26 AC 38 ms
9,588 KB
testcase_27 AC 41 ms
8,804 KB
testcase_28 AC 40 ms
10,332 KB
testcase_29 AC 41 ms
9,848 KB
testcase_30 AC 44 ms
10,112 KB
testcase_31 AC 41 ms
9,516 KB
testcase_32 AC 44 ms
9,388 KB
testcase_33 AC 42 ms
9,320 KB
testcase_34 AC 43 ms
10,120 KB
testcase_35 AC 38 ms
9,836 KB
testcase_36 AC 40 ms
9,472 KB
testcase_37 AC 41 ms
11,112 KB
testcase_38 AC 42 ms
10,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <numeric>
#include <vector>
#include <queue>

template <class Cost = int>
struct Edge {
    int src, dst;
    Cost cost;

    Edge() = default;
    Edge(int src, int dst, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return cost > e.cost; }
};

template <class Cost = int>
struct Graph : public std::vector<std::vector<Edge<Cost>>> {
    Graph(int n = 0) : std::vector<std::vector<Edge<Cost>>>(n) {}

    void span(bool direct, int src, int dst, Cost cost = 1) {
        (*this)[src].emplace_back(src, dst, cost);
        if (!direct) (*this)[dst].emplace_back(dst, src, cost);
    }
};

template <class Cost>
std::vector<Cost> bfs(const Graph<Cost>& graph, std::vector<int> ss) {
    std::vector<Cost> dist(graph.size(), -1);
    std::queue<int> que;
    for (auto s : ss) {
        dist[s] = 0;
        que.push(s);
    }

    while (!que.empty()) {
        auto v = que.front();
        que.pop();

        for (const auto& e : graph[v]) {
            if (dist[e.dst] != -1) continue;
            dist[e.dst] = dist[v] + e.cost;
            que.push(e.dst);
        }
    }

    return dist;
}

void solve() {
    int n, m, s, t;
    std::cin >> n >> m >> s >> t;

    Graph<> graph(n + 1);
    while (m--) {
        int l, r;
        std::cin >> l >> r;
        graph.span(false, --l, r);
    }

    std::vector<int> ss(s);
    std::iota(ss.begin(), ss.end(), 0);

    auto ds = bfs(graph, ss);
    int ans = -1;
    for (int v = t; v <= n; ++v) {
        if (ds[v] == -1) continue;
        if (ans == -1 || ds[v] < ans) ans = ds[v];
    }
    std::cout << ans << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0