POJ2054.Color a Tree

试题请参见: http://poj.org/problem?id=2054
Also available on: http://acm.hdu.edu.cn/showproblem.php?pid=1055

题目概述

Bob is very interested in the data structure of a tree. A tree is a directed graph in which a special node is singled out, called the “root” of the tree, and there is a unique path from the root to each of the other nodes.

Bob intends to color all the nodes of a tree with a pen. A tree has N nodes, these nodes are numbered 1, 2, …, N. Suppose coloring a node takes 1 unit of time, and after finishing coloring one node, he is allowed to color another. Additionally, he is allowed to color a node only when its father node has been colored. Obviously, Bob is only allowed to color the root in the first try.

Each node has a “coloring cost factor”, Ci. The coloring cost of each node depends both on Ci and the time at which Bob finishes the coloring of this node. At the beginning, the time is set to 0. If the finishing time of coloring node i is Fi, then the coloring cost of node i is Ci * Fi.

For example, a tree with five nodes is shown in Figure-1. The coloring cost factors of each node are 1, 2, 1, 2 and 4. Bob can color the tree in the order 1, 3, 5, 2, 4, with the minimum total coloring cost of 33.

Problem Image

Given a tree and the coloring cost factor of each node, please help Bob to find the minimum possible total coloring cost for coloring all the nodes.

解题思路

(过几个小时来填坑)

源代码

import java.util.Scanner;

public class Main {
    private class Node {
        public Node(int cost) {
            this.cost = cost;
            parentIndex = -1;
        }

        public int cost;
        public int parentIndex;
    }

    public int getMinCost(Node[] nodes, int rootIndex) {
        int n = nodes.length, totalCost = 0;
        boolean[] isVisited = new boolean[n];
        int[] totalCosts = new int[n];
        int[] totalNodes = new int[n];

        for ( int i = 0; i < n; ++ i ) {
            totalCosts[i] = nodes[i].cost;
            totalNodes[i] = 1;
        }
        for ( int i = 1; i < n; ++ i ) {
            int k = getBestNodeIndex(isVisited, totalCosts, totalNodes, rootIndex);
            int p = nodes[k].parentIndex;
            while ( isVisited[p] ) {
                p = nodes[p].parentIndex;
            }

            isVisited[k] = true;
            totalCost += totalCosts[k] * totalNodes[p];

            totalCosts[p] += totalCosts[k];
            totalNodes[p] += totalNodes[k];
        } 
        return totalCost + totalCosts[rootIndex];
    }

    private int getBestNodeIndex(boolean[] isVisited, 
        int[] totalCosts, int[] totalNodes, int rootIndex) {
        int bestIndex = 0;
        double bestValue = 0;

        for ( int i = 0; i < isVisited.length; ++ i ) {
            if ( isVisited[i] || i == rootIndex ) {
                continue;
            }

            double currentValue = (double)totalCosts[i] / totalNodes[i];
            if ( currentValue > bestValue ) {
                bestValue = currentValue;
                bestIndex = i;
            }
        }
        return bestIndex;
    }

    public static void main(String[] args) {
        Main s = new Main();
        Scanner in = new Scanner(System.in);

        while ( in.hasNext() ) {
            int n = in.nextInt(), r = in.nextInt();
            Node[] nodes = new Node[n];

            if ( n == 0 && r == 0 ) {
                break;
            }
            for ( int i = 0; i < n; ++ i ) {
                int cost = in.nextInt();
                nodes[i] = s.new Node(cost);
            }
            for ( int i = 1; i < n; ++ i ) {
                int v1 = in.nextInt(), v2 = in.nextInt();
                nodes[v2 - 1].parentIndex = v1 - 1;
            }
            System.out.println(s.getMinCost(nodes, r - 1));
        }
        in.close();
    }
}

参考资料

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值