Submission #1989384
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; 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; } } 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() { int size = this.table.length; for (int i = 0; i < size; i++) { this.table[i] = i; this.rank[i] = 1; } } } 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 | 6437 Byte |
Status | TLE |
Exec Time | 3161 ms |
Memory | 110616 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 0 / 600 | ||||
Status | AC |
|
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 | 153 ms | 26196 KB |
02.txt | AC | 617 ms | 65196 KB |
03.txt | TLE | 3160 ms | 65384 KB |
04.txt | AC | 1070 ms | 103088 KB |
05.txt | AC | 953 ms | 98732 KB |
06.txt | AC | 499 ms | 55240 KB |
07.txt | AC | 607 ms | 79292 KB |
08.txt | TLE | 3161 ms | 66984 KB |
09.txt | AC | 965 ms | 90660 KB |
10.txt | AC | 667 ms | 77600 KB |
11.txt | AC | 891 ms | 103872 KB |
12.txt | AC | 532 ms | 65964 KB |
13.txt | AC | 704 ms | 69136 KB |
14.txt | AC | 892 ms | 103884 KB |
15.txt | AC | 887 ms | 108236 KB |
16.txt | AC | 891 ms | 101496 KB |
17.txt | AC | 894 ms | 103964 KB |
18.txt | AC | 572 ms | 68380 KB |
19.txt | AC | 850 ms | 95284 KB |
20.txt | AC | 1077 ms | 110616 KB |
21.txt | TLE | 3161 ms | 63224 KB |
22.txt | AC | 1115 ms | 106196 KB |
23.txt | AC | 904 ms | 101980 KB |
24.txt | AC | 792 ms | 101908 KB |
25.txt | AC | 494 ms | 70792 KB |
26.txt | AC | 607 ms | 62848 KB |
27.txt | AC | 938 ms | 108656 KB |
28.txt | AC | 926 ms | 108640 KB |
29.txt | AC | 896 ms | 105960 KB |
30.txt | AC | 860 ms | 96984 KB |
31.txt | AC | 585 ms | 67304 KB |
32.txt | AC | 933 ms | 108544 KB |
33.txt | AC | 971 ms | 107096 KB |
34.txt | AC | 949 ms | 100672 KB |
35.txt | AC | 482 ms | 64376 KB |
36.txt | AC | 613 ms | 64648 KB |
37.txt | AC | 966 ms | 105592 KB |
38.txt | AC | 961 ms | 103544 KB |
sample_01.txt | AC | 150 ms | 23764 KB |
sample_02.txt | AC | 145 ms | 22100 KB |
sample_03.txt | AC | 152 ms | 24272 KB |
w1.txt | AC | 1039 ms | 106044 KB |
w10.txt | TLE | 3157 ms | 64464 KB |
w11.txt | AC | 628 ms | 69956 KB |
w12.txt | AC | 582 ms | 69516 KB |
w13.txt | AC | 675 ms | 64960 KB |
w14.txt | AC | 935 ms | 102836 KB |
w15.txt | AC | 551 ms | 68136 KB |
w16.txt | AC | 567 ms | 64232 KB |
w17.txt | AC | 622 ms | 65688 KB |
w18.txt | AC | 651 ms | 66640 KB |
w2.txt | AC | 927 ms | 103908 KB |
w3.txt | AC | 974 ms | 102972 KB |
w4.txt | AC | 832 ms | 104028 KB |
w5.txt | TLE | 3161 ms | 67260 KB |
w6.txt | AC | 907 ms | 97396 KB |
w7.txt | AC | 1042 ms | 105040 KB |
w8.txt | AC | 1206 ms | 106244 KB |
w9.txt | AC | 1223 ms | 106036 KB |