Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import static java.lang.Integer.parseInt;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.PriorityQueue;
- import java.util.Stack;
- import java.util.StringTokenizer;
- public class Main{
- static ArrayList<pair> graph[];
- static long cost[];
- static int n, m, parents[];
- static boolean visited[];
- static PriorityQueue<pair2> q;
- static BufferedWriter writer;
- static BufferedReader reader;
- static Stack<Integer> s;
- public static void main(String[] args) throws IOException {
- input();
- sol();
- }
- public static void input() throws IOException {
- reader = new BufferedReader(new InputStreamReader(System.in));
- StringTokenizer tok = new StringTokenizer(reader.readLine());
- n = parseInt(tok.nextToken());
- m = parseInt(tok.nextToken());
- graph = new ArrayList[1 + n];
- for (int i = 0; i <= n; i++) {
- graph[i] = new ArrayList<pair>();
- }
- for (int i = 1; i <= m; i++) {
- tok = new StringTokenizer(reader.readLine());
- int x = parseInt(tok.nextToken()), y = parseInt(tok.nextToken()), wieght = parseInt(tok.nextToken());
- graph[x].add(new pair(y, wieght));
- graph[y].add(new pair(x, wieght));
- }
- }
- public static void sol() throws IOException {
- writer = new BufferedWriter(new OutputStreamWriter(System.out));
- cost = new long[n + 1];
- parents = new int[n + 1];
- visited = new boolean[n + 1];
- q = new PriorityQueue<pair2>();
- s = new Stack<Integer>();
- q.add(new pair2(1, 0));
- visited[1] = true;
- Arrays.fill(cost, Integer.MAX_VALUE);
- cost[1] = 0;
- while (!q.isEmpty()) {
- pair2 obj = q.poll();
- for (pair i : graph[obj.cell]) {
- if (!visited[i.cell] || cost[i.cell] > (i.wieght + obj.wieght)) {
- visited[i.cell] = true;
- cost[i.cell] = i.wieght + obj.wieght;
- q.add(new pair2(i.cell, cost[i.cell]));
- parents[i.cell] = obj.cell;
- }
- }
- }
- int x = n;
- while (0 != parents[x]) {
- s.push(x);
- x = parents[x];
- }
- if (!s.isEmpty()) {
- writer.write(1 + " ");
- while (!s.isEmpty()) {
- writer.write(s.pop() + " ");
- }
- writer.flush();
- } else {
- System.out.println(-1);
- }
- }
- public static class pair {
- int cell, wieght;
- public pair(int cell, int wieght) {
- this.cell = cell;
- this.wieght = wieght;
- }
- }
- public static class pair2 implements Comparable<pair2> {
- int cell;long wieght;
- public pair2(int cell, long wieght) {
- this.cell = cell;
- this.wieght = wieght;
- }
- @Override
- public int compareTo(pair2 t) {
- if (this.wieght < t.wieght) {
- return -1;
- } else if (this.wieght > t.wieght) {
- return 1;
- } else {
- return 0;
- }
- }
- }
- }
- /*
- 10 10
- 1 4 200
- 1 9 197
- 3 4 79
- 3 5 213
- 3 6 149
- 5 8 3
- 5 9 189
- 6 7 130
- 6 9 51
- 8 10 135
- 1 9 5 8 10
- 3 1
- 1 2 1
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement