-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path2492. Minimum Score of a Path Between Two Cities 22 march
52 lines (45 loc) · 1.41 KB
/
2492. Minimum Score of a Path Between Two Cities 22 march
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
class Solution {
public:
int minScore(int n, vector<vector<int>>& roads)
{
// Storing graph in a adjacency list
vector<vector<int>> adj[n+1];
for(int i=0;i<roads.size();i++)
{
adj[roads[i][0]].push_back({roads[i][1],roads[i][2]});
adj[roads[i][1]].push_back({roads[i][0],roads[i][2]});
}
// making a visited array for keep tracking of all the cities (we have visited or not)
// n = number of cities
vector<bool> vis(n+1,false);
vis[1] = true; // mark source node as a visited city
// BFS
queue<int> q;
q.push(1);
while(!q.empty())
{
int node = q.front();
q.pop();
for(auto &it : adj[node])
{
if(!vis[it[0]])
{
vis[it[0]] = true;
q.push(it[0]);
}
}
}
// Computing minimum distance from city 1 to n
int ans = INT_MAX;
for(int i=0;i<roads.size();i++)
{
// minimum possible score out of all available paths
// if src and dest both are visited cities then try to make this our ans
if(vis[roads[i][0]] && vis[roads[i][1]])
{
ans = min(ans,roads[i][2]);
}
}
return ans;
}
};