結果

問題 No.30 たこやき工場
ユーザー 37zigen37zigen
提出日時 2020-03-30 15:52:25
言語 Java21
(openjdk 21)
結果
AC  
実行時間 221 ms / 5,000 ms
コード長 1,216 bytes
コンパイル時間 2,393 ms
コンパイル使用メモリ 81,056 KB
実行使用メモリ 58,968 KB
最終ジャッジ日時 2023-08-23 05:22:43
合計ジャッジ時間 6,096 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 131 ms
55,768 KB
testcase_01 AC 130 ms
55,720 KB
testcase_02 AC 131 ms
55,856 KB
testcase_03 AC 133 ms
56,152 KB
testcase_04 AC 132 ms
55,896 KB
testcase_05 AC 131 ms
55,808 KB
testcase_06 AC 140 ms
55,800 KB
testcase_07 AC 137 ms
55,980 KB
testcase_08 AC 190 ms
57,364 KB
testcase_09 AC 191 ms
56,232 KB
testcase_10 AC 221 ms
58,968 KB
testcase_11 AC 169 ms
56,136 KB
testcase_12 AC 136 ms
56,184 KB
testcase_13 AC 150 ms
56,208 KB
testcase_14 AC 191 ms
56,052 KB
testcase_15 AC 194 ms
56,332 KB
testcase_16 AC 197 ms
56,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

class Edge{
	int src,dst,need;
	
	public Edge(int src,int dst,int cost) {
		this.src=src;
		this.dst=dst;
		this.need=cost;
	}
}

class Main {
	public static void main(String[] args) {
		new Main().run();
	}
	
	void dfs(int cur,int par,ArrayList<Edge>[] g,long[] dp) {
		if(dp[cur]>0)return;
		for(Edge e:g[cur]) {
			if(e.dst==par)continue;
			dfs(e.dst,cur,g,dp);
			dp[cur]+=(long)dp[e.dst]*e.need;
		}
	}
	
	void run() {
		Scanner sc=new Scanner(System.in);
		int N=sc.nextInt();
		int M=sc.nextInt();
		ArrayList<Edge>[] g=new ArrayList[N];
		for(int i=0;i<g.length;++i)g[i]=new ArrayList<>();
		long[] dp=new long[N];
		int[] indeg=new int[N];
		dp[N-1]=1;	
		for(int i=0;i<M;++i) {
			int src=sc.nextInt();
			int need=sc.nextInt();
			int dst=sc.nextInt();
			--src;--dst;
			g[src].add(new Edge(src,dst,need));
			indeg[dst]++;
		}
		for(int i=0;i<N;++i) dfs(i,-1,g,dp);
		for(int i=0;i<N-1;++i) if(indeg[i]>0) dp[i]=0;
		Arrays.stream(Arrays.copyOfRange(dp, 0, N-1)).forEach(System.out::println);
	}
	
	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}

0