Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.StringTokenizer;
- public class Main {
- public static void main(String[] args) throws FileNotFoundException
- {
- InputStream inputStream = System.in;
- OutputStream outputStream = System.out;
- InputReader scn = new InputReader(inputStream);
- PrintWriter pw = new PrintWriter(new File("distance.out"));
- Task solver = new Task();
- solver.solve(scn, pw);
- pw.close();
- }
- }
- class Task{
- static int[] heap = new int[9000001];
- static int[] ver = new int[9000001];
- static int heapSize = 0;
- public static void solve(InputReader scn, PrintWriter pw)
- {
- int n = scn.nextInt(), m = scn.nextInt(), f = scn.nextInt(), s = scn.nextInt();
- long[] d = new long[70001];
- int[] prev = new int[70001];
- Arrays.fill(d, 100000000000L);
- Arrays.fill(prev, -1);
- ArrayList<ArrayList<Pair>> g = new ArrayList<ArrayList<Pair>>();
- for (int i = 0; i < n; i++)
- g.add(new ArrayList<Pair>());
- for (int i = 0; i < m; i++)
- {
- Pair para = new Pair();
- int x = scn.nextInt(), y = scn.nextInt(), z = scn.nextInt();
- --x;--y;
- para.to = y;
- para.cost = z;
- g.get(x).add(para);
- Pair para2 = new Pair();
- para2.to = x;
- para2.cost = z;
- g.get(y).add(para2);
- }
- --f;--s;
- d[f] = 0;
- add(0, f);
- while (true)
- {
- if (heapSize == 0)
- break;
- int v = getV();
- int dist = getH();
- delete();
- if (dist > d[v])
- continue;
- for (int i = 0; i < g.get(v).size(); i++)
- {
- Pair para = new Pair();
- para = g.get(v).get(i);
- int to = para.to;
- int cost = para.cost;
- int newDist = dist + cost;
- if (newDist < d[to])
- {
- d[to] = newDist;
- prev[to] = v;
- add(newDist, to);
- }
- }
- }
- long ans = d[s];
- if (ans == 100000000000L)
- ans = -1;
- else
- {
- pw.println(ans);
- ArrayList<Integer> path = new ArrayList<Integer>();
- int cur = s;
- while (true)
- {
- path.add(cur + 1);
- if (cur == f)
- break;
- cur = prev[cur];
- }
- for (int i = path.size()-1; i >= 0; --i)
- pw.print(path.get(i) + " ");
- }
- }
- public static void swap(int i, int j)
- {
- int c = heap[i];
- heap[i] = heap[j];
- heap[j] = c;
- c = ver[i];
- ver[i] = ver[j];
- ver[j] = c;
- }
- public static void reheapify_up(int v)
- {
- if (v > 1)
- if (heap[v / 2] > heap[v])
- {
- swap(v, v / 2);
- reheapify_up(v / 2);
- }
- }
- public static void reheapify_down(int v)
- {
- int p = v;
- if (v * 2 <= heapSize)
- if (heap[v * 2] < heap[p])
- p = v * 2;
- if (v * 2 + 1 <= heapSize)
- if (heap[v * 2 + 1] < heap[p])
- p = v * 2;
- if (v == p)
- return;
- swap(v, p);
- reheapify_down(p);
- }
- public static int getH()
- {
- return heap[1];
- }
- public static int getV()
- {
- return ver[1];
- }
- public static void delete()
- {
- heap[1] = heap[heapSize];
- ver[1] = ver[heapSize];
- --heapSize;
- reheapify_down(1);
- }
- public static void add(int cost, int v)
- {
- heap[++heapSize] = cost;
- ver[heapSize] = v;
- reheapify_up(heapSize);
- }
- }
- class Pair{
- public int to, cost;
- }
- class InputReader {
- public BufferedReader reader;
- public StringTokenizer tokenizer;
- public InputReader(InputStream stream) throws FileNotFoundException {
- reader = new BufferedReader(new InputStreamReader(new FileInputStream("distance.in")));
- tokenizer = null;
- }
- public String next() {
- while (tokenizer == null || !tokenizer.hasMoreTokens()) {
- try {
- tokenizer = new StringTokenizer(reader.readLine());
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- return tokenizer.nextToken();
- }
- public int nextInt() {
- return Integer.parseInt(next());
- }
- public long nextLong(){
- return Long.parseLong(next());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement