結果

問題 No.1190 Points
ユーザー MisterMister
提出日時 2020-08-22 14:17:44
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 157 ms / 2,000 ms
コード長 2,500 bytes
コンパイル時間 914 ms
コンパイル使用メモリ 86,696 KB
実行使用メモリ 18,364 KB
最終ジャッジ日時 2023-08-05 11:27:05
合計ジャッジ時間 5,583 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 87 ms
13,956 KB
testcase_04 AC 69 ms
12,808 KB
testcase_05 AC 71 ms
11,688 KB
testcase_06 AC 111 ms
16,456 KB
testcase_07 AC 132 ms
17,624 KB
testcase_08 AC 106 ms
16,184 KB
testcase_09 AC 114 ms
16,268 KB
testcase_10 AC 108 ms
16,188 KB
testcase_11 AC 80 ms
12,956 KB
testcase_12 AC 110 ms
16,300 KB
testcase_13 AC 90 ms
12,448 KB
testcase_14 AC 21 ms
9,796 KB
testcase_15 AC 144 ms
17,800 KB
testcase_16 AC 13 ms
5,740 KB
testcase_17 AC 127 ms
16,836 KB
testcase_18 AC 54 ms
10,848 KB
testcase_19 AC 18 ms
10,780 KB
testcase_20 AC 75 ms
12,260 KB
testcase_21 AC 36 ms
9,068 KB
testcase_22 AC 47 ms
12,968 KB
testcase_23 AC 151 ms
18,084 KB
testcase_24 AC 157 ms
18,364 KB
testcase_25 AC 116 ms
17,456 KB
testcase_26 AC 94 ms
17,496 KB
testcase_27 AC 122 ms
17,572 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