結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
55,896 KB
testcase_01 AC 126 ms
56,196 KB
testcase_02 AC 127 ms
55,864 KB
testcase_03 AC 130 ms
55,736 KB
testcase_04 AC 128 ms
55,884 KB
testcase_05 AC 130 ms
55,996 KB
testcase_06 AC 132 ms
56,092 KB
testcase_07 AC 131 ms
55,640 KB
testcase_08 AC 177 ms
56,284 KB
testcase_09 AC 185 ms
56,076 KB
testcase_10 AC 215 ms
58,728 KB
testcase_11 AC 162 ms
55,788 KB
testcase_12 AC 132 ms
56,196 KB
testcase_13 AC 140 ms
55,812 KB
testcase_14 AC 188 ms
56,448 KB
testcase_15 AC 186 ms
56,024 KB
testcase_16 AC 198 ms
56,488 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];
		int[] outdeg=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));
			outdeg[src]++;
			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) {
				System.out.println(dp[i]);
			}else {
				System.out.println(0);
			}
		}
	}
	
	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}

0