Lexicographic Kth path
Practice
4.5 (2 votes)
Counting and arrangements
Dynamic programming
Algorithms
Medium Hard
Problem
88% Success 774 Attempts 50 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You have been given an undirected graph containing of N nodes and M edges. Each of the edges have a lowercase character('a'-'z') written on them. Hence, each path from 1 to N can be described with a string consisting of characters coming on the path. You have to find lexicographic Kth shortest path from 1 to N in this graph.

INPUT:
First Line of input consists of three integers N ,M and K. Next M lines consists of two integers u and v and a character c denoting an edge between u and v with label c .

OUTPUT:
Print lexicographical Kth shortest path in this graph from 1 to N. If total number of shortest paths are less than K, print -1.

CONSTRAINTS:
2 ≤ N ≤ 105
N-1 ≤ M ≤ 106
1 ≤ K ≤ 1018
1 ≤ u,v ≤ N
c will be a lower case alphabet ('a'-'z').
Graph will not contain self loops and parallel edges. 1 and N will lie in same connected components.

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:50
6 votes
Tags:
Counting and ArrangementsDynamic ProgrammingAlgorithmsMedium-Hard
Points:50
Tags:
Medium-HardMedium-Hard
Points:50
3 votes
Tags:
Counting and ArrangementsDynamic ProgrammingAlgorithms