Fr.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package fr;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
/**
*
* @author cteam025
*/
class Edge{
public Node n1;
public Node n2;
public int price;
public Node dejSouseda(Node ja){
if(ja==n1){
return n2;
}
return n1;
}
}
class Node{
public int id = 0;
public ArrayList
<Edge
> hrany
= new ArrayList(); public Node parent;
public int getEffort(Node parent, int parentPrice){
int price = 0;
for(Edge h : hrany){
if(parent == h.dejSouseda(this)){
continue;
}
// System.out.println(h.dejSouseda(this).id);
price += h.dejSouseda(this).getEffort(this, h.price);
}
if (parent == null){
return price;
}
int thisprice = parentPrice;
if(price == 0 || price > thisprice){
return thisprice;
}
return price;
}
public Node(int id){
this.id = id;
}
}
class Graph{
public Node root;
public int nodes;
public void addEdge(int f, int t, int price){
Node node1
= uzly.
get(Integer.
valueOf(f
)); Node node2
= uzly.
get(Integer.
valueOf(t
)); if(node1==null){
node1 = new Node(f);
uzly.
put(Integer.
valueOf(f
), node1
); }
if(node2==null){
node2 = new Node(t);
uzly.
put(Integer.
valueOf(t
), node2
); }
Edge hrana = new Edge();
hrana.price = price;
hrana.n1 = node1;
hrana.n2 = node2;
node1.hrany.add(hrana);
node2.hrany.add(hrana);
}
}
public class Fr {
/**
* @param args the command line arguments
*/
// TODO code application logic here
int n
= Integer.
parseInt(line.
split(" ")[0]); int rootId
= Integer.
parseInt(line.
split(" ")[1]);
Node root = new Node(rootId);
Graph g = new Graph();
g.nodes = n;
g.root = root;
g.
uzly.
put(Integer.
valueOf(rootId
), root
); for(int i=0;i<(n-1);i++){
line = in.readLine();
String params
[] = line.
split(" "); int f
= Integer.
parseInt(params
[0]); int t
= Integer.
parseInt(params
[1]); int price
= Integer.
parseInt(params
[2]); g.addEdge(f,t,price);
}
System.
out.
println(g.
root.
getEffort(null,
0));
}
}