Source code for submission s789

Fr.java

  1. /*
  2.  * CTU Open 2013
  3.  * HES
  4.  */
  5. package template;
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.InputStreamReader;
  9. import java.util.HashMap;
  10. import java.util.LinkedList;
  11. import java.util.Map;
  12. import java.util.Queue;
  13. import java.util.StringTokenizer;
  14.  
  15. /**
  16.  *
  17.  * @author cteam007
  18.  */
  19. public class Fr {
  20.  
  21.  
  22. void useLine(String s) {
  23. st = new StringTokenizer(s);
  24. }
  25.  
  26. String nextToken() throws Exception {
  27. while (!st.hasMoreTokens()) {
  28. st = new StringTokenizer(input.readLine());
  29. }
  30. return st.nextToken();
  31. }
  32.  
  33. int nextInt() throws Exception {
  34. return Integer.parseInt(nextToken());
  35. }
  36.  
  37. /**
  38.   * @param args the command line arguments
  39.   */
  40. public static void main(String[] args) throws Exception {
  41. Fr instance = new Fr();
  42. while (instance.run()) {
  43. }
  44. }
  45.  
  46. boolean run() throws Exception {
  47. String in = input.readLine();
  48. if (in == null) {
  49. return false;
  50. }
  51.  
  52. useLine(in);
  53.  
  54. int numberOfNodes = nextInt();
  55. int centralNode = nextInt();
  56.  
  57. StartGraph sGraf = new StartGraph(numberOfNodes, centralNode);
  58.  
  59. for (int i = 0; i < numberOfNodes - 1; i++) {
  60. int nodeOne = nextInt();
  61. int nodeTwo = nextInt();
  62. int valve = nextInt();
  63.  
  64. sGraf.getStartNode(nodeOne).addEdge(nodeTwo, valve);
  65. sGraf.getStartNode(nodeTwo).addEdge(nodeOne, valve);
  66. }
  67.  
  68. BFS.create(centralNode, sGraf);
  69.  
  70. //System.out.println("");
  71. //sGraf.printGraph();
  72. //System.out.println("");
  73.  
  74. System.out.println(sGraf.getStartNode(sGraf.central).minimum(sGraf));
  75.  
  76. return true;
  77. }
  78. }
  79.  
  80. class StartNode {
  81.  
  82. int id;
  83. int parrent;
  84. int valve;
  85. char status;
  86. Map<Integer, Integer> neighbours = new HashMap<Integer, Integer>();
  87. Map<Integer, Integer> childs = new HashMap<Integer, Integer>();
  88.  
  89. public StartNode(int id) {
  90. this.id = id;
  91. this.status = 'F';
  92. this.parrent = -1;
  93. }
  94.  
  95. public void addEdge(int dest, int valve) {
  96. neighbours.put(dest, valve);
  97. }
  98.  
  99. public void addChild(int dest, int valve) {
  100. childs.put(dest, valve);
  101. }
  102.  
  103. public int minimum(StartGraph graph) {
  104.  
  105. int min = 0;
  106. for (Integer integer : childs.keySet()) {
  107. StartNode v = graph.getStartNode(integer);
  108. min += v.minimum(graph);
  109. }
  110.  
  111. if (childs.isEmpty()) {
  112. return valve;
  113. }
  114. return Math.min(min, valve);
  115. }
  116. }
  117.  
  118. class StartGraph {
  119.  
  120. Map<Integer, StartNode> nodes = new HashMap<Integer, StartNode>();
  121. int central;
  122. int count;
  123.  
  124. public StartGraph(int count, int central) {
  125. this.central = central;
  126. this.count = count;
  127.  
  128. for (int i = 1; i <= count; i++) {
  129. nodes.put(i, new StartNode(i));
  130. }
  131. }
  132.  
  133. public StartNode getStartNode(int id) {
  134. return nodes.get(id);
  135. }
  136.  
  137. public void printGraph() {
  138. for (int i = 0; i < count; i++) {
  139. System.out.println("Node: " + nodes.get(i).id + " v: " + nodes.get(i).valve);
  140.  
  141. for (Integer integer : nodes.get(i).childs.keySet()) {
  142. StartNode v = getStartNode(integer);
  143. System.out.println("-->Node: " + v.id + " v: " + v.valve);
  144. }
  145. }
  146. }
  147. }
  148.  
  149. class BFS {
  150.  
  151. public static void create(int central, StartGraph graf) {
  152. StartNode s = graf.getStartNode(central);
  153.  
  154. s.status = 'O';
  155. s.parrent = -1;
  156. s.valve = Integer.MAX_VALUE;
  157.  
  158. Queue<Integer> queue = new LinkedList<Integer>();
  159.  
  160. queue.add(central);
  161.  
  162. while (!queue.isEmpty()) {
  163. StartNode u = graf.getStartNode(queue.remove());
  164.  
  165.  
  166. for (Integer integer : u.neighbours.keySet()) {
  167. StartNode v = graf.getStartNode(integer);
  168.  
  169. if (v.status == 'F') {
  170. u.addChild(v.id, u.neighbours.get(v.id));
  171. v.valve = u.neighbours.get(v.id);
  172. v.status = 'O';
  173. v.parrent = u.id;
  174. queue.add(v.id);
  175. }
  176. }
  177. u.status = 'C';
  178. }
  179. }
  180. }
  181.