Advertisement
Guest User

Dijkstra

a guest
Apr 7th, 2014
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.26 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.io.OutputStream;
  9. import java.io.PrintWriter;
  10. import java.util.ArrayList;
  11. import java.util.Arrays;
  12. import java.util.StringTokenizer;
  13.  
  14.  
  15. public class Main {
  16.     public static void main(String[] args) throws FileNotFoundException
  17.     {
  18.         InputStream inputStream = System.in;
  19.         OutputStream outputStream = System.out;
  20.         InputReader scn = new InputReader(inputStream);
  21.         PrintWriter pw = new PrintWriter(new File("distance.out"));
  22.         Task solver = new Task();
  23.         solver.solve(scn, pw);
  24.         pw.close();
  25.     }
  26. }
  27.  
  28. class Task{
  29.     static int[] heap = new int[9000001];
  30.     static int[] ver = new int[9000001];
  31.     static int heapSize = 0;
  32.     public static void solve(InputReader scn, PrintWriter pw)
  33.     {
  34.         int n = scn.nextInt(), m = scn.nextInt(), f = scn.nextInt(), s = scn.nextInt();
  35.         long[] d = new long[70001];
  36.         int[] prev = new int[70001];
  37.         Arrays.fill(d, 100000000000L);
  38.         Arrays.fill(prev, -1);
  39.         ArrayList<ArrayList<Pair>> g = new ArrayList<ArrayList<Pair>>();
  40.         for (int i = 0; i < n; i++)
  41.             g.add(new ArrayList<Pair>());
  42.         for (int i = 0; i < m; i++)
  43.         {
  44.             Pair para = new Pair();
  45.             int x = scn.nextInt(), y = scn.nextInt(), z = scn.nextInt();
  46.             --x;--y;
  47.             para.to = y;
  48.             para.cost = z;
  49.             g.get(x).add(para);
  50.             Pair para2 = new Pair();
  51.             para2.to = x;
  52.             para2.cost = z;
  53.             g.get(y).add(para2);
  54.         }
  55.         --f;--s;
  56.         d[f] = 0;
  57.         add(0, f);
  58.         while (true)
  59.         {
  60.             if (heapSize == 0)
  61.                 break;
  62.             int v = getV();
  63.             int dist = getH();
  64.             delete();
  65.             if (dist > d[v])
  66.                 continue;
  67.             for (int i = 0; i < g.get(v).size(); i++)
  68.             {
  69.                 Pair para = new Pair();
  70.                 para = g.get(v).get(i);
  71.                 int to = para.to;
  72.                 int cost = para.cost;
  73.                 int newDist = dist + cost;
  74.                 if (newDist < d[to])
  75.                 {
  76.                     d[to] = newDist;
  77.                     prev[to] = v;
  78.                     add(newDist, to);
  79.                 }
  80.             }
  81.         }
  82.         long ans = d[s];
  83.         if (ans == 100000000000L)
  84.             ans = -1;
  85.         else
  86.             {
  87.             pw.println(ans);
  88.             ArrayList<Integer> path = new ArrayList<Integer>();
  89.             int cur = s;
  90.             while (true)
  91.                 {
  92.                     path.add(cur + 1);
  93.                     if (cur == f)
  94.                         break;
  95.                     cur = prev[cur];
  96.                 }
  97.             for (int i = path.size()-1; i >= 0; --i)
  98.                 pw.print(path.get(i) + " ");
  99.             }
  100.        
  101.     }
  102.     public static void swap(int i, int j)
  103.     {
  104.         int c = heap[i];
  105.         heap[i] = heap[j];
  106.         heap[j] = c;
  107.         c = ver[i];
  108.         ver[i] = ver[j];
  109.         ver[j] = c;
  110.     }
  111.    
  112.     public static void reheapify_up(int v)
  113.     {
  114.         if (v > 1)
  115.             if (heap[v / 2] > heap[v])
  116.             {
  117.                 swap(v, v / 2);
  118.                 reheapify_up(v / 2);
  119.             }
  120.     }
  121.    
  122.     public static void reheapify_down(int v)
  123.     {
  124.         int p = v;
  125.         if (v * 2 <= heapSize)
  126.             if (heap[v * 2] < heap[p])
  127.                 p = v * 2;
  128.         if (v * 2 + 1 <= heapSize)
  129.             if (heap[v * 2 + 1] < heap[p])
  130.                 p = v * 2;
  131.         if (v == p)
  132.             return;
  133.         swap(v, p);
  134.         reheapify_down(p);
  135.     }
  136.    
  137.     public static int getH()
  138.     {
  139.         return heap[1];
  140.     }
  141.    
  142.     public static int getV()
  143.     {
  144.         return ver[1];
  145.     }
  146.    
  147.     public static void delete()
  148.     {
  149.         heap[1] = heap[heapSize];
  150.         ver[1] = ver[heapSize];
  151.         --heapSize;
  152.         reheapify_down(1);
  153.     }
  154.    
  155.     public static void add(int cost, int v)
  156.     {
  157.         heap[++heapSize] = cost;
  158.         ver[heapSize] = v;
  159.         reheapify_up(heapSize);
  160.     }
  161. }
  162.  
  163.  
  164. class Pair{
  165.     public int to, cost;
  166. }
  167.    
  168.  
  169. class InputReader {
  170.         public BufferedReader reader;
  171.         public StringTokenizer tokenizer;
  172.  
  173.         public InputReader(InputStream stream) throws FileNotFoundException {
  174.             reader = new BufferedReader(new InputStreamReader(new FileInputStream("distance.in")));
  175.             tokenizer = null;
  176.         }
  177.  
  178.         public String next() {
  179.             while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  180.                 try {
  181.                     tokenizer = new StringTokenizer(reader.readLine());
  182.                 } catch (IOException e) {
  183.                     throw new RuntimeException(e);
  184.                 }
  185.             }
  186.             return tokenizer.nextToken();
  187.         }
  188.  
  189.         public int nextInt() {
  190.             return Integer.parseInt(next());
  191.         }
  192.        
  193.         public long nextLong(){
  194.             return Long.parseLong(next());
  195.         }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement