FQ.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package fq;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author drevenak3
*/
public class FQ {
/**
* @param args the command line arguments
*/
public static void main
(String[] args
) { Scanner scan
= new Scanner
(System.
in); try{
scan
=new Scanner
(new File("in.txt")); int n, u, v, hlavny;
while (scan.hasNext()) {
n = scan.nextInt();
hlavny = scan.nextInt();
int[][] pole = new int[n + 1][n + 1];
for (int i = 1; i <= n-1; i++) {
u = scan.nextInt();
v = scan.nextInt();
int c=scan.nextInt();
pole[u][v] = c;
pole[v][u] = c;
}
/*ArrayList<Integer> rh=new ArrayList<Integer>();
ArrayList<Integer> rhw=new ArrayList<Integer>();
for(int i=1;i<=n;++i){
int st=0;
for(int j=1;j<i;++j){
if(pole[i][j]!=0){
++st;
if(st>=2) break;
}
}
if(st==1){
int min=0;
rhw
}
}*/
int e=0;
for(int i=1;i<=n;++i){
if(pole[hlavny][i]!=0){
e+=enrgiaVetvy(pole,hlavny,i);
}
}
}
}
public static int enrgiaVetvy(int [][]paPole,int paZ,int paDo){
int cestaPrisiel=paPole[paDo][paZ];
int cestaZ=0;
for(int i=1;i<paPole.length;++i){
if(paPole[paDo][i]!=0&&i!=paZ){
cestaZ+=enrgiaVetvy(paPole,paDo,i);
}
}
if(cestaZ==0){
return cestaPrisiel;
}
return Math.
min(cestaPrisiel, cestaZ
); }
}