Power Outage
TopCoder SRM 144 DIV 2 1100:
Given a tree-structure graph (not necessary to be binary tree), and each edge has a weight. The graph is represented by an array of tuple: parent, child, weight.
The object is to get the minimum cost for visiting all nodes, starting from the root node.
The key is that each edge needs to be visited twice, if we want to start from the root and stop at the root. However, this problem does not require that we stop at the root, i.e., we can stop at any leaf node, wherever minimizes our total cost. Obviously, the problem is equivalent to find the maximum root-to-leaf path sum.
Following code passes system test.
Given a tree-structure graph (not necessary to be binary tree), and each edge has a weight. The graph is represented by an array of tuple: parent, child, weight.
The object is to get the minimum cost for visiting all nodes, starting from the root node.
The key is that each edge needs to be visited twice, if we want to start from the root and stop at the root. However, this problem does not require that we stop at the root, i.e., we can stop at any leaf node, wherever minimizes our total cost. Obviously, the problem is equivalent to find the maximum root-to-leaf path sum.
Following code passes system test.
Comments
Post a Comment