There are N cities, and you want to send trucks from your factory (in city 1) to the other cities, the taller the better. There are M two-way roads connecting pairs of cities. Each road has a maximum truck height, determined by bridges and tunnels along the way. For each city V , you want to work out the maximum height of a truck that can travel from city 1 to city V . You do not care about the length of the route, just the best height. Input Format The first line is “N M “, where N is the number cities, and M is the number of roads. Each of the next M lines is ” UVH ′′ . It describes a road between cities U and V , with max truck height H . Constraints All inputs are positive integers. 1≤N≤50000 1≤M≤200000 1≤U≤N 1≤V≤N 1≤H≤1000 ​

23 0

Get full Expert solution in seconds

$1.97 ONLY

Unlock Answer

Sample Input 0

4 3
1 2 72
1 4 96
2 4 80

Sample Output 0

80 0 96

Explanation 0

The best path to city 2 is via city 4. There is no path to city 3.

Sample Input 1

5 6
1 2 70
1 3 75
2 3 80
2 5 72
3 4 65
4 5 70

Sample Output 1

EXPERT ANSWER

Algorithm for the question:

  • this is a simple dijkstra algorithm problem
  • we have to find the minimum height of the truck that can reach from city 1 to city n
  • so we will use dijkstra algorithm to find the minimum height of the truck that can reach from city 1 to city n
  • we will use priority queue to find the minimum height of the truck that can reach from city 1 to city n
  • we will use a vector of pair to store the graph
  • we will use a array to store the minimum height of the truck that can reach from city 1 to city n
  • we will use a loop to take input of the graph
  • we will use a loop to find the minimum height of the truck that can reach from city 1 to city n
  • we will use a loop to print the minimum height of the truck that can reach from city 1 to city n
  • time complexity is O(nlogn)
  • space complexity is O(n)