/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author cteam039
*/
public class Fr {
public static void main
(String...
args) { try {
iteration(scan);
Logger.getLogger(Fr.class.getName()).log(Level.SEVERE, null, ex);
}
}
int points, firstPoint;
String line
= scan.
readLine(); while (line != null && line.split(" ").length == 2) {
points
= Integer.
parseInt(line.
split(" ")[0]); firstPoint
= Integer.
parseInt(line.
split(" ")[1]); line = scan.readLine();
Map
<Integer, List
<Struc
>> map
= new HashMap
<Integer, List
<Fr.
Struc>>();
for(int i = 0; i < points - 1; i++) {
int a
= Integer.
parseInt(line.
split(" ")[0]); int b
= Integer.
parseInt(line.
split(" ")[1]); int c
= Integer.
parseInt(line.
split(" ")[2]);
if (map.get(a) == null) {
map.put(a, new ArrayList<Fr.Struc>());
}
map.get(a).add(new Struc(b, c));
if (map.get(b) == null) {
map.put(b, new ArrayList<Fr.Struc>());
}
map.get(b).add(new Struc(a, c));
line = scan.readLine();
}
List<Struc> edges = map.get(firstPoint);
int sum = 0;
for (Struc strc : edges) {
int rec = cutOff(strc.line, firstPoint, map);
sum += rec > strc.weight ? strc.weight : rec;
}
}
}
public static int cutOff
(int node,
int sourceNode, Map
<Integer, List
<Struc
>> map
) { List<Struc> edges = map.get(node);
int sum = 0;
for (Struc strc : edges) {
if (strc.line != sourceNode) {
int nodeW = cutOff(strc.line, node, map);
// System.out.println("nodeW");
sum += strc.weight > nodeW ? nodeW : strc.weight;
}
}
if(sum == 0 ){
}
return sum;
}
public static class Struc {
int line, weight;
public Struc(int line, int weight) {
this.line = line;
this.weight = weight;
}
}
}