Submission #3769521
Source Code Expand
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.util.Collection; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.Queue; import java.util.LinkedList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); DTransitTreePath solver = new DTransitTreePath(); solver.solve(1, in, out); out.close(); } static class DTransitTreePath { int[][] weights; List<Integer>[] graph; long[] dist; boolean[] visited; public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); graph = new ArrayList[n + 1]; weights = new int[n + 1][n + 1]; dist = new long[n + 1]; visited = new boolean[n + 1]; for (int i = 0; i < n + 1; i++) { graph[i] = new ArrayList<>(); } for (int i = 1; i < n; i++) { int u = in.nextInt(); int v = in.nextInt(); int w = in.nextInt(); graph[u].add(v); graph[v].add(u); weights[u][v] = w; weights[v][u] = w; } Queue<Integer> q = new LinkedList<>(); int Q = in.nextInt(); int k = in.nextInt(); q.add(k); dist[k] = 0; visited[k] = true; while (!q.isEmpty()) { int u = q.poll(); for (int v : graph[u]) { if (!visited[v]) { dist[v] = dist[u] + weights[u][v]; visited[v] = true; q.add(v); } } } for (int i = 0; i < Q; i++) { int u = in.nextInt(); int v = in.nextInt(); long distance = dist[u] + dist[v]; out.println(distance); } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void println(long i) { writer.println(i); } } }
Submission Info
Submission Time | |
---|---|
Task | D - Transit Tree Path |
User | rogernadal |
Language | Java8 (OpenJDK 1.8.0) |
Score | 0 |
Code Size | 5085 Byte |
Status | MLE |
Exec Time | 594 ms |
Memory | 874324 KB |
Compile Error
Note: ./Main.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.
Judge Result
Set Name | Sample | All | ||||||
---|---|---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 0 / 400 | ||||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | sample_01.txt, sample_02.txt, sample_03.txt |
All | sample_01.txt, sample_02.txt, sample_03.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_12.txt, subtask_1_13.txt, subtask_1_14.txt, subtask_1_15.txt, subtask_1_16.txt, subtask_1_17.txt, subtask_1_18.txt, subtask_1_19.txt, subtask_1_2.txt, subtask_1_20.txt, subtask_1_21.txt, subtask_1_22.txt, subtask_1_23.txt, subtask_1_24.txt, subtask_1_25.txt, subtask_1_26.txt, subtask_1_27.txt, subtask_1_28.txt, subtask_1_3.txt, subtask_1_4.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
sample_01.txt | AC | 76 ms | 18644 KB |
sample_02.txt | AC | 76 ms | 17620 KB |
sample_03.txt | AC | 79 ms | 20564 KB |
subtask_1_1.txt | AC | 75 ms | 19924 KB |
subtask_1_10.txt | AC | 159 ms | 26644 KB |
subtask_1_11.txt | AC | 90 ms | 21460 KB |
subtask_1_12.txt | AC | 103 ms | 22868 KB |
subtask_1_13.txt | AC | 114 ms | 22228 KB |
subtask_1_14.txt | AC | 78 ms | 20820 KB |
subtask_1_15.txt | AC | 184 ms | 161332 KB |
subtask_1_16.txt | MLE | 587 ms | 864724 KB |
subtask_1_17.txt | MLE | 585 ms | 870100 KB |
subtask_1_18.txt | MLE | 580 ms | 870356 KB |
subtask_1_19.txt | MLE | 580 ms | 871380 KB |
subtask_1_2.txt | AC | 101 ms | 25172 KB |
subtask_1_20.txt | MLE | 582 ms | 867540 KB |
subtask_1_21.txt | MLE | 582 ms | 870100 KB |
subtask_1_22.txt | MLE | 578 ms | 869204 KB |
subtask_1_23.txt | MLE | 586 ms | 867412 KB |
subtask_1_24.txt | MLE | 594 ms | 867668 KB |
subtask_1_25.txt | MLE | 589 ms | 870612 KB |
subtask_1_26.txt | MLE | 586 ms | 872788 KB |
subtask_1_27.txt | MLE | 587 ms | 871632 KB |
subtask_1_28.txt | MLE | 583 ms | 867156 KB |
subtask_1_3.txt | MLE | 589 ms | 874324 KB |
subtask_1_4.txt | AC | 78 ms | 21332 KB |
subtask_1_5.txt | AC | 178 ms | 29848 KB |
subtask_1_6.txt | MLE | 348 ms | 453440 KB |
subtask_1_7.txt | AC | 168 ms | 27796 KB |
subtask_1_8.txt | AC | 78 ms | 21332 KB |
subtask_1_9.txt | MLE | 581 ms | 866896 KB |