Shortest Path Again?
Practice
3 (3 votes)
Shortest path algorithms
Graphs
Algorithms
Problem
86% Success 1159 Attempts 30 Points 5s Time Limit 256MB Memory 1024 KB Max Code

There are N cities and Demon is in city 1. Now, there are M pair of roads between some cities and each of the roads have cost to travel by it. The cost to travel from city c1 to ck is 1*w1 + 2*w2 + 3*w3+......+(k-1)*wk-1 where wi is the cost to travel from city pi to city pi+1. Demon is lazy and so he wants you to find the minimum cost to travel from city 1 to every other city from 1 to N. If there exists no path to travel from city 1 to city i, print -1.

Note: There can be self-loop roads or multiple edge roads. All the roads are bidirectional.

Input:
The first line contains two space-separated integers n and m - the number of nodes and edges respectively. The next m lines contain three space-separated integers x, y, and w - representing an edge between x and y with cost w

Constraints:
1<=n<=3000
0<=m<=10000
1<=x,y<=n
1<=w<=1e9

Output:
Output n lines. In the ith line, output the minimum distance from city 1 to the ith city. If there exists no such path, output -1.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
3 votes
Tags:
AlgorithmsGraphsShortest Path Algorithms
Points:30
12 votes
Tags:
AlgorithmsApprovedGraphsMediumOpenShortest Path Algorithm
Points:30
9 votes
Tags:
AlgorithmsBinary SearchFloyd Warshall AlgorithmGraphsMediumShortest Path Algorithm