Source code for submission s950

Go to diff to previous submission

Fr.java

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package fr;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11. import java.util.ArrayList;
  12. import java.util.HashMap;
  13.  
  14.  
  15. /**
  16.  *
  17.  * @author cteam025
  18.  */
  19.  
  20. class Edge{
  21.  
  22. public Node n1;
  23. public Node n2;
  24. public int price;
  25.  
  26. public Node dejSouseda(Node ja){
  27. if(ja==n1){
  28. return n2;
  29. }
  30. return n1;
  31. }
  32. }
  33. class Node{
  34. public int id = 0;
  35. public ArrayList<Edge> hrany = new ArrayList();
  36. public Node parent;
  37.  
  38. public int getEffort(Node parent, int parentPrice){
  39. int price = 0;
  40.  
  41. for(Edge h : hrany){
  42. if(parent == h.dejSouseda(this)){
  43. continue;
  44. }
  45. // System.out.println(h.dejSouseda(this).id);
  46. price += h.dejSouseda(this).getEffort(this, h.price);
  47. }
  48.  
  49. if (parent == null){
  50. return price;
  51. }
  52.  
  53.  
  54. if(price == 0 || price > parentPrice){
  55. return parentPrice;
  56. }
  57.  
  58. return price;
  59. }
  60.  
  61. public Node(int id){
  62. this.id = id;
  63. }
  64.  
  65. }
  66.  
  67. class Graph{
  68.  
  69. public Node root;
  70. public int nodes;
  71. public HashMap<Integer, Node> uzly = new HashMap();
  72.  
  73. public void addEdge(int f, int t, int price){
  74. Node node1 = uzly.get(Integer.valueOf(f));
  75. Node node2 = uzly.get(Integer.valueOf(t));
  76. if(node1==null){
  77. node1 = new Node(f);
  78. uzly.put(Integer.valueOf(f), node1);
  79. }
  80. if(node2==null){
  81. node2 = new Node(t);
  82. uzly.put(Integer.valueOf(t), node2);
  83. }
  84. Edge hrana = new Edge();
  85. hrana.price = price;
  86. hrana.n1 = node1;
  87. hrana.n2 = node2;
  88.  
  89. node1.hrany.add(hrana);
  90. node2.hrany.add(hrana);
  91. }
  92.  
  93.  
  94.  
  95. }
  96.  
  97.  
  98. public class Fr {
  99.  
  100. /**
  101.   * @param args the command line arguments
  102.   */
  103.  
  104. public static void main(String[] args) throws IOException {
  105. // TODO code application logic here
  106.  
  107. String line = in.readLine();
  108.  
  109. while(line != null){
  110. int n = Integer.parseInt(line.split(" ")[0]);
  111. int rootId = Integer.parseInt(line.split(" ")[1]);
  112.  
  113. Node root = new Node(rootId);
  114. Graph g = new Graph();
  115. g.nodes = n;
  116. g.root = root;
  117. g.uzly.put(Integer.valueOf(rootId), root);
  118. for(int i=0;i<(n-1);i++){
  119. line = in.readLine();
  120. String params[] = line.split(" ");
  121. int f = Integer.parseInt(params[0]);
  122. int t = Integer.parseInt(params[1]);
  123. int price = Integer.parseInt(params[2]);
  124. g.addEdge(f,t,price);
  125. }
  126.  
  127. System.out.println(g.root.getEffort(null,0));
  128. line = in.readLine();
  129. }
  130. }
  131.  
  132. }
  133.  

Diff to submission s924

Fr.java

--- c5.s924.cteam025.fr.java.0.Fr.java
+++ c5.s950.cteam025.fr.java.0.Fr.java
@@ -51,7 +51,7 @@
         }
         
-        int thisprice = parentPrice;
-        if(price == 0 || price > thisprice){
-            return thisprice;
+        
+        if(price == 0 || price > parentPrice){
+            return parentPrice;
         }
         
@@ -108,23 +108,25 @@
         String line = in.readLine();
         
-        int n = Integer.parseInt(line.split(" ")[0]);
-        int rootId = Integer.parseInt(line.split(" ")[1]);
-        
-        Node root = new Node(rootId);
-        Graph g = new Graph();
-        g.nodes = n;
-        g.root = root;
-        g.uzly.put(Integer.valueOf(rootId), root);
-        for(int i=0;i<(n-1);i++){
+        while(line != null){
+            int n = Integer.parseInt(line.split(" ")[0]);
+            int rootId = Integer.parseInt(line.split(" ")[1]);
+
+            Node root = new Node(rootId);
+            Graph g = new Graph();
+            g.nodes = n;
+            g.root = root;
+            g.uzly.put(Integer.valueOf(rootId), root);
+            for(int i=0;i<(n-1);i++){
+                line = in.readLine();
+                String params[] = line.split(" ");
+                int f = Integer.parseInt(params[0]);
+                int t = Integer.parseInt(params[1]);
+                int price = Integer.parseInt(params[2]);
+                g.addEdge(f,t,price);
+            }
+
+            System.out.println(g.root.getEffort(null,0));
             line = in.readLine();
-            String params[] = line.split(" ");
-            int f = Integer.parseInt(params[0]);
-            int t = Integer.parseInt(params[1]);
-            int price = Integer.parseInt(params[2]);
-            g.addEdge(f,t,price);
         }
-        
-        System.out.println(g.root.getEffort(null,0));
-        
     }