結果

問題 No.2712 Play more!
ユーザー blue_jamblue_jam
提出日時 2024-03-31 23:53:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 106 ms / 2,000 ms
コード長 2,370 bytes
コンパイル時間 4,887 ms
コンパイル使用メモリ 269,156 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-04-03 12:11:44
合計ジャッジ時間 6,170 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 40 ms
6,676 KB
testcase_08 AC 41 ms
6,676 KB
testcase_09 AC 41 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 15 ms
6,676 KB
testcase_12 AC 63 ms
6,676 KB
testcase_13 AC 20 ms
6,676 KB
testcase_14 AC 59 ms
6,676 KB
testcase_15 AC 106 ms
6,676 KB
testcase_16 AC 13 ms
6,676 KB
testcase_17 AC 5 ms
6,676 KB
testcase_18 AC 8 ms
6,676 KB
testcase_19 AC 5 ms
6,676 KB
testcase_20 AC 30 ms
6,676 KB
testcase_21 AC 21 ms
6,676 KB
testcase_22 AC 60 ms
6,676 KB
testcase_23 AC 9 ms
6,676 KB
testcase_24 AC 11 ms
6,676 KB
testcase_25 AC 6 ms
6,676 KB
testcase_26 AC 9 ms
6,676 KB
testcase_27 AC 46 ms
6,676 KB
testcase_28 AC 45 ms
6,676 KB
testcase_29 AC 14 ms
6,676 KB
testcase_30 AC 90 ms
6,676 KB
testcase_31 AC 51 ms
6,676 KB
testcase_32 AC 43 ms
6,676 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 2 ms
6,676 KB
testcase_35 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>

using namespace std;
using namespace atcoder;

using ll = long long;

template<typename T>
struct Edge {
    ll from, to;
    T weight;

    Edge() : from(-1), to(-1) {}

    Edge(ll from, ll to, T weight) :
            from(from), to(to), weight(weight) {}
};

template<typename T>
struct Graph {
    ll n;
    vector<vector<Edge<T>>> edges;

    Graph(ll n) : n(n), edges(n) {}

    void add(ll from, ll to, T weight) {
        edges[from].emplace_back(from, to, weight);
    }
};

template<typename T, T (*add)(T, T), bool (*lt)(T, T), T (*zero)(), T (*inf)()>
struct BellmanFord {
    Graph<T> g;
    vector<T> dist;
    vector<ll> prev;
    bool hasNegativeLoop;

    BellmanFord(Graph<T> g) : g(g), dist(g.n, inf()), prev(g.n, -1), hasNegativeLoop(false) {}

    bool run(int s) {
        const T INF = inf();
        const auto eq = [](T a, T b) { return !lt(a, b) && !lt(b, a); };

        int n = g.n;
        dist[s] = zero();

        for (int k = 0; k < 2 * n; ++k) {
            for (int v = 0; v < n; ++v) {
                for (auto &e: g.edges[v]) {
                    if (!eq(dist[e.from], INF) && lt(add(dist[e.from], e.weight), dist[e.to])) {
                        dist[e.to] = add(dist[e.from], e.weight);
                        prev[e.to] = e.from;
                        if (k >= n - 1) {
                            dist[e.to] = -INF;
                            hasNegativeLoop = true;
                        }
                    }
                }
            }
        }
        return hasNegativeLoop;
    }
};

auto add = [](ll a, ll b) { return a + b; };
auto lt = [](ll a, ll b) { return a < b; };
auto zero = []() { return (ll) 0; };
auto inf = []() { return (ll) 1e17; };

void solveE() {
    ll N, M;
    cin >> N >> M;
    vector<ll> A(N);
    for (ll i = 0; i < N; i++) {
        cin >> A[i];
    }

    Graph<ll> g(N);
    for (ll i = 0; i < M; i++) {
        ll a, b, c;
        cin >> a >> b >> c;
        a--;
        b--;
        g.add(a, b, c - A[b]);
    }

    BellmanFord<ll, add, lt, zero, inf> bellmanFord(g);
    bellmanFord.run(0);

    if (bellmanFord.dist[N - 1] == -inf()) {
        cout << "inf" << endl;
    } else {
        cout << A[0] - bellmanFord.dist[N - 1] << endl;
    }
}

int main() {
    ios::sync_with_stdio(false);

    solveE();

    return 0;
}
0