結果

問題 No.2712 Play more!
ユーザー InTheBloomInTheBloom
提出日時 2024-03-31 15:13:58
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 117 ms / 2,000 ms
コード長 3,253 bytes
コンパイル時間 3,152 ms
コンパイル使用メモリ 166,504 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-04-03 12:10:32
合計ジャッジ時間 5,487 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 1 ms
6,676 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 1 ms
6,676 KB
testcase_07 AC 69 ms
6,676 KB
testcase_08 AC 70 ms
6,676 KB
testcase_09 AC 68 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 21 ms
6,676 KB
testcase_12 AC 78 ms
6,676 KB
testcase_13 AC 21 ms
6,676 KB
testcase_14 AC 79 ms
6,676 KB
testcase_15 AC 114 ms
6,676 KB
testcase_16 AC 12 ms
6,676 KB
testcase_17 AC 5 ms
6,676 KB
testcase_18 AC 9 ms
6,676 KB
testcase_19 AC 6 ms
6,676 KB
testcase_20 AC 34 ms
6,676 KB
testcase_21 AC 18 ms
6,676 KB
testcase_22 AC 62 ms
6,676 KB
testcase_23 AC 9 ms
6,676 KB
testcase_24 AC 14 ms
6,676 KB
testcase_25 AC 6 ms
6,676 KB
testcase_26 AC 13 ms
6,676 KB
testcase_27 AC 27 ms
6,676 KB
testcase_28 AC 49 ms
6,676 KB
testcase_29 AC 15 ms
6,676 KB
testcase_30 AC 117 ms
6,676 KB
testcase_31 AC 93 ms
6,676 KB
testcase_32 AC 86 ms
6,676 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 1 ms
6,676 KB
testcase_35 AC 1 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

void main () {
    int N, M; readln.read(N, M);
    auto A = readln.split.to!(int[]);

    // 辺の重みをc - Aにすると最短経路問題に帰着
    // 1を含むSCCとNを含むSCCでベルマンフォードをし、inf判定 -> infでないなら全体のグラフでベルマンフォードをする。
    // これはしんどいので、全体グラフでベルマンフォードをし、負閉路が1かNのSCCに含まれるかをみる。

    solve(N, M, A);
}

void solve (int N, int M, int[] A) {
    auto graph = new Tuple!(int, int)[][](N, 0);
    foreach (i; 0..M) {
        int a, b, c; readln.read(a, b, c);
        a--, b--;
        graph[a] ~= tuple(b, c - A[a]);
    }

    auto dist = new long[](N);
    dist[] = long.max;
    dist[0] = 0;

    // bellman-ford
    foreach (_; 0..N - 1) {
        foreach (i; 0..N) {
            if (dist[i] == long.max) continue;
            foreach (to; graph[i]) {
                int v = to[0];
                long c = to[1];
                if (dist[v] <= dist[i] + c) continue;
                dist[v] = dist[i] + c;
            }
        }
    }

    foreach (_; 0..N - 1) {
        foreach (i; 0..N) {
            if (dist[i] == long.max) continue;
            foreach (to; graph[i]) {
                int v = to[0];
                long c = to[1];

                if (dist[i] == -long.max) {
                    dist[v] = -long.max;
                    continue;
                }
                if (dist[v] <= dist[i] + c) continue;
                dist[v] = -long.max;
            }
        }
    }

    if (dist[N - 1] == -long.max) {
        writeln("inf");
    }
    else {
        writeln(-dist[N - 1] + A[N - 1]);
    }
}

void read (T...) (string S, ref T args) {
    import std.conv : to;
    import std.array : split;
    auto buf = S.split;
    foreach (i, ref arg; args) {
        arg = buf[i].to!(typeof(arg));
    }
}

int[][] scc_decomposition (int[][] graph, int[][] revgraph)
in {
    assert(graph.length == revgraph.length, "graph.length must be equal to revgraph.length");
}
do {
    /**
     * assume:
     * 1. vertex is 0-indexed and maximum vertex number is less than graph.length
     * 2. if graph has edge (u, v), revgraph has edge (v, u).
     *
     * verified with:
     * - yosupo judge | Stringly Connected Components (https://judge.yosupo.jp/problem/scc) (LDC2/632ms/57.90Mib)
     * - AIZU ONLINE JUDGE | 強連結成分分解 (https://onlinejudge.u-aizu.ac.jp/problems/GRL_3_C)
     **/

    static bool[] vis;
    static int[] Q;

    vis.length = graph.length;
    Q.length = 0;

    void dfs (int pos) {
        vis[pos] = true;
        foreach (to; graph[pos]) {
            if (vis[to]) continue;
            dfs(to);
        }
        Q ~= pos;
    }

    foreach (i; 0..graph.length) {
        if (!vis[i]) dfs(cast(int) i);
    }

    int[][] scc;
    int idx = 0;
    vis[] = false;
    void revdfs (int pos) {
        vis[pos] = true;
        foreach (to; revgraph[pos]) {
            if (vis[to]) continue;
            revdfs(to);
        }
        scc[idx] ~= pos;
    }

    foreach_reverse (q; Q) {
        if (vis[q]) continue;
        scc ~= new int[](0);
        revdfs(q);
        idx++;
    }

    return scc;
}
0