Title Description
Cities are represented by a two-way connected graph, in which there are n nodes, numbered from 1 to n (including 1 and N). The edges in the figure are represented by a two-dimensional integer array edges, where each e d g e s [ i ] = [ u i , v i ] edges[i] = [u_i, v_i] edges[i]=[ui, vi] indicates a node u i u_i ui # and nodes v i v_i vi) bidirectional connected edges between. Each group of node pairs is connected by at most one edge, and the vertex has no edge connected to itself. The time to cross either side is time minutes.
Each node has a traffic light, which changes once every change minute, from green to red, and then from red to green, in a cycle. All signal lights change at the same time. You can enter a node at any time, but you can only leave when the node signal is green. If the signal light is green, you can't wait at the node and must leave.
The second smallest value is the smallest of all values strictly greater than the minimum value.
- For example, the second smallest value in [2, 3, 4] is 3, while the second smallest value in [2, 2, 4] is 4.
Give you n, edges, time and change, and return the second short time from node 1 to node n.
be careful:
- You can pass through any vertex any time, including 1 and n.
- You can assume that when you leave, all the lights just turn green.
Example 1
Input: n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 Output: 13 Explanation: The left figure above shows the given urban traffic map. The blue path on the right is the shortest time path. The time spent is: - Total time spent starting from node 1=0 - 1 -> 4: 3 Minutes, total time=3 - 4 -> 5: 3 Minutes, total time=6 Therefore, the minimum time required is 6 minutes. The red path on the right is the second short time path. - Total time spent starting from node 1=0 - 1 -> 3: 3 Minutes, total time=3 - 3 -> 4: 3 Minutes, total time=6 - Wait 4 minutes at node 4, total time spent=10 - 4 -> 5: 3 Minutes, total time=13 So the second short time is 13 minutes.
Example 2
Input: n = 2, edges = [[1,2]], time = 3, change = 2 Output: 11 Explanation: The shortest time path is 1 -> 2 ,Total time spent = 3 minute The shortest time path is 1 -> 2 -> 1 -> 2 ,Total time spent = 11 minute
Tips
- 2 < = n < = 1 0 4 2 <= n <= 10^4 2<=n<=104
- n − 1 < = e d g e s . l e n g t h < = m i n ( 2 ∗ 1 0 4 , n ∗ ( n − 1 ) / 2 ) n - 1 <= edges.length <= min(2 * 10^4, n * (n - 1) / 2) n−1<=edges.length<=min(2∗104,n∗(n−1)/2)
- e d g e s [ i ] . l e n g t h = = 2 edges[i].length == 2 edges[i].length==2
- 1 < = u i , v i < = n 1 <= u_i, v_i <= n 1<=ui,vi<=n
- u i ! = v i u_i != v_i ui!=vi
- Without duplicate edges
- Each node can arrive directly or indirectly from other nodes
- 1 < = t i m e , c h a n g e < = 1 0 3 1 <= time, change <= 10^3 1<=time,change<=103
Problem solving ideas
According to the meaning of the question, the time required for the same path length is the same, and the longer the path, the longer the time required. Therefore, the strict sub short path to the destination can be obtained, and the second short time to the destination can be calculated directly.
Breadth first search can be used to solve the shortest path problem with the same weight, but some modifications are made here. When using breadth first search to solve the shortest path, the path length of the passing point and the initial point is the smallest of all the paths that have not been searched. Therefore, the path length of the passing point and the initial point obtained by each breadth first search is non decreasing. The shortest path and strict sub short path of all points and initial points can be recorded. Once the target point and strict sub short path are obtained, the second short path to the destination can be calculated directly.
For the calculation of path length and practice, it is assumed that the time to reach node i is
t
i
t_i
ti, the node is reached
i
+
1
i+1
The time of i+1 is:
t
i
+
1
=
t
i
+
t
w
a
i
t
+
t
i
m
e
t_{i+1}=t_i+t_{wait}+time
ti+1=ti+twait+time
Among them,
t
w
a
i
t
t_{wait}
twait , is calculated as follows:
t
w
a
i
t
=
{
0
if
t
i
m
o
d
2
×
c
h
a
n
g
e
∈
[
0
,
c
h
a
n
g
e
)
2
×
c
h
a
n
g
e
−
t
i
m
o
d
2
×
c
h
a
n
g
e
if
t
i
m
o
d
2
×
c
h
a
n
g
e
∈
[
c
h
a
n
g
e
,
2
×
c
h
a
n
g
e
)
t_{wait}=\begin{cases} 0 & \text { if } {t_i\mod{2\times change}\in [0,change)}\\ 2\times change-t_i\mod{2\times change} & \text{ if }t_i \mod{2\times change}\in [change, 2 \times change) \end{cases}
twait={02×change−timod2×change if timod2×change∈[0,change) if timod2×change∈[change,2×change)
Code display
#!/usr/bin/env python # -*- coding:utf-8 -*- # @FileName :SecondMinimum.py # @Time :2022/1/25 19:34 # @Author :PangXZ # Leetcode 2045: the second short time to reach the destination from collections import deque from typing import List class Solution: def secondMinimum(self, n: int, edges: List[List[int]], time: int, change: int) -> int: graph = [[] for _ in range(n + 1)] # Create a two-dimensional array to store the destination from each node for e in edges: x, y = e[0], e[1] graph[x].append(y) graph[y].append(x) # dist[i][0] represents the shortest path from 1 to I, and dist[i][1] represents the strict sub shortest path from 1 to I dist = [[float('inf')] * 2 for _ in range(n + 1)] dist[1][0] = 0 # Indicates that the shortest path from 1 to itself is 1 q = deque([(1, 0)]) # Breadth first search queue, starting from 1, with step size of 0 while dist[n][1] == float('inf'): # Indicates that the loop is executed when the strict sub short path from 1 to n is infinite p = q.popleft() for y in graph[p[0]]: # Traverse all reachable nodes of the current node d = p[1] + 1 # Represents the step size if d < dist[y][0]: # If d is less than the shortest path from current to y, breadth first search is reflected dist[y][0] = d # Update shortest path q.append((y, d)) elif dist[y][0] < d < dist[y][1]: # If d is greater than the current shortest path to y, it is less than the strict sub shortest path to y dist[y][1] = d # Update strict secondary short path q.append((y, d)) ans = 0 for _ in range(int(dist[n][1])): # Simulate the process on the strict sub short path from 1 to n if ans % (change * 2) >= change: # If the current time exceeds the time when the signal lamp changes, the waiting time needs to be added ans += change * 2 - ans % (change * 2) ans += time # Plus passing time return ans if __name__ == "__main__": solution = Solution() print(solution.secondMinimum(n=5, edges=[[1, 2], [1, 3], [1, 4], [3, 4], [4, 5]], time=3, change=5))
Content source: Leetcode 2045. The second short time to reach the destination