結果

問題 No.2712 Play more!
ユーザー blue_jamblue_jam
提出日時 2024-03-31 21:53:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 4,447 bytes
コンパイル時間 4,470 ms
コンパイル使用メモリ 268,472 KB
実行使用メモリ 6,548 KB
最終ジャッジ日時 2024-03-31 21:53:37
合計ジャッジ時間 9,646 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using namespace std;
using namespace atcoder;

using ll = long long;
using mint = modint998244353;

/**
 * @file
 * ### グラフの要素
 * グラフは各頂点から出て行く辺をリストとして保持する隣接リスト表現と各頂点間に辺が存在する情報を記録する隣接行列表現がある.
 * それぞれ利点があるので状況に応じて使い分けること.
 *
 * <dl>
 *   <dt>隣接リスト</dt><dd>頂点を基準に考えればいいときに有向.重複辺も扱える.</dd>
 *   <dt>隣接行列</dt><dd>@f$O(E)@f$ が @f$O(V^2)@f$ になる.密グラフや,頂点数が少ないグラフについて効率よく扱える.重複辺を扱いにくい.</dd>
 * </dl>
 *
 * ### ソースコード
 *
 * @include graph.cpp
 *
 * Edgeのless演算子のオーバーロードで,大きいほうが左辺のときにtrueを返すようにしている.
 * これはpriority_queueで,weightの小さいほうを先に取り出すときの工夫である.
 * 気に入らない場合は,greater演算子をオーバーロードし,priority_queueを以下のように宣言する.
 *
 * @code
 * priority_queue<Edge>, vector<Edge>, greater<Edge>> Q;
 * @endcode
 */
typedef ll Weight;
struct Edge{
    int from, to;
    Weight weight;
    int rev;     // 無向グラフの対の辺
    Edge(int from, int to, Weight weight) :
            from(from), to(to), weight(weight) { }
    Edge(int from, int to, Weight weight, int rev) :
            from(from), to(to), weight(weight), rev(rev){ }
};
bool operator < (const Edge &a, const Edge &b){
    if(a.weight != b.weight) return a.weight > b.weight;
    if(a.from != b.from) return a.from > b.from;
    return  a.to > b.to;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;

void addFlowEdge(Graph &g, int a, int b, Weight c){
    g[a].push_back(Edge(a, b, c, g[b].size()));
    g[b].push_back(Edge(b, a, 0, g[a].size() - 1));
}
void addUndirectedEdge(Graph &g, int a, int b, Weight c){
    g[a].push_back(Edge(a, b, c, g[b].size()));
    g[b].push_back(Edge(b, a, c, g[a].size() - 1));
}

const ll INF = 1e17;

/**
 * bellmanFordでは最短路を計算する.
 *
 * bellmanFordにおいて,distに-INFと記録された頂点以外にも,負の閉路の影響を受ける頂点があることがある.-INFと記録された頂点から到達できるすべての頂点は負の閉路の影響を受ける.(要確認)
 *
 * @param g 最短路を求めたいグラフ
 * @param s 始点
 * @param dist 距離を記録する.経路がない場合はINF,そこへ到達するまでに負の閉路が含まれることがわかっていれば-1
 * @param prev 経路復元用の1つ前の位置を示す配列
 * @return 負の閉路が含まれているか
 */
bool bellmanFord(const Graph &g, int s, vector<Weight> &dist, vector<int> &prev){
    int n = g.size();
    bool negativeLoop = false;
    dist.assign(n, INF); dist[s] = 0;
    prev.assign(n, -1);
    for(int k = 0; k < n; ++k){
        for(int v = 0; v < 2 * n; ++v){
            for(Edges::const_iterator i=g[v].begin(); i != g[v].end(); ++i){
                if(dist[i -> from] != INF && dist[i -> to] > dist[i -> from] + i -> weight){
                    if (dist[i -> from] == -INF) {
                        dist[i -> to] = -INF;
                    } else {
                        dist[i -> to] = dist[i -> from] + i-> weight;
                    }
                    prev[i -> to] = i -> from;
                    if(k == n - 1){
                        dist[i -> to] = -INF;
                        negativeLoop = true;
                    }
                }
            }
        }
    }
    return negativeLoop;
}

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

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

    vector<ll> dist;
    vector<int> prev;
    bool negativeLoop = bellmanFord(g, 0, dist, prev);

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

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

    solveE();

    return 0;
}
0