You are given a positive integer n representing n cities numbered from 1
to n. You are also given a 2D array roads, where roads[i] = [ai, bi, costi] indicates that there is a bidirectional road between cities ai
and bi with a cost of traveling equal to costi.
You can buy apples in any city you want, but some cities have different costs to buy apples. You are given the 1-based array appleCost where
appleCost[i] is the cost of buying one apple from city i.
You start at some city, traverse through various roads, and eventually buy
exactly one apple from any city. After you buy that apple, you have to return back to the city you started at, but now the cost of all the roads will be multiplied by a given factor k.
Given the integer k, return a 1-based arrayanswerof sizenwhereanswer[i]_is theminimum total cost to buy an apple if you start at city _i.

Input: n =4, roads =[[1,2,4],[2,3,2],[2,4,5],[3,4,1],[1,3,4]], appleCost =[56,42,102,301], k =2Output: [54,42,48,51]Explanation: The minimum cost for each starting city is the following:- Starting at city 1: You take the path 1->2, buy an apple at city 2, and finally take the path 2->1. The total cost is4+42+4*2=54.- Starting at city 2: You directly buy an apple at city 2. The total cost is42.- Starting at city 3: You take the path 3->2, buy an apple at city 2, and finally take the path 2->3. The total cost is2+42+2*2=48.- Starting at city 4: You take the path 4->3->2 then you buy at city 2, and finally take the path 2->3->4. The total cost is1+2+42+1*2+2*2=51.
Example 2:
1
2
3
4

Input: n =3, roads =[[1,2,5],[2,3,1],[3,1,2]], appleCost =[2,3,1], k =3Output: [2,3,1]Explanation: It is always optimal to buy the apple in the starting city.
We need to find the minimum cost for each city to buy an apple and return, considering the cost of roads changes after buying the apple. The best way is to use Dijkstra’s algorithm twice: once for the trip to the apple city (normal cost), and once for the return trip (cost multiplied by k).
classSolution {
publiclong[]minimumCost(int n, int[][] roads, int[] appleCost, int k) {
List<int[]>[] g =new ArrayList[n];
for (int i = 0; i < n; ++i) g[i]=new ArrayList<>();
for (int[] r : roads) {
g[r[0]-1].add(newint[]{r[1]-1, r[2]});
g[r[1]-1].add(newint[]{r[0]-1, r[2]});
}
long[][] to =newlong[n][n], back =newlong[n][n];
for (int i = 0; i < n; ++i) {
to[i]= dijkstra(g, i, 1);
back[i]= dijkstra(g, i, k);
}
long[] ans =newlong[n];
Arrays.fill(ans, Long.MAX_VALUE);
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
ans[i]= Math.min(ans[i], to[i][j]+ appleCost[j]+ back[j][i]);
return ans;
}
long[]dijkstra(List<int[]>[] g, int start, int mul) {
int n = g.length;
long[] dist =newlong[n];
Arrays.fill(dist, Long.MAX_VALUE);
dist[start]= 0;
PriorityQueue<long[]> pq =new PriorityQueue<>(Comparator.comparingLong(a->a[0]));
pq.offer(newlong[]{0, start});
while (!pq.isEmpty()) {
long[] cur = pq.poll();
if (cur[0]> dist[(int)cur[1]]) continue;
for (int[] e : g[(int)cur[1]]) {
long nd = cur[0]+ e[1]*mul;
if (nd < dist[e[0]]) {
dist[e[0]]= nd;
pq.offer(newlong[]{nd, e[0]});
}
}
}
return dist;
}
}
classSolution:
defminimumCost(self, n: int, roads: list[list[int]], appleCost: list[int], k: int) -> list[int]:
import heapq
defdijkstra(start: int, mul: int) -> list[int]:
dist = [float('inf')] * n
dist[start] =0 pq: list[tuple[int, int]] = [(0, start)]
while pq:
d, u = heapq.heappop(pq)
if d > dist[u]: continuefor v, w in g[u]:
nd = d + w * mul
if nd < dist[v]:
dist[v] = nd
heapq.heappush(pq, (nd, v))
return dist
g = [[] for _ in range(n)]
for a, b, c in roads:
g[a-1].append((b-1, c))
g[b-1].append((a-1, c))
to = [dijkstra(i, 1) for i in range(n)]
back = [dijkstra(i, k) for i in range(n)]
ans = [float('inf')] * n
for i in range(n):
for j in range(n):
v = to[i][j] + appleCost[j] + back[j][i]
if v < ans[i]: ans[i] = v
return ans