結果

問題 No.92 逃走経路
ユーザー PachicobuePachicobue
提出日時 2017-08-04 04:05:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 18 ms / 5,000 ms
コード長 2,547 bytes
コンパイル時間 1,591 ms
コンパイル使用メモリ 177,444 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 01:51:11
合計ジャッジ時間 2,401 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 18 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 5 ms
5,376 KB
testcase_17 AC 5 ms
5,376 KB
testcase_18 AC 4 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// {{{ Templates
#include <bits/stdc++.h>

#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz:" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


constexpr ll MOD = (ll)1e9 + 7LL;

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 100;

// }}}

struct Edge {
    Edge() = default;
    Edge(int from, int to, ll cost) : from{from}, to{to}, cost{cost} {}
    int from;
    int to;
    ll cost;
    bool operator<(const Edge& e) const
    {
        return cost < e.cost;
    }
};

struct Graph {
    Graph(int n)
    {
        edge.resize(n);
    }
    void addEdge(int from, int to, ll cost)
    {
        edge[from].push_back(Edge{from, to, cost});
        edge[to].push_back(Edge{to, from, cost});
    }

    vector<vector<Edge>> edge;
};

int N, M, K;

bool dfs(const Graph& g, const int pos, const int tim, const vector<ll>& d)
{
    if (tim == K) {
        return true;
    }
    const ll cost = d[tim];
    if (g.edge[pos].empty()) {
        return false;
    }
    auto it = lower_bound(g.edge[pos].begin(), g.edge[pos].end(), Edge{0, 0, cost});
    if (it == g.edge[pos].end()) {
        return false;
    } else {
        for (; it != g.edge[pos].end() and it->cost == cost; it++) {
            if (dfs(g, it->to, tim + 1, d)) {
                return true;
            }
        }
        return false;
    }
}

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

    cin >> N >> M >> K;
    Graph g(N);
    for (int i = 0; i < M; i++) {
        int a, b;
        ll c;
        cin >> a >> b >> c;
        a--, b--;
        g.addEdge(a, b, c);
    }
    for (int i = 0; i < N; i++) {
        sort(g.edge[i].begin(), g.edge[i].end());
    }

    vector<ll> d(K);
    for (int i = 0; i < K; i++) {
        cin >> d[i];
    }
    reverse(d.begin(), d.end());

    vector<int> ans;
    for (int i = 0; i < N; i++) {
        if (dfs(g, i, 0, d)) {
            ans.push_back(i + 1);
        }
    }
    cout << ans.size() << endl;
    for (int i = 0; i < ans.size(); i++) {
        if (i != 0) {
            cout << " ";
        }
        cout << ans[i];
    }
    cout << endl;

    return 0;
}
0