Secure Connection
InterviewTime limit2sMemory limit512 MB
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 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 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 and (, ): 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 integers (). If , city hosts no data center of either giant. If , city hosts a Laim.UR data center, and if , city 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 lines contains three integers , , and , meaning that cities and (, ) can be joined by a segment of the communication channel with cost (). 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 , , and , meaning that a communication channel with total cost can be laid between cities and . City must host a Laim.UR data center, and city must host an Xenda data center. If there are several optimal answers, output any of them. If the channel cannot be built, output .
Hint
In the first example, the optimal channel consists of two segments: and .