結果

問題 No.1190 Points
ユーザー MisterMister
提出日時 2020-08-22 14:17:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 107 ms / 2,000 ms
コード長 2,500 bytes
コンパイル時間 846 ms
コンパイル使用メモリ 88,688 KB
実行使用メモリ 18,328 KB
最終ジャッジ日時 2024-04-23 08:38:06
合計ジャッジ時間 3,808 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 56 ms
14,080 KB
testcase_04 AC 47 ms
12,672 KB
testcase_05 AC 42 ms
11,896 KB
testcase_06 AC 74 ms
16,532 KB
testcase_07 AC 79 ms
17,796 KB
testcase_08 AC 73 ms
16,320 KB
testcase_09 AC 80 ms
16,324 KB
testcase_10 AC 74 ms
16,472 KB
testcase_11 AC 60 ms
13,144 KB
testcase_12 AC 78 ms
16,408 KB
testcase_13 AC 61 ms
12,544 KB
testcase_14 AC 18 ms
9,988 KB
testcase_15 AC 94 ms
18,016 KB
testcase_16 AC 12 ms
6,944 KB
testcase_17 AC 86 ms
16,876 KB
testcase_18 AC 47 ms
11,008 KB
testcase_19 AC 14 ms
10,988 KB
testcase_20 AC 54 ms
12,288 KB
testcase_21 AC 26 ms
9,344 KB
testcase_22 AC 30 ms
13,316 KB
testcase_23 AC 102 ms
18,216 KB
testcase_24 AC 107 ms
18,328 KB
testcase_25 AC 74 ms
17,836 KB
testcase_26 AC 55 ms
17,616 KB
testcase_27 AC 72 ms
17,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <class Cost = int>
struct Edge {
    int src, dst;
    Cost cost;
    Edge(int src = -1, int dst = -1, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

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

template <class Cost = int>
struct Graph {
    std::vector<std::vector<Edge<Cost>>> graph;

    Graph(int n = 0) : graph(n) {}

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

    int size() const { return graph.size(); }
    void clear() { graph.clear(); }
    void resize(int n) { graph.resize(n); }

    std::vector<Edge<Cost>>& operator[](int v) { return graph[v]; }
    std::vector<Edge<Cost>> operator[](int v) const { return graph[v]; }
};

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

    while (!que.empty()) {
        int 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, k, s, g;
    std::cin >> n >> m >> k >> s >> g;
    --s, --g;

    Graph<> graph(n * 2);
    auto enc = [&](int v, int t) { return v + t * n; };

    while (m--) {
        int u, v;
        std::cin >> u >> v;
        --u, --v;

        for (int t = 0; t < 2; ++t) {
            graph.span(false, enc(u, t), enc(v, 1 - t));
        }
    }

    auto sdist = bfs(graph, enc(s, 0));
    auto gdist = bfs(graph, enc(g, 0));

    std::vector<int> ans;
    for (int v = 0; v < n; ++v) {
        for (int t = 0; t < 2; ++t) {
            int nt = (t + k) % 2;
            if (sdist[enc(v, t)] != -1 && gdist[enc(v, nt)] != -1 &&
                sdist[enc(v, t)] + gdist[enc(v, nt)] <= k) {
                ans.push_back(v);
                break;
            }
        }
    }

    if (ans.empty()) {
        std::cout << "-1\n";
        return;
    }

    std::cout << ans.size() << "\n";
    for (auto v : ans) std::cout << v + 1 << "\n";
}

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

    solve();

    return 0;
}
0