結果

問題 No.30 たこやき工場
ユーザー ぴろずぴろず
提出日時 2015-02-16 22:28:59
言語 Java21
(openjdk 21)
結果
AC  
実行時間 220 ms / 5,000 ms
コード長 1,138 bytes
コンパイル時間 2,218 ms
コンパイル使用メモリ 81,900 KB
実行使用メモリ 59,540 KB
最終ジャッジ日時 2023-08-23 05:15:21
合計ジャッジ時間 5,889 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
55,916 KB
testcase_01 AC 128 ms
55,544 KB
testcase_02 AC 130 ms
55,564 KB
testcase_03 AC 132 ms
55,468 KB
testcase_04 AC 135 ms
55,536 KB
testcase_05 AC 133 ms
55,484 KB
testcase_06 AC 137 ms
55,536 KB
testcase_07 AC 135 ms
55,672 KB
testcase_08 AC 188 ms
56,068 KB
testcase_09 AC 192 ms
55,956 KB
testcase_10 AC 220 ms
59,540 KB
testcase_11 AC 173 ms
55,992 KB
testcase_12 AC 136 ms
55,808 KB
testcase_13 AC 146 ms
55,788 KB
testcase_14 AC 188 ms
55,712 KB
testcase_15 AC 190 ms
55,580 KB
testcase_16 AC 195 ms
56,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no30;

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

	static long[][] memo;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		@SuppressWarnings("unchecked")
		ArrayList<Pair>[] recipe = new ArrayList[n];
		for(int i=0;i<n;i++) {
			recipe[i] = new ArrayList<>();
		}
		for(int i=0;i<m;i++) {
			int p = sc.nextInt() - 1;
			int q = sc.nextInt();
			int r = sc.nextInt() - 1;
			recipe[r].add(new Pair(p,q));
		}
		memo = new long[n][];
		long[] ans = dfs(n-1,n,recipe);
		for(int i=0;i<n-1;i++) {
			System.out.println(ans[i]);
		}

	}

	static long[] dfs(int i,int n,ArrayList<Pair>[] recipe) {
		if (memo[i] != null) {
			return memo[i];
		}
		long[] sum = new long[n];
		if (recipe[i].size() == 0) {
			sum[i] = 1;
		}else{
			for(Pair p:recipe[i]) {
				long[] m = dfs(p.material,n,recipe);
				for(int j=0;j<n;j++) {
					sum[j] += m[j] * p.quantity;
				}
			}
		}
		return memo[i] = sum;
	}


}
class Pair {
	int material;
	int quantity;
	public Pair(int p,int q) {
		this.material = p;
		this.quantity = q;
	}
}
0