-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_2473.java
60 lines (57 loc) · 2.37 KB
/
_2473.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.fishercoder.solutions.thirdthousand;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;
public class _2473 {
public static class Solution1 {
/*
* My completely original solution, Dijkstra algorithm!
*/
public long[] minCost(int n, int[][] roads, int[] appleCost, int k) {
List<int[]>[] graph = new ArrayList[n];
for (int i = 0; i < n; i++) {
graph[i] = new ArrayList<>();
}
for (int[] road : roads) {
graph[road[0] - 1].add(new int[] {road[1] - 1, road[2]});
graph[road[1] - 1].add(new int[] {road[0] - 1, road[2]});
}
long[] ans = new long[n];
for (int i = 1; i <= n; i++) {
ans[i - 1] = dijkstra(graph, appleCost, k, i);
}
return ans;
}
private long dijkstra(List<int[]>[] graph, int[] appleCost, int k, int startCity) {
long[] minCostEachCity = new long[appleCost.length];
Arrays.fill(minCostEachCity, Integer.MAX_VALUE);
minCostEachCity[startCity - 1] = 0;
PriorityQueue<int[]> minHeap = new PriorityQueue<>((a, b) -> a[1] - b[1]);
minHeap.offer(new int[] {startCity - 1, 0});
while (!minHeap.isEmpty()) {
int[] curr = minHeap.poll();
int currCity = curr[0];
int currCost = curr[1];
if (currCost > minCostEachCity[currCity]) {
continue;
}
for (int[] neighbor : graph[currCity]) {
int neighborCity = neighbor[0];
int neighborCost = neighbor[1];
int neighborTotalCost = currCost + neighborCost * (k + 1);
if (neighborTotalCost < minCostEachCity[neighborCity]) {
minCostEachCity[neighborCity] = neighborTotalCost;
minHeap.offer(
new int[] {neighborCity, (int) minCostEachCity[neighborCity]});
}
}
}
long min = Long.MAX_VALUE;
for (int i = 0; i < minCostEachCity.length; i++) {
min = Math.min(min, minCostEachCity[i] + appleCost[i]);
}
return min;
}
}
}