Source code for submission s1095

Go to diff to previous submission

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]) - 1;
  30. int cil = Integer.parseInt(cesta[1]) - 1;
  31. int vaha = Integer.parseInt(cesta[2]);
  32.  
  33.  
  34. if(cil == c){
  35. int temp = od;
  36. od = cil;
  37. cil = temp;
  38. }
  39.  
  40. if(strom[cil].pred != null){
  41. int temp = od;
  42. od = cil;
  43. cil = temp;
  44. }
  45.  
  46.  
  47. strom[cil].pred = strom[od];
  48. strom[od].potomci.add(new Hrana(vaha, strom[cil]));
  49.  
  50. }
  51.  
  52. int celkovaVaha = projdiNasl(strom[c], Integer.MAX_VALUE);
  53.  
  54. System.out.println(celkovaVaha);
  55.  
  56. }
  57.  
  58. }
  59.  
  60. public static int projdiNasl(Node od, int vaha){
  61. int celkovaVaha = 0;
  62.  
  63. for (int i = 0; i < od.potomci.size(); i++) {
  64. celkovaVaha += Math.min(od.potomci.get(i).vaha, projdiNasl(od.potomci.get(i).cil, od.potomci.get(i).vaha));
  65. }
  66. if(od.potomci.size()>0)
  67. return celkovaVaha;
  68. else
  69. return Integer.MAX_VALUE;
  70. }
  71.  
  72. }
  73.  
  74. class Node{
  75. ArrayList<Hrana> potomci;
  76. int cislo;
  77. Node pred = null;
  78.  
  79. public Node(int cislo){
  80. this.cislo = cislo;
  81. potomci = new ArrayList<Hrana>();
  82. }
  83. }
  84.  
  85.  
  86. class Hrana{
  87. int vaha;
  88. Node cil;
  89.  
  90.  
  91. public Hrana(int vaha, Node cil){
  92. this.vaha = vaha;
  93. this.cil = cil;
  94. }
  95.  
  96. }
  97.  

Diff to submission s1045

Fr.java

--- c5.s1045.cteam058.fr.java.0.Fr.java
+++ c5.s1095.cteam058.fr.java.0.Fr.java
@@ -27,9 +27,16 @@
                                 String vstup = sc.nextLine();
                                 String[] cesta = vstup.split(" ");
-                                int od = Integer.parseInt(cesta[0]);
-                                int cil = Integer.parseInt(cesta[1]);
+                                int od = Integer.parseInt(cesta[0]) - 1;
+                                int cil = Integer.parseInt(cesta[1]) - 1;
                                 int vaha = Integer.parseInt(cesta[2]);
                                 
-                                if(cil - 1 == c){
+                                
+                                if(cil == c){
+                                        int temp = od;
+                                        od = cil;
+                                        cil = temp;
+                                }
+                                
+                                if(strom[cil].pred != null){
                                         int temp = od;
                                         od = cil;
@@ -37,6 +44,8 @@
                                 }
                                         
+                                        
+                                strom[cil].pred = strom[od];
+                                strom[od].potomci.add(new Hrana(vaha, strom[cil]));
                                 
-                                strom[od - 1].potomci.add(new Hrana(vaha, strom[cil - 1]));
                         }
                         
@@ -66,4 +75,5 @@
         ArrayList<Hrana> potomci;
         int cislo;
+        Node pred = null;
         
         public Node(int cislo){
@@ -78,4 +88,5 @@
         Node cil;
         
+        
         public Hrana(int vaha, Node cil){
                 this.vaha = vaha;