Source code for submission s981

FQ.java

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package fq;
  6.  
  7. import java.io.File;
  8. import java.io.FileNotFoundException;
  9. import java.util.ArrayList;
  10. import java.util.Scanner;
  11.  
  12. /**
  13.  *
  14.  * @author drevenak3
  15.  */
  16. public class FQ {
  17.  
  18. /**
  19.   * @param args the command line arguments
  20.   */
  21. public static void main(String[] args) {
  22. Scanner scan = new Scanner(System.in);
  23. try{
  24. scan=new Scanner(new File("in.txt"));
  25. int n, u, v, hlavny;
  26. while (scan.hasNext()) {
  27. n = scan.nextInt();
  28. hlavny = scan.nextInt();
  29. int[][] pole = new int[n + 1][n + 1];
  30. for (int i = 1; i <= n-1; i++) {
  31. u = scan.nextInt();
  32. v = scan.nextInt();
  33. int c=scan.nextInt();
  34. pole[u][v] = c;
  35. pole[v][u] = c;
  36. }
  37. /*ArrayList<Integer> rh=new ArrayList<Integer>();
  38. ArrayList<Integer> rhw=new ArrayList<Integer>();
  39. for(int i=1;i<=n;++i){
  40. int st=0;
  41. for(int j=1;j<i;++j){
  42. if(pole[i][j]!=0){
  43. ++st;
  44. if(st>=2) break;
  45. }
  46. }
  47. if(st==1){
  48. int min=0;
  49.  
  50. rhw
  51. }
  52. }*/
  53. int e=0;
  54. for(int i=1;i<=n;++i){
  55. if(pole[hlavny][i]!=0){
  56. e+=enrgiaVetvy(pole,hlavny,i);
  57. }
  58. }
  59. System.out.println(e);
  60. }
  61. }
  62.  
  63. public static int enrgiaVetvy(int [][]paPole,int paZ,int paDo){
  64. int cestaPrisiel=paPole[paDo][paZ];
  65. int cestaZ=0;
  66. for(int i=1;i<paPole.length;++i){
  67. if(paPole[paDo][i]!=0&&i!=paZ){
  68. cestaZ+=enrgiaVetvy(paPole,paDo,i);
  69. }
  70. }
  71. if(cestaZ==0){
  72. return cestaPrisiel;
  73. }
  74. return Math.min(cestaPrisiel, cestaZ);
  75. }
  76. }
  77.