Dijkstra算法实例

import java.util.Arrays;
import java.util.Scanner;

/**
 * Dijkstra 算法
 * 图存储: 邻接矩阵
 * 图类型:无向有权图
 */
public class Main2 {

    static int Nv, Ne;
    static int[][] G;
    static final int MAX = 101;
    static Scanner cin = new Scanner(System.in);

    static void buildGraph() {
        Nv = cin.nextInt();
        Ne = cin.nextInt();
        G = new int[Nv+1][Nv+1];

        for(int i = 1; i <= Nv; i++){
            Arrays.fill(G[i], MAX);
        }
        for(int i = 1; i <= Ne; i++){
            int start, end, w;
            start = cin.nextInt();
            end = cin.nextInt();
            w = cin.nextInt();
            G[start][end] = w;
            G[end][start] = w;
        }
    }

    static boolean dijkstra(int start,int[] dist, int[] path) {

        boolean[] visit = new boolean[Nv +1];

        for (int i = 1; i <= Nv; i++) {
            dist[i] = G[start][i];
            path[i] = start;
        }
        // 收录 start
        visit[start] = true;
        dist[start] = 0;
        int count = 1;
        while(true){
            int v = distMin(dist, visit);
            if(v == -1){
                break;
            }
            visit[v] = true;
            count++;  // 记录收录顶点的个数

            for(int i =1; i <= Nv; i++) {
                if(visit[i] == false && G[v][i] < MAX){ // 是 v的邻接点,并未访问过
                    if(dist[i] > G[v][i] + dist[v]) {
                        dist[i] = G[v][i] + dist[v];
                        path[i] = v;
                    }
                }
            }

        }
        // 图不连通
        if (count != Nv) {
            return false;
        }else{
            return true;
        }

    }

    static int distMin(int[] dist, boolean[] v) {
        int min = MAX, minIndex = -1;
        for(int i = 1; i <= Nv; i++) {
            if(v[i] == false && dist[i] < min){
                min = dist[i];
                minIndex = i;
            }
        }
        return minIndex;
    }

    public static void main(String[] args) {
        buildGraph();
        int[] dist = new int[Nv + 1];
        int[] path = new int[Nv + 1];
        int start = 1;
        dijkstra(start, dist, path);
        
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值