import java.util.*;
public class Leetcode {
public static void main(String args[]) {
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
int m=scan.nextInt();
int s=scan.nextInt();
int cost[][]=new int[n+1][n+1];
for(int i=1;i<n+1;i++) {
for(int j=1;j<n+1;j++)
cost[i][j]=Integer.MAX_VALUE;
}
for(int i=0;i<m;i++) {
cost[scan.nextInt()][scan.nextInt()]=scan.nextInt();
}
long dp[]=new long [n+1];
Arrays.fill(dp, Integer.MAX_VALUE);
dp[s]=0;
Queue<node> queue=new PriorityQueue();
queue.offer(new node(0,s));
while(!queue.isEmpty()) {
node t=queue.poll();
int v=t.num;
if(dp[v]<t.first) continue;
for(int i=1;i<n+1;i++) {
if(cost[v][i]!=Integer.MAX_VALUE) {
dp[i]=Math.min(dp[i],dp[v]+cost[v][i]);
que
java最短路径迪杰斯特拉算法(最小堆优化版)
最新推荐文章于 2023-03-12 16:11:39 发布
本文详细介绍了如何使用Java实现Dijkstra算法,并通过引入最小堆进行优化,以提高寻找图中节点间最短路径的效率。内容包括算法原理、Java代码实现及优化细节。
摘要由CSDN通过智能技术生成