Source code for submission s1045

Fr.java

  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4.  
  5. public class Fr {
  6.  
  7. static Node [] strom;
  8.  
  9. public static void main(String[] args) {
  10. Scanner sc = new Scanner(System.in);
  11.  
  12.  
  13.  
  14. while (sc.hasNextInt()) {
  15.  
  16. int n = sc.nextInt();
  17. int c = sc.nextInt() - 1;
  18. sc.nextLine();
  19.  
  20. strom = new Node[n];
  21.  
  22. for(int i = 0; i < strom.length; i++){
  23. strom[i] = new Node(i);
  24. }
  25.  
  26. for (int i = 0; i < strom.length - 1; i++) {
  27. String vstup = sc.nextLine();
  28. String[] cesta = vstup.split(" ");
  29. int od = Integer.parseInt(cesta[0]);
  30. int cil = Integer.parseInt(cesta[1]);
  31. int vaha = Integer.parseInt(cesta[2]);
  32.  
  33. if(cil - 1 == c){
  34. int temp = od;
  35. od = cil;
  36. cil = temp;
  37. }
  38.  
  39.  
  40. strom[od - 1].potomci.add(new Hrana(vaha, strom[cil - 1]));
  41. }
  42.  
  43. int celkovaVaha = projdiNasl(strom[c], Integer.MAX_VALUE);
  44.  
  45. System.out.println(celkovaVaha);
  46.  
  47. }
  48.  
  49. }
  50.  
  51. public static int projdiNasl(Node od, int vaha){
  52. int celkovaVaha = 0;
  53.  
  54. for (int i = 0; i < od.potomci.size(); i++) {
  55. celkovaVaha += Math.min(od.potomci.get(i).vaha, projdiNasl(od.potomci.get(i).cil, od.potomci.get(i).vaha));
  56. }
  57. if(od.potomci.size()>0)
  58. return celkovaVaha;
  59. else
  60. return Integer.MAX_VALUE;
  61. }
  62.  
  63. }
  64.  
  65. class Node{
  66. ArrayList<Hrana> potomci;
  67. int cislo;
  68.  
  69. public Node(int cislo){
  70. this.cislo = cislo;
  71. potomci = new ArrayList<Hrana>();
  72. }
  73. }
  74.  
  75.  
  76. class Hrana{
  77. int vaha;
  78. Node cil;
  79.  
  80. public Hrana(int vaha, Node cil){
  81. this.vaha = vaha;
  82. this.cil = cil;
  83. }
  84.  
  85. }
  86.