算法设计与分析: 3-21 有向树k中值问题

3-21 有向树k中值问题


问题描述

给定一棵有向树 T,树 T 中每个顶点 u 都有一个权 w(u);树的每条边(u,v)也都有一个非负边长 d(u,v)。有向树 T 的每个顶点 u 可以看作客户,其服务需求量为 w(u)。每条边(u,v) 的边长 d(u,v) 可以看作运输费用。如果在顶点 u 处未设置服务机构,则将顶点 u 处的服务 需求沿有向树的边(u,v)转移到顶点 v 处服务机构需付出的服务转移费用为 w(u)*d(u,v)。 树根处已设置了服务机构,现在要在树 T 中增设 k 处服务机构,使得整棵树 T 的服务转移费用最小。

对于给定的有向树 T,编程计算在树 T 中增设 k 处服务机构的最小服务转移费用。

数据输入:
第 1 行有 2 个正整数 n 和 k。n 表示有向树 T 的边数; k是要增设的服务机构数。有向树 T 的顶点编号为 0,1,…,n。根结点编号为 0。接下来的 n 行中,每行有表示有向树 T 的一条有向边的 3 个整数。第 i+1 行的 3 个整数 w i , v i , d i 分别表 示编号为 i 的顶点的权为 w i ,相应的有向边为 ( i , v i ) ,其边长为 d i


Java

import java.util.Scanner;

class Node{
    int w,d,dep;
    int[][] cost;
    int[] dis;
    Node parent,left,right;

}

public class YouXiangShuKZhongZhi {

    private static int n,k;
    private static Node[] subt;
    private static int MAX = 100000;

    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int a,b,c;

        while (true){
            n = input.nextInt();
            k = input.nextInt();

            subt = new Node[n+1];

            for(int i=0; i<=n; i++)
                subt[i] = new Node();

            for(int i=1; i<=n; i++){
                a = input.nextInt();
                b = input.nextInt();
                c = input.nextInt();
                subt[i].w = a;
                subt[i].d = c;
                Node p = findc(subt[b]);
                Node q = new Node();
                p.left = subt[i];
                p.right = q;
                subt[i].parent = p;
                q.parent = p;
            }

            sd(subt[0]);

            System.out.println(solution());
        }
    }

    private static int solution(){
        PostOrder(subt[0]);
        return subt[0].cost[k][0];
    }

    private static void PostOrder(Node t){
        if(t != null){
            PostOrder(t.left);
            PostOrder(t.right);
            comp(t);
        }
    }

    private static void comp(Node t){
        if(t.left == null){
            for(int j=0; j<=t.dep; j++)
                t.cost[0][j] = t.w * t.dis[j];
            for(int i=1; i<=k; i++)
                for(int j=0; j<=t.dep; j++)
                    t.cost[i][j] = 0;
        }else
            for(int i=0; i<=k; i++)
                for(int j=0; j<=t.dep; j++){
                    if(i > 0)
                        t.cost[i][j] = t.cost[i-1][0];
                    else
                        t.cost[i][j] = MAX;
                    for(int a=0; a<=i; a++){
                        int tmp = t.left.cost[a][j+1] + t.right.cost[i-a][j+1] + t.w *t.dis[j];
                        if(t.cost[i][j] > tmp)
                            t.cost[i][j] = tmp;
                    }
                }
    }

    private static Node findc(Node q){
        while (q!=null && q.right!=null)
            q = q.right;
        return q;
    }

    private static void sd(Node t){
        PreOrder(t);
    }

    private static void PreOrder(Node t){
        if(t != null){
            adddep(t);
            PreOrder(t.left);
            PreOrder(t.right);
        }
    }

    private static void adddep(Node t){
        if(t.parent != null)
            t.dep = t.parent.dep + 1;
        t.cost = new int[k+1][t.dep+1];
        t.dis = new int[t.dep+1];
        t.dis[0] = 0;
        if(t.parent != null)
            for(int i=1; i<=t.dep; i++)
                t.dis[i] = t.parent.dis[i-1] + t.d;
    }
}

Input & Output

4 2
1 0 1
1 1 10
10 2 5
1 2 3
4

王晓东《计算机算法设计与分析》(第3版)P97

猜你喜欢

转载自blog.csdn.net/ioio_/article/details/81043737