Submission #1989397


Source Code Expand

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;


class UnionFind {
  private int[] table;
  private int[] rank;
  private int[] orgTable;
  private int[] orgRank;
  private int size;

  public UnionFind(int size) {
    this.table = new int[size];
    this.rank = new int[size];
    for (int i = 0; i < size; i++) {
      this.table[i] = i;
      this.rank[i] = 1;
    }
    this.size = size;
    orgTable = Arrays.copyOf(table, size);
    orgRank = Arrays.copyOf(rank, size);
  }

  public boolean isSame(int node1, int node2) {
    return find(node1) == find(node2);
  }

  public int find(int node) {
    if (table[node] == node) {
      return node;
    } else {
      return table[node] = find(table[node]);
    }
  }

  public void union(int node1, int node2) {
    int root1 = find(node1);
    int root2 = find(node2);

    if (rank[root1] < rank[root2]) {
      table[root1] = root2;
    } else if (rank[root1] > rank[root2]) {
      table[root2] = root1;
    } else if (root1 != root2) {
      table[root2] = root1;
      rank[root1]++;
    }
  }

  public void clear() {
    System.arraycopy(orgTable, 0, table, 0, size);
    System.arraycopy(orgRank, 0, rank, 0, size);
  }
}


public class Main {

  static List<List<int[]>> g = new ArrayList<>();

  private static void solve() {
    int n = ni();
    int m = ni();

    int[][] es = new int[m][];
    for (int i = 0; i < m; i++) {
      es[i] = new int[] {ni() - 1, ni() - 1, ni()};
    }
    Arrays.sort(es, (o1, o2) -> o1[2] - o2[2]);
    UnionFind uf = new UnionFind(n);

    int[] cen = new int[n];
    Arrays.fill(cen, -1);
    int id = n;
    int[] from = new int[2 * m];
    int[] to = new int[2 * m];
    int[] w = new int[2 * m];
    Arrays.fill(w, 1);
    int ep = 0;
    for (int i = 0; i < m;) {
      int j = i;
      while (j < m && es[j][2] == es[i][2])
        j++;

      for (int k = i; k < j; k++) {
        uf.union(es[k][0], es[k][1]);
      }

      for (int k = i; k < j; k++) {
        for (int u = 0; u < 2; u++) {
          int root = uf.find(es[k][u]);
          if (cen[root] == -1) {
            cen[root] = id;
            id++;
          }
          from[ep] = es[k][u];
          to[ep] = cen[root];
          ep++;
        }
      }
      for (int k = i; k < j; k ++) {
        for (int u = 0; u < 2; u ++) {
          cen[uf.find(es[k][u])] = -1;
        }
      }
      uf.clear();
      i = j;
    }

    int[][][] g = packWU(id, from, to, w, ep);
    int[] d = dijk(g, 0);
    if (d[n - 1] == Integer.MAX_VALUE) {
      System.out.println(-1);
    } else {
      System.out.println(d[n - 1] / 2);
    }
  }

  public static int[] dijk(int[][][] g, int from) {
    int n = g.length;
    int[] d = new int[n];
    Arrays.fill(d, Integer.MAX_VALUE);
    Queue<int[]> q = new PriorityQueue<int[]>(100000, new Comparator<int[]>() {
      public int compare(int[] a, int[] b) {
        if (a[1] - b[1] != 0)
          return a[1] - b[1];
        return a[0] - b[0];
      }
    });
    q.add(new int[] {from, 0});

    d[from] = 0;
    while (q.size() > 0) {
      int[] cur = q.poll();
      for (int[] e : g[cur[0]]) {
        int next = e[0];
        int nd = d[cur[0]] + e[1];
        if (nd < d[next]) {
          d[next] = nd;
          q.add(new int[] {next, nd});
        }
      }
    }
    return d;
  }



  public static int[][][] packWU(int n, int[] from, int[] to, int[] w, int sup) {
    int[][][] g = new int[n][][];
    int[] p = new int[n];
    for (int i = 0; i < sup; i++)
      p[from[i]]++;
    for (int i = 0; i < sup; i++)
      p[to[i]]++;
    for (int i = 0; i < n; i++)
      g[i] = new int[p[i]][2];
    for (int i = 0; i < sup; i++) {
      --p[from[i]];
      g[from[i]][p[from[i]]][0] = to[i];
      g[from[i]][p[from[i]]][1] = w[i];
      --p[to[i]];
      g[to[i]][p[to[i]]][0] = from[i];
      g[to[i]][p[to[i]]][1] = w[i];
    }
    return g;
  }

  public static void main(String[] args) {
    new Thread(null, new Runnable() {
      @Override
      public void run() {
        long start = System.currentTimeMillis();
        String debug = System.getProperty("debug");
        if (debug != null) {
          try {
            is = java.nio.file.Files.newInputStream(java.nio.file.Paths.get(debug));
          } catch (Exception e) {
            throw new RuntimeException(e);
          }
        }
        reader = new java.io.BufferedReader(new java.io.InputStreamReader(is), 32768);
        solve();
        out.flush();
        tr((System.currentTimeMillis() - start) + "ms");
      }
    }, "", 64000000).start();
  }

  private static java.io.InputStream is = System.in;
  private static java.io.PrintWriter out = new java.io.PrintWriter(System.out);
  private static java.util.StringTokenizer tokenizer = null;
  private static java.io.BufferedReader reader;

  public static String next() {
    while (tokenizer == null || !tokenizer.hasMoreTokens()) {
      try {
        tokenizer = new java.util.StringTokenizer(reader.readLine());
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }
    return tokenizer.nextToken();
  }

  private static double nd() {
    return Double.parseDouble(next());
  }

  private static long nl() {
    return Long.parseLong(next());
  }

  private static int[] na(int n) {
    int[] a = new int[n];
    for (int i = 0; i < n; i++)
      a[i] = ni();
    return a;
  }

  private static char[] ns() {
    return next().toCharArray();
  }

  private static long[] nal(int n) {
    long[] a = new long[n];
    for (int i = 0; i < n; i++)
      a[i] = nl();
    return a;
  }

  private static int[][] ntable(int n, int m) {
    int[][] table = new int[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        table[i][j] = ni();
      }
    }
    return table;
  }

  private static int[][] nlist(int n, int m) {
    int[][] table = new int[m][n];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        table[j][i] = ni();
      }
    }
    return table;
  }

  private static int ni() {
    return Integer.parseInt(next());
  }

  private static void tr(Object... o) {
    if (is != System.in)
      System.out.println(java.util.Arrays.deepToString(o));
  }
}

Submission Info

Submission Time
Task E - Snuke's Subway Trip
User hiromi_ayase
Language Java8 (OpenJDK 1.8.0)
Score 0
Code Size 6591 Byte
Status TLE
Exec Time 3161 ms
Memory 112076 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 600
Status AC
AC × 55
TLE × 4
Set Name Test Cases
Sample
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, sample_01.txt, sample_02.txt, sample_03.txt, w1.txt, w10.txt, w11.txt, w12.txt, w13.txt, w14.txt, w15.txt, w16.txt, w17.txt, w18.txt, w2.txt, w3.txt, w4.txt, w5.txt, w6.txt, w7.txt, w8.txt, w9.txt
Case Name Status Exec Time Memory
01.txt AC 160 ms 26452 KB
02.txt AC 1139 ms 67360 KB
03.txt TLE 3161 ms 67096 KB
04.txt AC 924 ms 99192 KB
05.txt AC 943 ms 98912 KB
06.txt AC 487 ms 56616 KB
07.txt AC 562 ms 76016 KB
08.txt TLE 3161 ms 66664 KB
09.txt AC 686 ms 81636 KB
10.txt AC 637 ms 78728 KB
11.txt AC 922 ms 100932 KB
12.txt AC 548 ms 66776 KB
13.txt AC 602 ms 65324 KB
14.txt AC 867 ms 103244 KB
15.txt AC 893 ms 112076 KB
16.txt AC 844 ms 102040 KB
17.txt AC 896 ms 105752 KB
18.txt AC 598 ms 68904 KB
19.txt AC 919 ms 104956 KB
20.txt AC 939 ms 102544 KB
21.txt TLE 3157 ms 66120 KB
22.txt AC 922 ms 103312 KB
23.txt AC 961 ms 99804 KB
24.txt AC 933 ms 107616 KB
25.txt AC 522 ms 70216 KB
26.txt AC 592 ms 64160 KB
27.txt AC 895 ms 109588 KB
28.txt AC 843 ms 104716 KB
29.txt AC 934 ms 108128 KB
30.txt AC 912 ms 109628 KB
31.txt AC 541 ms 66848 KB
32.txt AC 924 ms 107804 KB
33.txt AC 928 ms 107236 KB
34.txt AC 943 ms 105612 KB
35.txt AC 520 ms 68052 KB
36.txt AC 618 ms 67288 KB
37.txt AC 975 ms 106192 KB
38.txt AC 1004 ms 109920 KB
sample_01.txt AC 154 ms 25684 KB
sample_02.txt AC 142 ms 28116 KB
sample_03.txt AC 143 ms 24276 KB
w1.txt AC 1088 ms 104720 KB
w10.txt AC 2727 ms 105672 KB
w11.txt AC 637 ms 65704 KB
w12.txt AC 685 ms 65220 KB
w13.txt AC 754 ms 66512 KB
w14.txt AC 925 ms 103512 KB
w15.txt AC 538 ms 66392 KB
w16.txt AC 560 ms 67020 KB
w17.txt AC 591 ms 67124 KB
w18.txt AC 623 ms 64120 KB
w2.txt AC 842 ms 96844 KB
w3.txt AC 950 ms 102760 KB
w4.txt AC 820 ms 106304 KB
w5.txt TLE 3161 ms 69128 KB
w6.txt AC 1099 ms 101068 KB
w7.txt AC 1067 ms 102148 KB
w8.txt AC 1197 ms 105104 KB
w9.txt AC 1129 ms 107788 KB