Source code for submission s924

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. int thisprice = parentPrice;
  54. if(price == 0 || price > thisprice){
  55. return thisprice;
  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. int n = Integer.parseInt(line.split(" ")[0]);
  110. int rootId = Integer.parseInt(line.split(" ")[1]);
  111.  
  112. Node root = new Node(rootId);
  113. Graph g = new Graph();
  114. g.nodes = n;
  115. g.root = root;
  116. g.uzly.put(Integer.valueOf(rootId), root);
  117. for(int i=0;i<(n-1);i++){
  118. line = in.readLine();
  119. String params[] = line.split(" ");
  120. int f = Integer.parseInt(params[0]);
  121. int t = Integer.parseInt(params[1]);
  122. int price = Integer.parseInt(params[2]);
  123. g.addEdge(f,t,price);
  124. }
  125.  
  126. System.out.println(g.root.getEffort(null,0));
  127.  
  128. }
  129.  
  130. }
  131.