結果

問題 No.2712 Play more!
ユーザー shogo314
提出日時 2024-03-31 14:54:41
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,806 bytes
コンパイル時間 621 ms
コンパイル使用メモリ 78,388 KB
最終ジャッジ日時 2025-02-20 17:29:45
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28 WA * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>  //fillを使うため
#include <iostream>
#include <vector>

using ll = long long;

using namespace std;

const ll INF = 1LL << 61;

struct edge {
    ll from;  // 出発点
    ll to;    // 到達点
    ll cost;  // 移動コスト
};

int main() {
    ll V;                // 頂点の数
    ll side;             // 辺の数
    ll S;                // 始点
    ll G;                // 終点
    vector<edge> edges;  // 移動の情報を保存する

    cin >> V;
    cin >> side;
    vector<ll> A(V);
    for (ll& a : A) cin >> a;
    S = 0;
    G = V - 1;

    vector<ll> d(V, INF);  // 始点から添え字の頂点に行くのにかかるコスト
    d[S] = 0;              // 始点を0にする

    for (int i = 0; i < side; i++) {
        struct edge add;
        cin >> add.from;
        add.from--;
        cin >> add.to;
        add.to--;
        cin >> add.cost;
        add.cost -= A[add.to];
        edges.push_back(add);
    }

    for (int i = 0; i < V; i++) {
        for (int j = 0; j < (int)edges.size(); j++) {
            struct edge e = edges[j];
            if (d[e.from] == -INF) {
                d[e.to] = -INF;
                continue;
            }
            if (d[e.to] > d[e.from] + e.cost) {  // 移動した後のコストが小さいと、頂点のコストを更新
                d[e.to] = d[e.from] + e.cost;
                if (i == V - 1) {  // 頂点の数と同じ回数ループすると、負の閉路があるのでループをぬける
                    d[e.to] = -INF;
                }
            }
            if (d[e.to] < -INF) {
                d[e.to] = -INF;
            }
        }
    }
    if (d[G] == -INF) {
        std::cout << "inf" << std::endl;
        return 0;
    }
    cout << A[S] - d[G] << endl;
}
0