Time Machine
Time limit1sMemory limit256 MB
Compute the fastest times from city 1 over bus routes with possibly negative durations, or print -1 when a reachable negative cycle exists.
- Level
Medium4 of 10
- Topics
- Shortest path, Graph
- Solved
- No attempts yet
Problem
There are cities and bus routes, each running from one city to another. A route is written as three integers , , , where is the departure city, is the arrival city, and is the time the ride takes.
The time is not always positive. When the ride is instant, and when the bus is a time machine that sends you back in time.
Write a program that finds the fastest time to travel from city 1 to every other city.
Input
The first line has the number of cities () and the number of bus routes ().
Each of the next lines has one route , , (, ). Several routes may connect the same pair of cities, and a route with may appear.
Output
If a trip that starts at city 1 can push the time back without bound, print -1 on the first line.
Otherwise print lines holding the fastest time from city 1 to city 2, city 3, ..., city , one time per line in that order. If a city has no route from city 1, print -1 on its line. When , print nothing.