Java的迪杰斯特拉算法(朴素版本and堆优化版本)基于Acwing849,850

迪杰斯特拉算法的两种写法,分别对应稀疏图和稠密图,当我们存储稠密图时用邻接矩阵进行存储,当我们存储系数图时用邻接表进行存储,朴素版本时间复杂度O(n^2),堆优化版本时间复杂度为O(mlog2n)

import java.io.BufferedReader;
import java.io.InputStreamReader;
//朴素dij的时间复杂度为O(n^2),适用于稠密图,用邻接矩阵存储
public class 朴素DIJ {
	public static int N = 510,n,m,INF=0x3f3f3f3f;
	static int[] dis=new int[N],st = new int[N];//dis存放从1到n的距离,st存放是否已经确定从1到n的最短距离
	static int[][] g = new int[N][N];//存放边的信息
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] s = br.readLine().split(" ");
        n = Integer.valueOf(s[0]);
        m = Integer.valueOf(s[1]);
        for(int i=0;i<N;i++){
            dis[i]=INF;
            for(int j=0;j<N;j++){
                g[i][j]=INF;
            }
        }//初始化dis和g都设为无穷大
        for(int i=0;i<m;i++){
            String[] s1 = br.readLine().split(" ");
            int x = Integer.valueOf(s1[0]);
            int y = Integer.valueOf(s1[1]);
            int w = Integer.valueOf(s1[2]);
            g[x][y] = Math.min(g[x][y],w);
        }//如果存在自环或者重边选择权值最小的
        System.out.println(Dij());
        
    }
    public static int Dij(){
        dis[1]=0;
        for(int i=0;i<n-1;i++){//只操作n-1次,最后一个点不需要操作
            int t=-1;
            for(int j=1;j<=n;j++){
                if(st[j]!=1&&(t==-1||dis[j]<dis[t])) t=j;
            }//找到需要当前距离最小的点
       
            for(int j=1;j<=n;j++) dis[j] = Math.min(dis[j],dis[t]+g[t][j]);//更新当前的距离
            st[t]=1;//保存已经找到最短路的状态

        }
        if(dis[n]==INF) return -1;
        else return dis[n];
    }
}
import java.io.*;
import java.util.*;
//堆优化后的用优先队列对每个入队的点进行排序
public class 堆优化DIJ {
	public static int N = 150010,n,m,INF=0x3f3f3f3f,idx;
	static int[] dis=new int[N],st = new int[N],h=new int[N],val = new int[N*2],ne = new int[N*2],we = new int[N*2];
	static PriorityQueue<Node> q = new PriorityQueue<Node>();
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] s = br.readLine().split(" ");
        n = Integer.valueOf(s[0]);
        m = Integer.valueOf(s[1]);
        for(int i=0;i<N;i++){
            dis[i]=INF;
            h[i]=-1;
        }
        for(int i=0;i<m;i++){
            String[] s1 = br.readLine().split(" ");
            int x = Integer.valueOf(s1[0]);
            int y = Integer.valueOf(s1[1]);
            int w = Integer.valueOf(s1[2]);
            add(x,y,w);
        }
        System.out.println(Dij());
        
    }
    public static void add(int a,int b,int c){//数组模拟链表
        val[idx] = b;
        ne[idx] = h[a];
        we[idx] = c;//we数组记录每个边的权值
        h[a]=idx++;
    }
    public static int Dij(){
        dis[1]=0;
        q.add(new Node(0,1));
        while(!q.isEmpty()){
            Node temp = q.poll();
            int dd = temp.w;
            int v = temp.val;
            if(st[v]==1) continue;
            st[v]=1;
            for(int i=h[v];i!=-1;i=ne[i]){
                int j=val[i];
                if(dis[j]>dd+we[i]) {
                    dis[j] = dd+we[i];
                    q.add(new Node(dis[j],j));
                }
            }
        }
        if(dis[n]==INF) return -1;
        else return dis[n];
    }
        }
class Node implements Comparable<Node>{//Node的w是点到1的距离,val是点的编号
    public int w;
    public int val;
    public Node(int w,int val){
        this.w=w;
        this.val = val;
    }
    @Override
    public int compareTo(Node o){
        if(this.w>o.w) return 1;//降序
        if(this.w==o.w) return 0;
        else return -1;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值