SW练习_最短路径生成树_迪杰斯特拉

本文详细探讨了使用迪杰斯特拉算法求解图中节点间最短路径的方法,通过实例解析算法步骤,并提供了伪代码和实际代码实现,帮助读者深入理解这一经典路径搜索算法。
摘要由CSDN通过智能技术生成
package com.company.real;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;
public class ZuiduanLujingShu_20200731 {
    static NodePro0731[] nodes;
    static long lencount;
    static int pid[];
    static int shortWeight[];
    public static void main(String[] args) throws Exception{
        System.setIn(new FileInputStream("C:\\Users\\XAGDC\\Desktop\\sw\\PRO\\58Pro 最短路径生成树 The Shortest Path Spanning Tree\\eval_input.txt"));
        BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer(reader.readLine());
        int T=Integer.parseInt(st.nextToken());
        for (int zz = 0; zz <T ; zz++) {
            long startTime=System.currentTimeMillis();
            st=new StringTokenizer(reader.readLine());
            int N=Integer.parseInt(st.nextToken());//N个顶点
            int M=Integer.parseInt(st.nextToken());//M条边
            int S=Integer.parseInt(st.nextToken());//S是起点
            nodes=new NodePro0731[N];
            shortWeight=new int[N];
            pid=new int[N];
            for (int i = 0; i <nodes.length ; i++) {
                nodes[i]=new NodePro0731();
            }
            for (int i = 0; i <M ; i++) {
                st=new StringTokenizer(reader.readLine());
                int start=Integer.parseInt(st.nextToken());
                int end=Integer.parseInt(st.nextToken());
                int weight=Integer.parseInt(st.nextToken());
                nodes[start].sonList.add(new int[]{end,weight});
                nodes[end].sonList.add(new int[]{start,weight});
            }
            process(S);
            lencount=0;
            for (int i = 0; i <nodes.length ; i++) {
                lencount=lencount+shortWeight[i];
            }
            //System.out.printf("pid: %s \n", Arrays.toString(pid));
            //System.out.printf("shortWeight: %s \n", Arrays.toString(shortWeight));
            System.out.printf("#%d %d\n",(zz+1),lencount);
            //System.out.println(System.currentTimeMillis()-startTime);
        }
        reader.close();
    }
    public static void process(int startIndex){
        PriorityQueue<Integer> pq=new PriorityQueue<Integer>();
        pq.add(startIndex);
        nodes[startIndex].len=0;
        nodes[startIndex].shortWeight=0;
        nodes[startIndex].pid=startIndex;
        while(!pq.isEmpty()){
            int nodeIndex=pq.poll();
            NodePro0731 node=nodes[nodeIndex];
            for (int[] tolen:node.sonList ) {
                if(nodes[tolen[0]].len > (node.len+tolen[1])){
                    nodes[tolen[0]].len = (node.len+tolen[1]);
                    shortWeight[tolen[0]]=tolen[1];
                    pid[tolen[0]]=nodeIndex;
                    pq.add(tolen[0]);
                }else if(nodes[tolen[0]].len == (node.len+tolen[1])){//距离相同的情况,要使用到达距离最近的节点
                    if(shortWeight[tolen[0]] >tolen[1]){
                        shortWeight[tolen[0]]=tolen[1];
                        pid[tolen[0]]=nodeIndex;
                    }
                }
            }
        }
    }
}
class NodePro0731{
    public List<int[]> sonList;
    public int len;
    public int shortWeight;
    public int pid;
    public NodePro0731(){
        this.sonList=new ArrayList<>();
        len=2087654321;
    }
}

 

迪杰斯特拉算法(Dijkstra)是一种贪心算法,用于解决最短路径问题。它可以处理有权有向图或无向图,但不允许有负权边(权重必须为非负数)。 算法思路: 1. 从起点开始,初始化所有节点的距离为无穷大,起点距离为0; 2. 将起点加入“已访问”集合; 3. 对于起点的所有邻居节点,更新它们的距离(如果通过当前节点到达邻居节点的距离小于邻居节点原有的距离,则更新邻居节点的距离); 4. 从未访问集合中选择距离起点最近的节点,加入“已访问”集合; 5. 重复步骤3和4,直到所有节点都被加入“已访问”集合或者没有与起点相连的节点。 算法实现: Dijkstra算法的实现通常使用优先队列(PriorityQueue)来维护未访问集合中距离起点最近的节点。具体实现步骤如下: 1. 创建一个空的优先队列Q,将起点加入Q中,并设置起点到自身的距离为0; 2. 创建一个数组dist[],用于保存起点到各个节点的距离,初始化为无穷大; 3. 创建一个数组visited[],用于标记节点是否被访问过,初始化为false; 4. 将dist[起点]的值设置为0; 5. 当Q不为空时,重复以下步骤: a. 从Q中取出距离起点最近的节点u; b. 如果节点u已经被访问过,则跳过此次循环; c. 将节点u标记为已访问; d. 对于节点u的每个邻居节点v,如果节点v未被访问过并且通过节点u到达节点v的距离小于dist[v],则更新dist[v]的值; e. 将节点v加入Q中。 6. 最终,dist数组中保存的就是起点到各个节点的最短距离。 Dijkstra算法的时间复杂度为O(ElogV),其中E为边数,V为节点数。这是因为算法需要对每个节点的所有邻居节点进行遍历,而优先队列的插入和删除操作的时间复杂度为O(logV)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值