結果

問題 No.468 役に立つ競技プログラミング実践編
ユーザー tenten
提出日時 2020-12-24 13:06:06
言語 Java
(openjdk 23)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,733 bytes
コンパイル時間 2,702 ms
コンパイル使用メモリ 87,752 KB
実行使用メモリ 151,300 KB
最終ジャッジ日時 2024-09-21 16:52:59
合計ジャッジ時間 25,491 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 21 TLE * 10
other AC * 6
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>();
    static ArrayList<HashMap<Integer, Integer>> rgraph = new ArrayList<>();
    static int[] costs;
    static int[] rcosts;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        for (int i = 0; i < n; i++) {
            graph.add(new HashMap<>());
            rgraph.add(new HashMap<>());
        }
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
            graph.get(a).put(b, c);
            rgraph.get(b).put(a, c);
        }
        costs = new int[n];
        Arrays.fill(costs, -1);
        costs[0] = 0;
        dfw(n - 1);
        rcosts = new int[n];
        Arrays.fill(rcosts, Integer.MAX_VALUE);
        rcosts[n - 1] = costs[n - 1];
        rdfw(0);
        int ans = 0;
        for (int i = 0; i < n; i++) {
            if (costs[i] < rcosts[i]) {
                ans++;
            }
        }
        System.out.println(costs[n - 1] + " " + ans + "/" + n);
    }
    
    static int rdfw(int idx) {
        if (rcosts[idx] == Integer.MAX_VALUE) {
            for (int x : graph.get(idx).keySet()) {
                rcosts[idx] = Math.min(rcosts[idx], rdfw(x) - graph.get(idx).get(x));
            }
        }
        return rcosts[idx];
    }
    
    static int dfw(int idx) {
        if (costs[idx] < 0) {
            for (int x : rgraph.get(idx).keySet()) {
                costs[idx] = Math.max(costs[idx], dfw(x) + rgraph.get(idx).get(x));
            }
        }
        return costs[idx];
    }
}
0