OJ贪心#257. 树的颜色

题目描述

​ 有一棵树,它的所有节点都需要染色,每个节点都有一个代价基础值 Ci

。第一个染色的是根节点,其余节点染色的时候其父节点必须已染色。染色一个节点会用掉一个时间单位,每个节点染色的代价是染完此节点时的总时间 T 乘上这个节点的基础值 Ci

。求染完所有节点所需的最小代价。


输入

​ 第一行包含两个整数 N,R

其中,N 是树中节点个数,R

是根节点编号。

​ 第二行输入 N

个整数,编号为 i 的节点的代价基础值 Ci。(1≤Ci≤500)

​ 接下来 N−1

行为边的信息,每行两个数分别表示父节点编号和子节点编号。

输出

​ 输出一个整数,表示最小代价。


样例输入

5 1
1 2 1 2 4
1 2
1 3
2 4
3 5

样例输出

33

数据规模与约定

​ 时间限制:1 s

​ 内存限制:256 M

​ 100% 的数据保证 1≤R≤N≤1000

import java.util.*;

public class oj_treeColor {
    public static void main(String[] args){
        Scanner scan=new Scanner(System.in);
        int n=scan.nextInt(),r=scan.nextInt();
        int[] weight=new int[n];
        for(int i=0;i<n;i++){
            weight[i]=scan.nextInt();
        }
        int[][] relation=new int[n-1][];
        for(int i=0;i<n-1;i++){
            relation[i]=new int[2];
            for(int j=0;j<2;j++){
                relation[i][j]=scan.nextInt();
            }
        }
        MiniWeightSum miniWeightSum=new MiniWeightSum(weight,relation);
        System.out.println(miniWeightSum.outc);
        scan.close();
    }
}
class MiniWeightSum{
    int[] weight;
    int[][] relation;
    Map<Integer,Node> hashMap;
    Boolean[] visited;
    int outc=0,cnt;
    MiniWeightSum(int[] weight,int[][] relation){
        this.relation=relation;
        this.weight=weight;
        solve();
    }
    void solve(){
        hashMap=new HashMap<>();
        hashMap.put(1,new Node(weight[0],1,0));
        for (int i=0;i<relation.length;i++){    //hashmap初始化
            int seq=relation[i][1];
            int p=relation[i][0];
            int w=weight[seq-1];
            hashMap.put(seq,new Node(w,seq,p));
            Node pn=hashMap.get(p);
            pn.setChild(seq+"");
            sumofChildWeight(p,w);//计算每棵子树总权值
        }
        visited=new Boolean[relation.length+2];
        Arrays.fill(visited,false);
        cnt=1;
        outc=0;
        countMiniSum(1);
    }
    void countMiniSum(int root){
        visited[root]=true;
        Node now=hashMap.get(root);
        String s=now.child;
        outc+=cnt*hashMap.get(root).weight;
        cnt++;
        if(s=="")   return;
        for(int j=0;j<s.length();j++){
            int tag=0,index=0;
            for(int i=0;i<s.length();i++){
                int cntWeight=hashMap.get(s.charAt(i)-'0').cntWeight;
                if(!visited[s.charAt(i)-'0']&&cntWeight>tag){
                    tag=cntWeight;
                    index=s.charAt(i)-'0';
                }
            }
            countMiniSum(index);
        }
    }
    void sumofChildWeight(int current,int weight){
        if(current==0) return;
        Node now=hashMap.get(current);
        now.addWeight(weight);
        int parent=now.parent;
        sumofChildWeight(parent,weight);
    }
    static class Node{
        int weight,seq,parent,cntWeight;
        String child;
        Node(int weight,int seq,int parent){
            this.weight=weight;
            this.seq=seq;
            this.parent=parent;
            cntWeight=weight;
            child="";
        }
        void setChild(String s){
            child+=s;
        }
        void addWeight(int n){
            cntWeight+=n;
        }
    }
}

 

### Java 编译与运行 OJ 系统中 `.class` 文件不存在解决方案 #### 1. 检查源代码路径和编译命令 确保在本地环境中使用的源代码路径以及编译命令正确无误。如果是在Eclipse或其他IDE中开发,则需确认项目结构设置合理,源文件位于正确的目录下[^1]。 ```bash javac -d . HelloWorld.java ``` 上述命令表示将编译后的`.class`文件放置于当前工作目录(`.`),并指定要编译的Java源文件为`HelloWorld.java`。 #### 2. 验证类名定义准确性 检查是否存在拼写错误或大小写不匹配的情况。例如,在给定的例子中: ```java // 错误版本 System.out.println("Hello World"); // 正确版本应改为如下所示: System.out.printLn("Hello World"); ``` 注意区分字母大小写及方法名称书写规范性[^3]。 #### 3. 处理多文件依赖关系 当涉及多个相互关联的Java源文件时,务必保证所有必要的`.java`文件都已成功编译,并且生成的目标`.class`文件存在于预期位置。特别是对于嵌套内部类的情形,可能还需要额外处理其对应的`.class`文件命名规则变化问题[^4]。 #### 4. 清除缓存重新构建项目 有时由于环境配置不当或者残留旧版编译产物的影响,可能会导致新修改未能生效。此时建议清理整个项目的临时数据后再试一次完整的编译流程。 #### 5. 调整OJ平台提交方式 部分在线评测系统(OJ)对上传代码有特定的要求,比如某些情况下只允许直接粘贴纯文本形式的源码而非附件压缩包等形式;另外还需留意是否有关于入口函数签名等方面的特殊规定。 #### 6. 排查输入/输出流相关异常 针对涉及到文件读写的场景,应当仔细审查所使用的I/O API调用逻辑及其参数传递情况,防止因资源未关闭等原因引发潜在隐患[^2]。 #### 7. 定位具体报错信息 面对诸如“找不到符号”的提示时,应该依据具体的上下文线索去定位实际发生的位置,进而针对性地修正相应变量声明等问题[^5]。 ```java BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String line; while ((line = reader.readLine()) != null){ System.out.println(line); } reader.close(); ``` 以上代码展示了如何安全地从标准输入获取字符串直至结束标志出现为止,同时记得释放占用的外部连接。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值