Under construction

Still being built. Things may change

Lexicographically Minimum Walk

Time limit2sMemory limit1024 MB

Summary
Find the lexicographically smallest color sequence among all walks from S to T of length at most 10^100, reporting IMPOSSIBLE or TOO LONG when needed.
Level

Hard8 of 10

Topics
Graph, Greedy, BFS, Implementation
Solved
No attempts yet

Problem

There is a directed graph GG with NN nodes and MM edges. Each node is numbered 11 through NN, and each edge is numbered 11 through MM. For each ii (1≤i≤M1 \le i \le M), edge ii goes from vertex u_iu\_i to vertex v_iv\_i and has a unique color c_ic\_i.

A walk is a sequence of edges e_1,e_2,⋯ ,e_le\_1, e\_2, \cdots, e\_{l} where for each 1≤k<l1 \le k < l, v_e_kv\_{e\_k} (the tail of edge e_ke\_k) is the same as u_e_k+1u\_{e\_{k+1}} (the head of edge e_k+1e\_{k+1}). We say a walk e_1,e_2,⋯ ,e_le\_1, e\_2, \cdots, e\_l starts at vertex u_e_1u\_{e\_1} and ends at vertex v_e_lv\_{e\_l}. The same edge can appear multiple times in a walk.

The color sequence of a walk e_1,e_2,⋯ ,e_le\_1, e\_2, \cdots, e\_l is defined as c_e_1,c_e_2,⋯ ,c_e_lc\_{e\_1}, c\_{e\_2}, \cdots, c\_{e\_l}.

Consider all color sequences of walks of length at most 1010010^{100} from vertex SS to vertex TT in GG. Write a program that finds the lexicographically minimum sequence among them.

Input

The first line of the input contains four space-separated integers NN, MM, SS, and TT (1≤N≤100 0001 \le N \le 100\,000, 0≤M≤300 0000 \le M \le 300\,000, 1≤S≤N1 \le S \le N, 1≤T≤N1 \le T \le N, S≠TS \neq T).

Then MM lines follow: the ii (1≤i≤M1 \le i \le M)-th of them contains three space-separated integers u_iu\_i, v_iv\_i, and c_ic\_i (1≤u_i,v_i≤N1 \le u\_i, v\_i \le N, u_i≠v_iu\_i \neq v\_i, 1≤c_i≤1091 \le c\_i \le 10^{9}); it describes a directed edge from vertex u_iu\_i to vertex v_iv\_i with color c_ic\_i.

The graph has no multiple edges and no loops, and each edge has a unique color. Formally, for any 1≤i<j≤M1 \le i < j \le M, c_i≠c_jc\_i \neq c\_j and (u_i, v_i)≠(u_j, v_j)(u\_i,\ v\_i) \neq (u\_j,\ v\_j).

Output

If there is no walk from vertex SS to vertex TT, print "IMPOSSIBLE". (without quotes)

Otherwise, let a_1,a_2,⋯ ,a_la\_1, a\_2, \cdots, a\_l be the lexicographically minimum sequence among all color sequences of walks of length at most 1010010^{100} from vertex SS to vertex TT.

  • If l≤106l \le 10^{6}, print a_1,a_2,⋯ ,a_la\_1, a\_2, \cdots, a\_l in the first line. There should be a space between each printed integer.
  • If l>106l > 10^{6}, print "TOO LONG". (without quotes)

Notes

A sequence p_1,p_2,⋯ ,p_np\_1, p\_2, \cdots, p\_{n} is lexicographically smaller than another sequence q_1,q_2,⋯ ,q_mq\_1, q\_2, \cdots, q\_{m} if and only if one of the following holds:

  • There is a unique jj (0≤j<min⁡(n,m)0 \le j < \min(n, m)) where p_1=q_1p\_1 = q\_1, p_2=q_2p\_2 = q\_2, ⋯\cdots, p_j=q_jp\_{j} = q\_{j} and p_j+1<q_j+1p\_{j+1} < q\_{j+1}.
  • n<mn < m and p_1=q_1p\_1 = q\_1, p_2=q_2p\_2 = q\_2, ⋯\cdots, p_n=q_np\_n = q\_n. In other words, pp is a strict prefix of qq.

Examples3

  1. Example 1

    Input
    3 3 1 3
    1 2 1
    2 3 7
    1 3 5
    
    Expected output
    1 7
    
  2. Example 2

    Input
    3 4 1 3
    1 2 1
    2 1 2
    2 3 7
    1 3 5
    
    Expected output
    TOO LONG
    
  3. Example 3

    Input
    2 0 2 1
    
    Expected output
    IMPOSSIBLE