Dijkstra算法
public int networkDelayTime(int[][] times, int n, int k) { int max=Integer.MAX_VALUE/2; int[][] g=new int[n][n]; for(int i=0;i<n;i++) Arrays.fill(g[i],max); int[] dist=new int[n]; Arrays.fill(dist,max); boolean[] vis=new boolean[n]; for(int[] time:times){ g[time[0]-1][time[1]-1]=time[2]; } //----------------------------------------------- dist[k-1]=0; for(int i=0;i<n;i++){ int x=-1; for(int j=0;j<n;j++){ if(!vis[j]&&(x==-1||dist[j]<dist[x])){ x=j; } } vis[x]=true; for(int j=0;j<n;j++) dist[j]=Math.min(dist[j],dist[x]+g[x][j]); } //----------------------------------------------- int ans=Arrays.stream(dist).max().getAsInt(); return ans==max?-1:ans; }