All Tracks Algorithms Dynamic Programming Problem

Lexicographic Kth path
Tag(s):

Algorithms, Counting and Arrangements, Dynamic Programming, Medium-Hard

Problem
Editorial
Analytics

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.

SAMPLE INPUT
7 8 4
1 2 a
1 3 a
2 4 a
3 4 a
4 5 a
4 6 a
5 7 b
6 7 c
SAMPLE OUTPUT
aaac
Explanation

All the 4 path strings from 1 to 7 are aaab, aaab, aaac and aaac. Lexicographic 4th one will be aaac.

Time Limit: 1.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: Bash, C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Swift-4.1, Visual Basic

CODE EDITOR

Initializing Code Editor...
Your Rating:

Contributor

This Problem was Asked in

HackerEarth

Challenge Name

March Circuits '18

OTHER PROBLEMS OF THIS CHALLENGE
Notifications
View All Notifications

?