There are N network nodes, labeled 1 to N.
Given a list of times, it indicates the transit time of the signal through the directed edge. times [i] = (u, v, w), where u is the source node, v is the target node, and w is the time for a signal to pass from the source node to the target node.
Now, we send a signal to the current node K. How long does it take for all nodes to receive the signal? If all nodes cannot be signaled, -1 is returned.
note:
The range of N is between [1, 100].
The range of K is between [1, N].
The length of times is between [1, 6000].
All edges times [i] = (u, v, w) have 1 <= u, v <= N and 0 <= w <= 100.
Source: LeetCode
Link: https://leetcode-cn.com/problems/network-delay-time
Copyright belongs to Deduction Network. Please contact the official authorization for commercial reprint, and please indicate the source for non-commercial reprint.
Dijkstra algorithm implementation
class Solution {
//自己实现的Map
class MyMap{
int[] arr;
MyMap(int size){
arr=new int[size];
for(int i=0;i<size;i++)
arr[i]=-1;
}
void put(int key,int value){
arr[key]=value;
}
int get(int key){
return arr[key];
}
boolean containsKey(int key){
return arr[key]!=-1;
}
void remove(int key){
arr[key]=-1;
}
}
//索引最小堆
class MinHeap {
Edge[] heap;
int size;
MyMap map;
public MinHeap(int size) {
heap=new Edge[size+1];
this.size=0;
map=new MyMap(size+1);
}
private void swap(int p1,int p2) {
Edge t=heap[p1];
heap[p1]=heap[p2];
heap[p2]=t;
}
private void sink(int pos) {
while(2*pos<=size){
int j=2*pos;
if(j<size&&heap[j].weight>heap[j+1].weight)
j++;
if(heap[pos].weight>heap[j].weight)
break;
swap(pos,j);
pos=j;
}
map.put(heap[pos].end,pos);
}
private void swim(int pos) {
while(pos>1&&heap[pos/2].weight>heap[pos].weight){
swap(pos/2,pos);
pos=pos/2;
}
map.put(heap[pos].end,pos);
}
public void add(int end,int weight) {
heap[++size]=new Edge(end,weight);
swim(size);
}
public Edge delMin() {
Edge t=heap[1];
swap(1,size--);
map.remove(t.end);
sink(1);
return t;
}
public void change(int vertex,int weight) {
int pos=map.get(vertex);
heap[pos].weight=weight;
swim(pos);
}
public boolean isEmpty() {
return size==0;
}
}
class Edge {
int end;
int weight;
public Edge(int e,int w) {
this.end=e;
this.weight=w;
}
}
public class Graph {
public ArrayList<Edge>[] adj;
private int v;
private int e;
public Graph(int v) {
adj=new ArrayList[v];
for(int i=0;i<v;i++)
adj[i]=new ArrayList<Edge>();
this.v=v;
}
public void addEdge(int v1,int v2,int w) {
e++;
adj[v1].add(new Edge(v2,w));
}
}
private Graph build(int[][] times,int N){
Graph graph=new Graph(N+1);
for(int[] temp:times)
graph.addEdge(temp[0],temp[1],temp[2]);
return graph;
}
class Dijkstra{
int start;
int[] distTo;
ArrayList<Edge>[] adj;
MinHeap heap;
public Dijkstra(int V,Graph graph,int start) {
distTo=new int[V+1];
for(int i=1;i<=V;i++)
distTo[i]=Integer.MAX_VALUE;
this.adj=graph.adj;
this.start=start;
heap=new MinHeap(V);
distTo[start]=0;
heap.add(start,0);
while(!heap.isEmpty()) {
int v=heap.delMin().end;
relax(v);
}
}
private void relax(int v) {
for(Edge e:adj[v]) {
int w=e.end;
if(distTo[w]>distTo[v]+e.weight) {
distTo[w]=distTo[v]+e.weight;
if(heap.map.containsKey(w))
heap.change(w, distTo[w]);
else
heap.add(w,distTo[w]);
}
}
}
}
public int networkDelayTime(int[][] times, int N, int K) {
Dijkstra d=new Dijkstra(N,build(times,N),K);
int res=0;
for(int i=1;i<d.distTo.length;i++){
if(d.distTo[i]!=Integer.MAX_VALUE)
res=Math.max(res,d.distTo[i]);
else
return -1;
}
return res;
}
}