Skip to content

Commit af0d96e

Browse files
Add 28th July problem solutions
1 parent 2aa2e79 commit af0d96e

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Intuition
2+
Hard problem.
3+
Since each move takes the same time, it's no need for a priority queue, just a regular queue which holding the info pair (node, distance).
4+
5+
Since it's to find the 2nd min path, it needs 2 containers, say dist, dist2 to hold the distance to the starting node 1.
6+
7+
Approach
8+
Define the function wtime(int step, int time, int change) to compute the time to take for steps=step; this wtime is dependent on step.
9+
In the function secondMinimum(int n, vector<vector<int>>& edges, int time, int change) compute the adjacency list adj as in usual for graphical problem.
10+
Set 2 containers dist(n+1, INT_MAX), dist2(n+1, INT_MAX). The 1 st one is for the min distances; the 2nd one is for the 2nd min distances.
11+
Set queue q holding the info pair (node, distance); push (1,0)& set dist[1]=0
12+
Proceed the BFS by a while loop as long as q is not empty; let (x,d) be the element at front of q.
13+
During the iteration for y in adj[x], newD=d+1, update dist[y], dist2[y] & push (y, newD) to q as follows
14+
for (int y : adj[x]) {
15+
int newD=d+1;
16+
if (newD < dist[y]) {
17+
dist2[y]=dist[y];
18+
dist[y]=newD;
19+
q.emplace(y, newD);
20+
}
21+
else if (newD>dist[y] && newD<dist2[y]) {
22+
dist2[y]=newD;
23+
q.emplace(y, newD);
24+
}
25+
}
26+
steps=dist2[n] is the moves for the 2nd min one takes; its time to take is wtime(steps, time, change)
27+
Complexity
28+
Time complexity:
29+
O(∣V∣+∣E∣)
30+
31+
Space complexity:
32+
O(∣E∣)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
class Solution
2+
{
3+
public:
4+
using int2 = pair<int, int>;
5+
6+
inline static int wtime(int step, int time, int change)
7+
{
8+
int ans = 0;
9+
for (int i = 0; i < step; i++)
10+
{
11+
int gr = ans / change;
12+
if (gr & 1) // If it's a red light
13+
ans = (gr + 1) * change;
14+
ans += time;
15+
}
16+
return ans;
17+
}
18+
19+
static int secondMinimum(int n, vector<vector<int>> &edges, int time, int change)
20+
{
21+
vector<vector<int>> adj(n + 1);
22+
for (auto &e : edges)
23+
{
24+
int v = e[0], w = e[1];
25+
adj[v].push_back(w);
26+
adj[w].push_back(v);
27+
}
28+
29+
vector<int> dist(n + 1, INT_MAX), dist2(n + 1, INT_MAX);
30+
queue<int2> q; // {node, distance}
31+
q.emplace(1, 0);
32+
dist[1] = 0;
33+
34+
while (!q.empty())
35+
{
36+
auto [x, d] = q.front();
37+
q.pop();
38+
for (int y : adj[x])
39+
{
40+
int newD = d + 1;
41+
if (newD < dist[y])
42+
{
43+
dist2[y] = dist[y];
44+
dist[y] = newD;
45+
q.emplace(y, newD);
46+
}
47+
else if (newD > dist[y] && newD < dist2[y])
48+
{
49+
dist2[y] = newD;
50+
q.emplace(y, newD);
51+
}
52+
}
53+
}
54+
55+
int steps = dist2[n];
56+
if (steps == INT_MAX)
57+
return -1;
58+
return wtime(steps, time, change);
59+
}
60+
};
61+
62+
auto init = []()
63+
{
64+
ios::sync_with_stdio(false);
65+
cin.tie(nullptr);
66+
cout.tie(nullptr);
67+
return 'c';
68+
}();

0 commit comments

Comments
 (0)