Note Fragments
Time limit2sMemory limit1024 MB
Find a walk from vertex 1 to vertex N that visits every vertex holding a note fragment and arrives no later than the shortest 1-to-N distance, or report -1.
- Level
Medium7 of 10
- Topics
- Shortest path, Graph, Dynamic programming
- Solved
- No attempts yet
Problem
A running race is held in a world of vertices numbered 1 through , connected by bidirectional edges. The race starts at vertex . The first person to reach vertex and log in to their account wins. If several people log in first at the same time, all of them win.
Participant lobo_prix has a poor memory and writes the password on a notebook. The malicious participant jhnah917 tore up the notebook and hid the pieces around the world. lobo_prix cannot remember the password, so lobo_prix must collect every note fragment and then go to vertex . jhnah917 only travels along shortest paths to vertex .
The time to pick up a fragment and the time to log in are negligible, so assume they are . All participants move at the same speed. Is there a path that lets lobo_prix win?
Input
The first line contains the number of vertices and the number of edges , separated by a space. ()
Each of the next lines contains the two vertices connected by the -th edge and its length . ( is an integer)
The next line contains integers . If vertex has a note fragment, . Otherwise, .
No pair of vertices is connected by more than one edge. The graph is given such that vertex can be reached from vertex .
Output
If there is no path that lets lobo_prix win, print .
If there is such a path, print the number of visited vertices on the first line. On the next line, print the visited vertices in the order they are visited, separated by spaces.
If several paths are possible, print any one of them.