Source code for submission s935

Fr.java

  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4.  
  5.  
  6. public class Fr {
  7.  
  8. /**
  9. * @param args
  10. */
  11. public static void main(String[] args) {
  12. Scanner sc = new Scanner(System.in);
  13.  
  14. while(sc.hasNext()){
  15. int nodeCnt = sc.nextInt();
  16. int centralNd = sc.nextInt();
  17.  
  18. // Fr.Node[] nodes = new Fr.Node[nodeCnt];
  19. // Fr.Node root = nodes[centralNd];
  20.  
  21. int[][] edges = new int[nodeCnt][nodeCnt];
  22.  
  23. for(int i = 0; i < nodeCnt - 1; i++){
  24. int nodeA = sc.nextInt() - 1;
  25. int nodeB = sc.nextInt() - 1;
  26. int price = sc.nextInt();
  27.  
  28. edges[nodeA][nodeB] = price;
  29. edges[nodeB][nodeA] = price;
  30. }
  31.  
  32. long suma = getMin(centralNd - 1, edges, Long.MAX_VALUE);
  33.  
  34. System.out.println(suma);
  35.  
  36.  
  37.  
  38. }
  39.  
  40.  
  41. }
  42.  
  43. public static long getMin(int nodeIndex, int edges [][], long tmpMin){
  44. long suma = 0;
  45.  
  46. for(int i = 0; i < edges.length; i++){
  47. if(i != nodeIndex){
  48. if(edges[nodeIndex][i] != 0){
  49. int tmpVal = edges[nodeIndex][i];
  50. edges[nodeIndex][i] = 0;
  51. edges[i][nodeIndex] = 0;
  52.  
  53. suma += getMin(i, edges, tmpVal);
  54. }
  55. }
  56.  
  57. }
  58.  
  59.  
  60.  
  61. if(suma == 0){
  62. return tmpMin;
  63. }else{
  64. return tmpMin < suma ? tmpMin : suma;
  65. }
  66. }
  67.  
  68. class Node {
  69. ArrayList<Node> childrens;
  70. long price;
  71.  
  72. public Node(long price) {
  73. childrens = new ArrayList<Fr.Node>();
  74. this.price = price;
  75. }
  76.  
  77. public void addChildren(Node n){
  78. childrens.add(n);
  79. }
  80. }
  81.  
  82. }
  83.