結果

問題 No.30 たこやき工場
ユーザー ぴろず
提出日時 2015-02-16 22:28:59
言語 Java
(openjdk 23)
結果
AC  
実行時間 243 ms / 5,000 ms
コード長 1,138 bytes
コンパイル時間 2,802 ms
コンパイル使用メモリ 78,064 KB
実行使用メモリ 57,480 KB
最終ジャッジ日時 2024-12-21 05:19:34
合計ジャッジ時間 6,760 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

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