Parity Constraint Shortest Path
InterviewTime limit1sMemory limit1024 MB
Given an undirected weighted graph, find for every vertex the minimum cost of an even path and of an odd path from vertex 1, allowing repeated vertices and edges.
- Level
Medium6 of 10
- Topics
- Graph, Shortest path, Dynamic programming, Heap
- Solved
- No attempts yet
Problem
In 2019, the optimization lab at Yonsei University went through a phase of solving problems with a Parity Constraint suddenly added to them, and alumnus Kim Gangsan wrote a paper on Parity Constraints as well. To commemorate this, Gukryeol decided to set a Parity Constraint problem for the Yonsei University programming contest.
You are given a graph with N vertices and M undirected weighted edges. Every vertex is numbered from 1 to N. From any vertex there is always a path to any other vertex, and between any two distinct vertices there is at most one edge.
You start at vertex 1 and want to travel to the other vertices. You may pass through vertices or edges you have already visited again, and each time you pass through an edge its cost is added. For convenience, define the cost of a path as the sum of the costs of the edges that make up the path. If the cost of a path is odd, call it an odd path; if even, an even path. For each vertex, find the cost of the minimum-cost odd path and the cost of the minimum-cost even path from vertex 1.
Input
The input is given as follows.
N M
u1 v1 w1
. . .
uM vM wM
Output
Print N lines. On the i-th line, print the cost of the minimum odd path and the cost of the minimum even path from vertex 1 to vertex i, separated by a space. If such a path does not exist, print -1.
Constraints
- 2 ≤ N ≤ 100,000.
- 1 ≤ M ≤ 300,000.
- 1 ≤ ui, vi ≤ N. ui ≠ vi. ui and vi are the endpoints of the i-th edge.
- 1 ≤ wi ≤ 1,000,000,000. wi is the cost of the i-th edge.
- All numbers in the input are integers.