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;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值