Go to diff to previous submission
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { protected int nodesCnt; protected int centralNode; protected int[][] edges = new int[1001][1000]; protected int[][] edgesPrice = new int[1001][1000]; protected int[] edgesCnt = new int[1001]; Main p = new Main(); p.run(); } while (input.ready()) { this.nodesCnt = this.nextInt(); this.centralNode = this.nextInt(); for (int i = 1; i <= nodesCnt; i++) { edgesCnt[i] = 0; } for (int i = 0; i < this.nodesCnt - 1; i++) { int b = this.nextInt(); int e = this.nextInt(); int price = this.nextInt(); edges[b][edgesCnt[b]] = e; edges[e][edgesCnt[e]] = b; edgesPrice[b][edgesCnt[b]] = price; edgesPrice[e][edgesCnt[e]] = price; edgesCnt[b]++; edgesCnt[e]++; } } } protected int getPrice(int node, int parent, int parentPrice) { if (edgesCnt[node] == 1 && parentPrice != -10) { return edgesPrice[node][0]; } int sum = 0; for (int i = 0; i < edgesCnt[node]; i++) { if (edges[node][i] != parent) { sum += getPrice(edges[node][i], node, edgesPrice[node][i]); } if (sum >= parentPrice && parentPrice != -10) { return parentPrice; } } return sum; /*int price = 0; for (int i = 0; i < edgesCnt[node]; i++) { if (edges[node][i] != parent) { closeMePrice += edgesPrice[node][i]; closeChildrenPrice += getPrice(edges[node][i], node); } } if (closeChildrenPrice == 0) { return closeMePrice; } return Math.min(closeMePrice, closeChildrenPrice);*/ } while (!st.hasMoreTokens()) { } return st.nextToken(); } } return input.readLine(); } }
--- c5.s806.cteam001.fr.java.0.Main.java +++ c5.s831.cteam001.fr.java.0.Main.java @@ -8,7 +8,7 @@ protected int nodesCnt; protected int centralNode; - protected int[][] edges; - protected int[][] edgesPrice; - protected int[] edgesCnt; + protected int[][] edges = new int[1001][1000]; + protected int[][] edgesPrice = new int[1001][1000]; + protected int[] edgesCnt = new int[1001]; public static void main(String[] args) throws Exception { @@ -22,7 +22,7 @@ this.centralNode = this.nextInt(); - edges = new int[nodesCnt + 1][nodesCnt]; - edgesPrice = new int[nodesCnt + 1][nodesCnt]; - edgesCnt = new int[nodesCnt + 1]; + for (int i = 1; i <= nodesCnt; i++) { + edgesCnt[i] = 0; + } for (int i = 0; i < this.nodesCnt - 1; i++) {