https://leetcode.com/problems/network-delay-time/
/* Java program to find a Pair which has maximum score*/
import javafx.util.Pair; class Solution { public int networkDelayTime(int[][] times, int N, int K) { Map<Integer,List<int[]>> graph = new HashMap<>(); for(int[] edge:times)
{
if(!graph.containsKey(edge[0]))
graph.put(edge[0],new ArrayList<int[]>());
graph.get(edge[0]).add(new int[]{edge[1],edge[2]});
} Map<Integer,Integer> node_dis = new HashMap<>(); for(int i =1;i<=N;i++)
node_dis.put(i,Integer.MAX_VALUE);
node_dis.put(K,0); boolean[] seen = new boolean[N+1]; while(true)
{
int curNode = -1;
//find the nearest node
int curDis = Integer.MAX_VALUE;
for(int i =1;i<=N;i++)
{
if(!seen[i] && node_dis.get(i)<curDis)
{
curDis = node_dis.get(i);
curNode = i;
}
} if(curNode==-1) break;
seen[curNode] = true;
if(graph.containsKey(curNode))
for(int[] pair: graph.get(curNode))
{
node_dis.put(pair[0],Math.min(node_dis.get(pair[0]), curDis+pair[1]));
}
} int res =0;
for(int d: node_dis.values())
{
if(d==Integer.MAX_VALUE) return -1;
res = Math.max(res,d);
} return res;
}
}