結果

問題 No.2712 Play more!
ユーザー shogo314shogo314
提出日時 2024-03-31 15:17:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 81 ms / 2,000 ms
コード長 2,409 bytes
コンパイル時間 2,111 ms
コンパイル使用メモリ 206,896 KB
実行使用メモリ 6,548 KB
最終ジャッジ日時 2024-04-03 12:10:31
合計ジャッジ時間 3,571 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 1 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 2 ms
6,548 KB
testcase_07 AC 27 ms
6,548 KB
testcase_08 AC 27 ms
6,548 KB
testcase_09 AC 26 ms
6,548 KB
testcase_10 AC 2 ms
6,548 KB
testcase_11 AC 21 ms
6,548 KB
testcase_12 AC 24 ms
6,548 KB
testcase_13 AC 18 ms
6,548 KB
testcase_14 AC 27 ms
6,548 KB
testcase_15 AC 81 ms
6,548 KB
testcase_16 AC 15 ms
6,548 KB
testcase_17 AC 5 ms
6,548 KB
testcase_18 AC 6 ms
6,548 KB
testcase_19 AC 4 ms
6,548 KB
testcase_20 AC 33 ms
6,548 KB
testcase_21 AC 23 ms
6,548 KB
testcase_22 AC 38 ms
6,548 KB
testcase_23 AC 11 ms
6,548 KB
testcase_24 AC 12 ms
6,548 KB
testcase_25 AC 7 ms
6,548 KB
testcase_26 AC 6 ms
6,548 KB
testcase_27 AC 12 ms
6,548 KB
testcase_28 AC 4 ms
6,548 KB
testcase_29 AC 11 ms
6,548 KB
testcase_30 AC 49 ms
6,548 KB
testcase_31 AC 7 ms
6,548 KB
testcase_32 AC 6 ms
6,548 KB
testcase_33 AC 2 ms
6,548 KB
testcase_34 AC 2 ms
6,548 KB
testcase_35 AC 2 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using ll = long long;
constexpr ll INF = (1LL << 61);

// 辺
struct Edge {
    int from;
    int to;
    ll cost;
};

// ベルマンフォード法 (1.2 負閉路の影響を受ける頂点を調べる)
// 負の閉路が存在する場合 true を返し, 負閉路の影響を受ける頂点は -INF にセットされる
// distances は頂点数と同じサイズ, 全要素 INF で初期化しておく
bool BellmanFord(const std::vector<Edge>& edges, std::vector<long long>& distances, int startIndex) {
    distances[startIndex] = 0;

    for (size_t i = 0; i < distances.size(); ++i) {
        bool changed = false;

        // 各辺について
        for (const auto& edge : edges) {
            // (INF + cost) は INF なので処理しない
            if (distances[edge.from] == INF) {
                continue;
            }

            // to までの新しい距離
            const long long d = (distances[edge.from] + edge.cost);

            // d が現在の記録より小さければ更新
            if (d < distances[edge.to]) {
                distances[edge.to] = d;

                changed = true;
            }
        }

        // どの頂点も更新されなかったら終了
        if (!changed) {
            return false;
        }
    }

    // 頂点数分だけさらに繰り返し, 負閉路の影響を受ける頂点に -INF を伝播
    for (size_t i = 0; i < distances.size(); ++i) {
        for (const auto& edge : edges) {
            if (distances[edge.from] == INF) {
                continue;
            }

            const long long d = (distances[edge.from] + edge.cost);

            if (d < distances[edge.to]) {
                // 負閉路の影響を受ける頂点を -INF に
                distances[edge.to] = -INF;
            }
        }
    }

    return true;
}

int main() {
    ll N, M;
    std::cin >> N >> M;
    std::vector<ll> A(N);
    for (ll& a : A) std::cin >> a;
    std::vector<Edge> edges;
    for (int i = 0; i < M; i++) {
        int a, b;
        ll c;
        std::cin >> a >> b >> c;
        edges.push_back(Edge{a - 1, b - 1, c - A[b - 1]});
    }
    std::vector<ll> distances(N, INF);
    BellmanFord(edges, distances, 0);
    if (distances[N - 1] == -INF) {
        std::cout << "inf" << std::endl;
    } else {
        std::cout << A[0] - distances[N - 1] << std::endl;
    }
}
0