This page is still under construction.

Parts of this page are still being built. What you see may change.

Secure Connection

Interview

Time limit2sMemory limit512 MB

Summary
Given a weighted undirected graph where each vertex is labeled 0, 1, or 2, find the cheapest path between any label-1 vertex and any label-2 vertex, and report its endpoints and cost.
Level

Medium5 of 10

Topics
Graph, Shortest path, Heap, Dynamic programming
Solved
No attempts yet

Problem

After recent news about wiretapping of communication channels, two rival internet giants of Uragania, Laim.UR and Xenda, agreed to establish a secure communication channel between each other's data centers. Uragania has nn cities, but unfortunately no city hosts data centers of both giants. So building the secure channel requires laying intercity communication lines.

Specialists of the companies identified mm pairs of cities that can be joined by laying a segment of the communication channel, and estimated the cost of building such a segment for each pair.

The resulting channel may consist of several segments. It must start in one of the cities hosting a data center of the first company, may pass through intermediate cities, and must end in a city hosting a data center of the second company.

Now you need to determine the minimum cost of a secure channel connecting the two companies' data centers.

Input

The first line contains integers nn and mm (2≤n≤5 0002 \le n \le 5\,000, 1≤m≤1051 \le m \le 10^5): the number of cities and the number of pairs of cities that can be joined by a segment of the communication channel.

The second line contains nn integers a_ia\_i (0≤a_i≤20 \le a\_i \le 2). If a_i=0a\_i = 0, city ii hosts no data center of either giant. If a_i=1a\_i = 1, city ii hosts a Laim.UR data center, and if a_i=2a\_i = 2, city ii hosts an Xenda data center. It is guaranteed that among these numbers there is at least one 1 and at least one 2.

Each of the following mm lines contains three integers s_is\_i, t_it\_i, and c_ic\_i, meaning that cities s_is\_i and t_it\_i (1≤s_i,t_i≤n1 \le s\_i, t\_i \le n, s_i≠t_is\_i \ne t\_i) can be joined by a segment of the communication channel with cost c_ic\_i (1≤c_i≤1051 \le c\_i \le 10^5). Each pair of cities can be joined by at most one segment of the channel.

Output

If two data centers of different internet giants can be connected by a secure communication channel, output three numbers xx, yy, and dd, meaning that a communication channel with total cost dd can be laid between cities xx and yy. City xx must host a Laim.UR data center, and city yy must host an Xenda data center. If there are several optimal answers, output any of them. If the channel cannot be built, output −1-1.

Hint

In the first example, the optimal channel consists of two segments: 3−23-2 and 2−42-4.

Examples2

  1. Example 1

    Input
    6 7
    1 0 1 2 2 0
    1 3 3
    1 2 4
    2 3 3
    2 4 2
    1 6 5
    3 5 6
    5 6 1
    
    Expected output
    3 4 5
    
  2. Example 2

    Input
    4 2
    1 0 0 2
    1 3 3
    2 4 2
    
    Expected output
    -1