撒斯泰克大学lab10a题

题意

就是用prim algorithm求出最小生成树的total weight

idea

使用邻接链表储存邻接节点。

1.首先将第一个节点加入最小生成树的节点集合A中,这里第一个加入的节点可以随意,不必是图最短边的节点。用TotalCost储存与集合A相邻的顶点的最小权值,0表示该结点已经在MST中,如果没有边连接的话,就是infinite。
2. 对于剩下的N-1个节点,就是循环N-1次,每一次找到与集合A相邻的但不在集合A中的权重最小的点nVertex,将它加入到集合A中,同时更新totalCoast。
3. 每一次循环找到的最小权值累加起来就是答案

code

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        AReader in = new AReader();
        AWriter output = new AWriter();
        int n=in.nextInt();
        int m=in.nextInt();
//        AbstractMap.SimpleEntry<Integer, Integer> entry= new AbstractMap.SimpleEntry<>(1,1);
        ArrayList<node>[]adjacency=new ArrayList[n];
        for (int i = 0; i < n; i++) {
            adjacency[i]=new ArrayList();
        }
        for (int i = 0; i < m; i++) {
            int u=in.nextInt();
            int v=in.nextInt();
            int w=in.nextInt();
            adjacency[u-1].add(new node(v-1,w));
            adjacency[v-1].add(new node(u-1,w));
        }
        int result=Prim(n,adjacency);
        output.println(result);
        output.close();
    }
    public static int Prim(int N,ArrayList<node>[] graph){
        int nCost = 0;
        ArrayList<Integer> MSTSet=new ArrayList();// 储存MST的结点

        int[] TotalCost=new int[N];// 储存与集合A相邻的顶点的最小权值,0表示该结点已经在MST中
        MSTSet.add(0);// 将结点1加入MST
        TotalCost[0] = 0;
        for(int i = 1; i <N; i++)// 初始化,切记要将除1以外的都置为INF
        {
            TotalCost[i] = Integer.MAX_VALUE;
        }
        for(int i = 0; i < graph[0].size(); i++)// 处理与结点1相连的顶点
        {
            TotalCost[graph[0].get(i).vertex] = graph[0].get(i).weight;
        }

        for(int i = 1; i <= N - 1; i++)// 剩余N-1个顶点,循环N-1次
        {
            int nVertex = 0, nWeight = Integer.MAX_VALUE;// 用于寻找最短的边
            for(int j = 0; j < N; j++)
            {
                if(nWeight > TotalCost[j] && TotalCost[j] != 0)
                {
                    nVertex = j;
                    nWeight = TotalCost[j];
                }
            }

            TotalCost[nVertex] = 0;

            MSTSet.add(nVertex);// 将节点nVertex加入MST

            nCost += nWeight;// 计算MST的费用

            for(int j = 0; j < graph[nVertex].size(); j++)// 更新pCost数组
            {
                if(TotalCost[graph[nVertex].get(j).vertex] != 0 &&
                        TotalCost[graph[nVertex].get(j).vertex] > graph[nVertex].get(j).weight)
                {
                    TotalCost[graph[nVertex].get(j).vertex]= graph[nVertex].get(j).weight;

                }

            }

        }
return nCost;
//        System.out.println(nCost);


    }
}
class node{
    int vertex;
    int weight;
    public node(int vertex, int weight) {
        this.vertex = vertex;
        this.weight = weight;
    }
}
class AReader {
    private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private StringTokenizer tokenizer = new StringTokenizer("");

    private String innerNextLine() {
        try {
            return reader.readLine();
        } catch (IOException ex) {
            return null;
        }
    }

    public boolean hasNext() {
        while (!tokenizer.hasMoreTokens()) {
            String nextLine = innerNextLine();
            if (nextLine == null) {
                return false;
            }
            tokenizer = new StringTokenizer(nextLine);
        }
        return true;
    }

    public String nextLine() {
        tokenizer = new StringTokenizer("");
        return innerNextLine();
    }

    public String next() {
        hasNext();
        return tokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }
}

class AWriter implements Closeable {
    private BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public void print(Object object) throws IOException {
        writer.write(object.toString());
    }

    public void println(Object object) throws IOException {
        writer.write(object.toString());
        writer.write("\n");
    }

    @Override
    public void close() throws IOException {
        writer.close();
    }
}


期末大作业基于python的足球运动员数据分析源码+数据集(高分项目),个人经导师指导并认可通过的高分设计项目,评审分98分,项目中的源码都是经过本地编译过可运行的,都经过严格调试,确保可以运行!主要针对计算机相关专业的正在做大作业、毕业设计的学生和需要项目实战练习的学习者,资源项目的难度比较适中,内容都是经过助教老师审定过的能够满足学习、使用需求,如果有需要的话可以放心下载使用。 期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于pyth
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值