結果

問題 No.30 たこやき工場
ユーザー htensaihtensai
提出日時 2020-05-14 18:45:41
言語 Java21
(openjdk 21)
結果
AC  
実行時間 213 ms / 5,000 ms
コード長 1,737 bytes
コンパイル時間 2,257 ms
コンパイル使用メモリ 81,768 KB
実行使用メモリ 59,844 KB
最終ジャッジ日時 2023-08-23 05:22:53
合計ジャッジ時間 5,789 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
55,976 KB
testcase_01 AC 123 ms
55,600 KB
testcase_02 AC 124 ms
56,128 KB
testcase_03 AC 130 ms
55,896 KB
testcase_04 AC 129 ms
55,888 KB
testcase_05 AC 128 ms
55,892 KB
testcase_06 AC 134 ms
55,452 KB
testcase_07 AC 131 ms
55,392 KB
testcase_08 AC 180 ms
56,424 KB
testcase_09 AC 183 ms
56,936 KB
testcase_10 AC 213 ms
59,844 KB
testcase_11 AC 176 ms
56,192 KB
testcase_12 AC 132 ms
55,872 KB
testcase_13 AC 142 ms
55,396 KB
testcase_14 AC 180 ms
55,988 KB
testcase_15 AC 170 ms
57,308 KB
testcase_16 AC 184 ms
56,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>();
    static ArrayList<HashMap<Integer, Integer>> result = new ArrayList<>();
	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<>());
		    result.add(new HashMap<>());
		}
		for (int i = 0; i < m; i++) {
		    int source = sc.nextInt() - 1;
		    int count = sc.nextInt();
		    graph.get(sc.nextInt() - 1).put(source, count);
		}
		HashMap<Integer, Integer> ans = search(n - 1);
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < n - 1; i++) {
		    if (ans.containsKey(i)) {
		        sb.append(ans.get(i));
		    } else {
		        sb.append("0");
		    }
		    sb.append("\n");
		}
		System.out.print(sb);
	}
    
    static HashMap<Integer, Integer> search(int idx) {
        HashMap<Integer, Integer> map = result.get(idx);
        if (map.size() > 0) {
            return map;
        }
        if (graph.get(idx).size() == 0) {
            map.put(idx, 1);
            return map;
        }
        for (Map.Entry<Integer, Integer> entry1 : graph.get(idx).entrySet()) {
            HashMap<Integer, Integer> parts = search(entry1.getKey());
            for (Map.Entry<Integer, Integer> entry2 : parts.entrySet()) {
                int key = entry2.getKey();
                int value = entry1.getValue() * entry2.getValue();
                if (map.containsKey(key)) {
                    map.put(key, map.get(key) + value);
                } else {
                    map.put(key, value);
                }
            }
        }
        return map;
    }
}
0