#GD0000237. Đường đi ngắn nhất khi có cạnh âm (Greedy vs Dijkstra - Negative Edge)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đường đi ngắn nhất khi có cạnh âm (Greedy vs Dijkstra - Negative Edge)
Greedy vs Dijkstra - Negative Edge
Source: Phước Hưng OJ
Version: Phuoc Hung OJ Extended
Problem Statement
Given a directed graph of vertices and edges, where edge has integer weight , possibly negative. A path's length is the sum of its edge weights. There are no negative-weight cycles.
Find the shortest distances from vertex to all vertices. Print INF for an unreachable vertex. Parallel edges and nonnegative self-loops are permitted.
This exercise examines the failure of finalizing shortest distances too early when negative edges exist.
Input
- The first line contains .
- Each of the next lines contains , a directed weighted edge.
Output
Print space-separated values: the shortest distance from to vertex , or INF if unreachable.
Subtasks
- Subtask 1 (20 points): ; ; ; ; không có chu trình trọng số âm.
- Subtask 2 (30 points): ; ; ; ; không có chu trình trọng số âm.
- Subtask 3 (50 points): ; ; ; ; không có chu trình trọng số âm.
Examples
Input
4 4
1 2 2
1 3 5
3 2 -10
2 4 3
Output
0 -5 5 -2
Explanation
Vertex is reached at distance . Although the direct edge has length , the route has length . Following gives distance to vertex . The distance to vertex itself is .