report VI VI EN
Register | Login
  • HOME
  • PROBLEMSET
  • ROADMAP
  • COMPETITION
  • TOPIC
  • RANKING
  • GUIDE
  • MASHUP
  • ABOUT
  • CONTACT
  • Problem
  • Submit
  • Results
Lexicographically smallest path - MarisaOJ: Marisa Online Judge

Lexicographically smallest path

Time limit: 1000 ms
Memory limit: 256 MB
Consider a undirected graph with $n$ vertices and $m$ edges, where each edge has a weight associated with it. Your task is to find the shortest path from vertex $1$ to vertex $n$. This shortest path should consist of the smallest possible number of edges and the sequence formed by edges weight must be lexicographically smallest among all paths with the same number of edges. ### Input - The first line contains two integer $n,m$. - The next $m$ lines, each line contains three integer $u,v,w$, there is an edge of weight $w$ from between $u$ and $v$. ### Output - The first line, print an integer $k$, the length of the shortest path. It is guaranteed that the answer exists. - The second line, print $k$ integers, the weight of edges on the path from $1$ to $n$. ### Constraints - $1 \le n,m \le 10^5$. - $1 \le u, v \le n, u \neq v$. - $1 \le w \le 10^5$. ### Example Input: ``` 4 4 1 2 4 1 3 3 2 4 1 3 4 2 ``` Output: ``` 2 3 2 ```
BFS / DFS
Connected component
Shortest path
Finding the path
Path on binary matrix
Garden
Operations on number
Bipartite graph
Tom and Jerry
Festival 1
Bamboo Forest of the Lost
Radar
Festival 2
Go
Escape from... dolls
Long leg
Lexicographically smallest path
Graph coloring
Topic
Shortest path
Rating 1800
Solution (2) Solution