classSolution: defsecondMinimum(self, n: int, edges: List[List[int]], time: int, change: int) -> int: graph = [[] for _ inrange(n + 1)] for e in edges: x, y = e[0], e[1] graph[x].append(y) graph[y].append(x)
# dist[i][0] 表示从 1 到 i 的最短路长度,dist[i][1] 表示从 1 到 i 的严格次短路长度 dist = [[float('inf')] * 2for _ inrange(n + 1)] dist[1][0] = 0 q = deque([(1, 0)]) while dist[n][1] == float('inf'): p = q.popleft() for y in graph[p[0]]: d = p[1] + 1 if d < dist[y][0]: dist[y][0] = d q.append((y, d)) elif dist[y][0] < d < dist[y][1]: dist[y][1] = d q.append((y, d))
ans = 0 for _ inrange(dist[n][1]): if ans % (change * 2) >= change: ans += change * 2 - ans % (change * 2) ans += time return ans
classSolution { public: intsecondMinimum(int n, vector<vector<int>>& edges, int time, int change){ vector<vector<int>> graph(n + 1); for (auto &e : edges) { graph[e[0]].push_back(e[1]); graph[e[1]].push_back(e[0]); }
// path[i][0] 表示从 1 到 i 的最短路长度,path[i][1] 表示从 1 到 i 的严格次短路长度 vector<vector<int>> path(n + 1, vector<int>(2, INT_MAX)); path[1][0] = 0; queue<pair<int, int>> q; q.push({1, 0}); while (path[n][1] == INT_MAX) { auto [cur, len] = q.front(); q.pop(); for (auto next : graph[cur]) { if (len + 1 < path[next][0]) { path[next][0] = len + 1; q.push({next, len + 1}); } elseif (len + 1 > path[next][0] && len + 1 < path[next][1]) { path[next][1] = len + 1; q.push({next, len + 1}); } } }
int ret = 0; for (int i = 0; i < path[n][1]; i++) { if (ret % (2 * change) >= change) { ret = ret + (2 * change - ret % (2 * change)); } ret = ret + time; } return ret; } };
publicclassSolution { publicintSecondMinimum(int n, int[][] edges, int time, int change) { IList<int>[] graph = new IList<int>[n + 1]; for (int i = 0; i <= n; i++) { graph[i] = new List<int>(); } foreach (int[] edge in edges) { graph[edge[0]].Add(edge[1]); graph[edge[1]].Add(edge[0]); }
// path[i][0] 表示从 1 到 i 的最短路长度,path[i][1] 表示从 1 到 i 的严格次短路长度 int[,] path = newint[n + 1, 2]; for (int i = 0; i <= n; i++) { for (int j = 0; j < 2; j++) { path[i, j] = int.MaxValue; } } path[1, 0] = 0; Queue<int[]> queue = new Queue<int[]>(); queue.Enqueue(newint[]{1, 0}); while (path[n, 1] == int.MaxValue) { int[] arr = queue.Dequeue(); int cur = arr[0], len = arr[1]; foreach (int next in graph[cur]) { if (len + 1 < path[next, 0]) { path[next, 0] = len + 1; queue.Enqueue(newint[]{next, len + 1}); } elseif (len + 1 > path[next, 0] && len + 1 < path[next, 1]) { path[next, 1] = len + 1; queue.Enqueue(newint[]{next, len + 1}); } } }
int ret = 0; for (int i = 0; i < path[n, 1]; i++) { if (ret % (2 * change) >= change) { ret = ret + (2 * change - ret % (2 * change)); } ret = ret + time; } return ret; } }
while (path[n + n] == INT_MAX) { int cur = queue[front].node; int len = queue[front++].len; structNode *next = graph[cur]; while (next) { if (len + 1 < path[next->val]) { path[next->val] = len + 1; queue[back].node = next->val; queue[back++].len = len + 1; } elseif (len + 1 > path[next->val] && len + 1 < path[next->val + n]) { path[next->val + n] = len + 1; queue[back].node = next->val; queue[back++].len = len + 1; } next = next->next; } }
int ret = 0; for (int i = 0; i < path[n + n]; i++) { if (ret % (2 * change) >= change) { ret = ret + 2 * change - ret % (2 * change); } ret = ret + time; }
free(queue); free(path); for (int i = 1; i <= n; i++) { lst_free(graph[i]); } free(graph);
var secondMinimum = function(n, edges, time, change) { const graph = newArray(n + 1).fill(0).map(() =>newArray()); for (const edge of edges) { graph[edge[0]].push(edge[1]); graph[edge[1]].push(edge[0]); }
// path[i][0] 表示从 1 到 i 的最短路长度,path[i][1] 表示从 1 到 i 的严格次短路长度 const path = newArray(n + 1).fill(0).map(() =>newArray(2).fill(Number.MAX_VALUE)); path[1][0] = 0; const queue = []; queue.push([1, 0]); while (path[n][1] === Number.MAX_VALUE) { const [cur, len] = queue.shift(); for (const next of graph[cur]) { if (len + 1 < path[next][0]) { path[next][0] = len + 1; queue.push([next, len + 1]); } elseif (len + 1 > path[next][0] && len + 1 < path[next][1]) { path[next][1] = len + 1; queue.push([next, len + 1]); } } }
let ret = 0; for (let i = 0; i < path[n][1]; i++) { if (ret % (2 * change) >= change) { ret = ret + (2 * change - ret % (2 * change)); } ret = ret + time; } return ret; };