Problem:
Height of a Tree

Given a tree T with non-negative weight, find the height of each node of the tree. For each node, the height is the distance to the most distant leaf from the node.

Input

n
s1 t1 w1
s2 t2 w2
:
sn-1 tn-1 wn-1

The first line consists of an integer n which represents the number of nodes in the tree. Every node has a unique ID from 0 to n-1 respectively.

In the following n-1 lines, edges of the tree are given.

si and ti represent end-points of the i-th edge (undirected) and wi represents the weight (distance) of the i-th edge.

Output

The output consists of n lines. Print the height of each node 0, 1, 2, ..., n-1 in order.

Constraints

1 ≤ n ≤ 10,000

0 ≤ wi ≤ 1,000

Sample Input 1

4
0 1 2
1 2 1
1 3 3

Sample Output 1

5
3
4
5

Solution in JAVA (judge verdict: Wrong Answer)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		
		int n = in.nextInt();
		init(n);
		for (int i=0;i<n-1;i++) {
			int s = in.nextInt();
			int t = in.nextInt();
			int w = in.nextInt();
			adList[s].add(new P(t, w));
			adList[t].add(new P(s, w));
		}
		long[] height = height(n);
		for (int i=0;i<n;i++) {
			System.out.println(height[i]);
		}
	}
	
	///
	static List<P>[] adList;
	static long[] dist;
	static long[] height(int n) { // n is number of nodes
		long[] heights = new long[n];
		Arrays.fill(heights, Long.MIN_VALUE);
		for (int i=0;i<n;i++) {
			if (heights[i]>Long.MIN_VALUE) continue;
			dijkstra(i);
			long furthest = -1;
			int furthestIdx = -1;
			for (int j=0;j<n;j++) {
				if (furthest<dist[j]) {
					furthest = dist[j];
					furthestIdx = j;
				}
			}
			heights[i] = furthest;
			heights[furthestIdx] = furthest;
			Arrays.fill(dist, Long.MAX_VALUE);
		}
		return heights;
	}
	static void dijkstra(int s) { // s is start
		dist[s] = 0L;
		PriorityQueue<P> q = new PriorityQueue<P>();
		q.add(new P(s, 0L));
		
		while (!q.isEmpty()) {
			P cur = q.poll();
			if (dist[cur.index] < cur.dist)
				continue;
			for (P next : adList[cur.index]) {
				if (dist[next.index] > dist[cur.index] + next.dist) {
					dist[next.index] = dist[cur.index] + next.dist;
					q.add(new P(next.index, dist[next.index]));
				}
			}
		}
	}
	@SuppressWarnings("unchecked")
	static void init(int n) {
		adList = new List[n];
		for (int i=0;i<n;i++) {
			adList[i] = new ArrayList<P>();
		}
		dist = new long[n];
		Arrays.fill(dist, Long.MAX_VALUE);
	}
	static class P implements Comparable<P> {
		int index; long dist;
		public P(int index, long dist) {this.index = index; this.dist = dist;}
		public int compareTo(P p) {return this.dist < p.dist ? -1 : this.dist == p.dist ? 0 : 1;}
		public String toString() {return index+","+dist;}
	}
	// in adList, P.dist is distance from adjacent point
	// in q, P.dist is shortest path distance from start point
}