Main.java
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
*
* @author cteam049
*/
public class Main {
static class Node {
int value;
int force;
public Node(int value) {
this.value = value;
}
}
private static boolean appendNode(Node node, int i1, int i2, int i3) {
if (node.value == i1) {
Node n = new Node(i2);
n.force = i3;
node.sucs.put(i2, n);
return true;
} else if (node.value == i2) {
Node n = new Node(i1);
n.force = i3;
node.sucs.put(i1, n);
return true;
} else if (node.sucs.containsKey(i1)) {
return appendNode(node.sucs.get(i1), i1, i2, i3);
} else if (node.sucs.containsKey(i2)) {
return appendNode(node.sucs.get(i2), i1, i2, i3);
} else {
for (Node tmp : node.sucs.values()) {
if (appendNode(tmp, i1, i2, i3)) return true;
}
}
return false;
}
private static void preorder(Node node) {
System.
out.
println(node.
value); for (Node n : node.sucs.values()) {
preorder(n);
}
}
private static int solve(Node node) {
if (node.sucs.isEmpty()) {
return node.force;
}
int sum = 0;
for (Node n : node.sucs.values()) {
sum += solve(n);
}
if (sum < node.force) {
return sum;
}
return node.force;
}
public static void main
(String[] args
) { Scanner sc
= new Scanner
(System.
in); int n, c;
while (sc.hasNext()) {
n = sc.nextInt();
c = sc.nextInt();
Node n1 = new Node(c);
for (int i=0 ; i<n-1 ; i++) {
int i1 = sc.nextInt();
int i2 = sc.nextInt();
int i3 = sc.nextInt();
appendNode(n1, i1, i2, i3);
}
// preorder(n1);
System.
out.
println(solve
(n1
)); }
}
}