結果
問題 | No.1565 Union |
ユーザー | NASU41 |
提出日時 | 2021-07-09 21:17:41 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 970 ms / 2,000 ms |
コード長 | 17,536 bytes |
コンパイル時間 | 3,395 ms |
コンパイル使用メモリ | 100,772 KB |
実行使用メモリ | 96,440 KB |
最終ジャッジ日時 | 2024-06-10 15:33:22 |
合計ジャッジ時間 | 17,435 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 68 ms
51,116 KB |
testcase_01 | AC | 49 ms
50,612 KB |
testcase_02 | AC | 63 ms
51,184 KB |
testcase_03 | AC | 68 ms
51,336 KB |
testcase_04 | AC | 50 ms
50,596 KB |
testcase_05 | AC | 71 ms
51,336 KB |
testcase_06 | AC | 84 ms
51,288 KB |
testcase_07 | AC | 75 ms
51,364 KB |
testcase_08 | AC | 48 ms
50,428 KB |
testcase_09 | AC | 67 ms
50,996 KB |
testcase_10 | AC | 970 ms
85,116 KB |
testcase_11 | AC | 394 ms
76,228 KB |
testcase_12 | AC | 641 ms
81,612 KB |
testcase_13 | AC | 259 ms
61,272 KB |
testcase_14 | AC | 542 ms
80,344 KB |
testcase_15 | AC | 717 ms
91,172 KB |
testcase_16 | AC | 272 ms
64,880 KB |
testcase_17 | AC | 766 ms
80,032 KB |
testcase_18 | AC | 757 ms
80,072 KB |
testcase_19 | AC | 770 ms
86,176 KB |
testcase_20 | AC | 380 ms
94,328 KB |
testcase_21 | AC | 414 ms
83,712 KB |
testcase_22 | AC | 426 ms
94,436 KB |
testcase_23 | AC | 438 ms
84,104 KB |
testcase_24 | AC | 499 ms
94,592 KB |
testcase_25 | AC | 506 ms
84,456 KB |
testcase_26 | AC | 522 ms
96,440 KB |
testcase_27 | AC | 513 ms
85,972 KB |
testcase_28 | AC | 567 ms
94,764 KB |
testcase_29 | AC | 532 ms
84,788 KB |
ソースコード
import java.util.*; public class Main { public static void main(String[] args) throws Exception{ final ContestScanner sc = new ContestScanner(); final ContestPrinter pr = new ContestPrinter(); final int N = sc.nextInt(), M = sc.nextInt(); UndirectedGraph graph = new UndirectedGraph(N); for(int m=0; m<M; m++){ graph.addEdge(sc.nextInt()-1, sc.nextInt()-1); } long distance = graph.dijkstra(0)[N-1]; if(distance > M+100) pr.println(-1); else pr.println(distance); pr.close(); } } class UndirectedGraph{ private static long INF = 1L << 60; class Edge implements Comparable<Edge>{ int u,v; long length; public Edge(int u, int v, long l){ this.u = Math.min(u, v); this.v = Math.max(u, v); this.length = l; } public long getLength(){return length;} public int getLeftVertex(){return u;} public int getRightVertex(){return v;} public int getOtherVertex(int x){ return u+v-x; } public String toString(){ return String.format("[%d<->%d, %d]", u, v, length); } public int compareTo(Edge o){ return java.util.Comparator.comparingLong(Edge::getLength) .thenComparingInt(Edge::getLeftVertex) .thenComparingInt(Edge::getRightVertex) .compare(this, o); } } int V, E; java.util.List<Edge>[] edge; java.util.ArrayList<Edge> allEdge; public UndirectedGraph(int V){ this.V = V; this.E = 0; allEdge = new java.util.ArrayList<>(); edge = new java.util.ArrayList[V]; for(int v=0; v<V; v++) edge[v] = new java.util.ArrayList<>(); } public void addEdge(int u, int v, long length){ E++; Edge e = new Edge(u, v, length); allEdge.add(e); edge[u].add(e); edge[v].add(e); } public void addEdge(int u, int v){ addEdge(u, v, 1L); } public java.util.List<Edge> getEdges(int v){return edge[v];} public java.util.List<Edge> getAllEdges(){return allEdge;} public long[] dijkstra(int start){ long[] ans = new long[V]; java.util.Arrays.fill(ans, INF); java.util.PriorityQueue<Pair<Long,Integer>> queue = new java.util.PriorityQueue<>(); queue.add(new Pair<>(0L, start)); while(!queue.isEmpty()){ Pair<Long,Integer> top = queue.poll(); long d = top.getFirst(); int v = top.getSecond(); if(d > ans[v]) continue; ans[v] = d; for(Edge e: edge[v]){ if(d + e.getLength() < ans[e.getOtherVertex(v)]){ queue.add(new Pair<>(d + e.getLength(), e.getOtherVertex(v))); } } } return ans; } public long[] bellmanford(int start) throws Exception{ long[] distance = new long[V]; java.util.Arrays.fill(distance, INF); distance[start] = 0; for(int i=0; i<V; i++){ for(Edge e: allEdge){ int u = e.getLeftVertex(), v = e.getRightVertex(); long l = e.getLength(); if(distance[v] > distance[u] + l) distance[v] = distance[u] + l; } } for(Edge e: allEdge){ int u = e.getLeftVertex(), v = e.getRightVertex(); long l = e.getLength(); if(distance[v] > distance[u] + l) throw new Exception("Bellman-Ford algorithm: negative-weight cycle found"); } return distance; } public long[][] warshallfloyd(){ long[][] ans = new long[V][V]; for(int i=0; i<V; i++)for(int j=0; j<V; j++){ ans[i][j] = INF; } for(Edge e: allEdge){ ans[e.getLeftVertex()][e.getRightVertex()] = e.getLength(); ans[e.getRightVertex()][e.getLeftVertex()] = e.getLength(); } for(int k=0; k<V; k++)for(int i=0; i<V; i++)for(int j=0; j<V; j++){ ans[i][j] = Math.min(ans[i][j], ans[i][k]+ans[k][j]); } return ans; } public long[] bfs01(int start){ long[] ans = new long[V]; java.util.Arrays.fill(ans, INF); ans[start] = 0; java.util.LinkedList<Integer> queue = new java.util.LinkedList<>(); queue.add(start); while(!queue.isEmpty()){ int v = queue.pollFirst(); for(Edge e: getEdges(v)) if(ans[e.getOtherVertex(v)] > ans[v] + e.getLength()){ ans[e.getOtherVertex(v)] = ans[v] + e.getLength(); if(e.getLength() > 0) queue.addLast(e.getOtherVertex(v)); else queue.addFirst(e.getOtherVertex(v)); } } return ans; } public UndirectedGraph kruskal() throws Exception{ UndirectedGraph ans = new UndirectedGraph(V); java.util.Collections.sort(allEdge); DSU uf = new DSU(V); for(Edge e:allEdge){ if(!uf.same(e.getLeftVertex(), e.getRightVertex())){ uf.merge(e.getLeftVertex(), e.getRightVertex()); ans.addEdge(e.getLeftVertex(), e.getRightVertex(), e.getLength()); } } if(uf.groups().size() > 1) throw new Exception("kruskal algorithm: the graph is disconnected"); return ans; } public String toString(){return allEdge.toString();} } class Pair<S extends Comparable<S>, T extends Comparable<T>> implements Comparable<Pair<S,T>>{ S first; T second; public Pair(S s, T t){ first = s; second = t; } public S getFirst(){return first;} public T getSecond(){return second;} public boolean equals(Object another){ if(this==another) return true; if(!(another instanceof Pair)) return false; Pair otherPair = (Pair)another; return this.first.equals(otherPair.first) && this.second.equals(otherPair.second); } public int compareTo(Pair<S,T> another){ java.util.Comparator<Pair<S,T>> comp1 = java.util.Comparator.comparing(Pair::getFirst); java.util.Comparator<Pair<S,T>> comp2 = comp1.thenComparing(Pair::getSecond); return comp2.compare(this, another); } public int hashCode(){ return first.hashCode() * 10007 + second.hashCode(); } public String toString(){ return String.format("(%s, %s)", first, second); } } class DSU { private int n; private int[] parentOrSize; public DSU(int n) { this.n = n; this.parentOrSize = new int[n]; java.util.Arrays.fill(parentOrSize, -1); } int merge(int a, int b) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("a=" + a); if (!(0 <= b && b < n)) throw new IndexOutOfBoundsException("b=" + b); int x = leader(a); int y = leader(b); if (x == y) return x; if (-parentOrSize[x] < -parentOrSize[y]) { int tmp = x; x = y; y = tmp; } parentOrSize[x] += parentOrSize[y]; parentOrSize[y] = x; return x; } boolean same(int a, int b) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("a=" + a); if (!(0 <= b && b < n)) throw new IndexOutOfBoundsException("b=" + b); return leader(a) == leader(b); } int leader(int a) { if (parentOrSize[a] < 0) { return a; } else { parentOrSize[a] = leader(parentOrSize[a]); return parentOrSize[a]; } } int size(int a) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("" + a); return -parentOrSize[leader(a)]; } java.util.ArrayList<java.util.ArrayList<Integer>> groups() { int[] leaderBuf = new int[n]; int[] groupSize = new int[n]; for (int i = 0; i < n; i++) { leaderBuf[i] = leader(i); groupSize[leaderBuf[i]]++; } java.util.ArrayList<java.util.ArrayList<Integer>> result = new java.util.ArrayList<>(n); for (int i = 0; i < n; i++) { result.add(new java.util.ArrayList<>(groupSize[i])); } for (int i = 0; i < n; i++) { result.get(leaderBuf[i]).add(i); } result.removeIf(java.util.ArrayList::isEmpty); return result; } } class ContestScanner { private final java.io.InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private static final long LONG_MAX_TENTHS = 922337203685477580L; private static final int LONG_MAX_LAST_DIGIT = 7; private static final int LONG_MIN_LAST_DIGIT = 8; public ContestScanner(java.io.InputStream in){ this.in = in; } public ContestScanner(){ this(System.in); } private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (java.io.IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new java.util.NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new java.util.NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { int digit = b - '0'; if (n >= LONG_MAX_TENTHS) { if (n == LONG_MAX_TENTHS) { if (minus) { if (digit <= LONG_MIN_LAST_DIGIT) { n = -n * 10 - digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } else { if (digit <= LONG_MAX_LAST_DIGIT) { n = n * 10 + digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } } throw new ArithmeticException( String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit) ); } n = n * 10 + digit; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public long[] nextLongArray(int length){ long[] array = new long[length]; for(int i=0; i<length; i++) array[i] = this.nextLong(); return array; } public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map){ long[] array = new long[length]; for(int i=0; i<length; i++) array[i] = map.applyAsLong(this.nextLong()); return array; } public int[] nextIntArray(int length){ int[] array = new int[length]; for(int i=0; i<length; i++) array[i] = this.nextInt(); return array; } public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map){ int[] array = new int[length]; for(int i=0; i<length; i++) array[i] = map.applyAsInt(this.nextInt()); return array; } public double[] nextDoubleArray(int length){ double[] array = new double[length]; for(int i=0; i<length; i++) array[i] = this.nextDouble(); return array; } public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map){ double[] array = new double[length]; for(int i=0; i<length; i++) array[i] = map.applyAsDouble(this.nextDouble()); return array; } public long[][] nextLongMatrix(int height, int width){ long[][] mat = new long[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextLong(); } return mat; } public int[][] nextIntMatrix(int height, int width){ int[][] mat = new int[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextInt(); } return mat; } public double[][] nextDoubleMatrix(int height, int width){ double[][] mat = new double[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextDouble(); } return mat; } public char[][] nextCharMatrix(int height, int width){ char[][] mat = new char[height][width]; for(int h=0; h<height; h++){ String s = this.next(); for(int w=0; w<width; w++){ mat[h][w] = s.charAt(w); } } return mat; } } class ContestPrinter extends java.io.PrintWriter{ public ContestPrinter(java.io.PrintStream stream){ super(stream); } public ContestPrinter(){ super(System.out); } private static String dtos(double x, int n) { StringBuilder sb = new StringBuilder(); if(x < 0){ sb.append('-'); x = -x; } x += Math.pow(10, -n)/2; sb.append((long)x); sb.append("."); x -= (long)x; for(int i = 0;i < n;i++){ x *= 10; sb.append((int)x); x -= (int)x; } return sb.toString(); } @Override public void print(float f){ super.print(dtos(f, 20)); } @Override public void println(float f){ super.println(dtos(f, 20)); } @Override public void print(double d){ super.print(dtos(d, 20)); } @Override public void println(double d){ super.println(dtos(d, 20)); } public void printArray(int[] array, String separator){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(array[i]); super.print(separator); } super.println(array[n-1]); } public void printArray(int[] array){ this.printArray(array, " "); } public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(map.applyAsInt(array[i])); super.print(separator); } super.println(map.applyAsInt(array[n-1])); } public void printArray(int[] array, java.util.function.IntUnaryOperator map){ this.printArray(array, " ", map); } public void printArray(long[] array, String separator){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(array[i]); super.print(separator); } super.println(array[n-1]); } public void printArray(long[] array){ this.printArray(array, " "); } public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(map.applyAsLong(array[i])); super.print(separator); } super.println(map.applyAsLong(array[n-1])); } public void printArray(long[] array, java.util.function.LongUnaryOperator map){ this.printArray(array, " ", map); } }