結果
問題 | No.468 役に立つ競技プログラミング実践編 |
ユーザー | uafr_cs |
提出日時 | 2017-11-01 23:33:46 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,706 ms / 2,000 ms |
コード長 | 3,962 bytes |
コンパイル時間 | 2,879 ms |
コンパイル使用メモリ | 87,496 KB |
実行使用メモリ | 135,184 KB |
最終ジャッジ日時 | 2024-05-02 04:24:08 |
合計ジャッジ時間 | 22,336 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 81 ms
40,416 KB |
testcase_01 | AC | 81 ms
40,472 KB |
testcase_02 | AC | 88 ms
40,040 KB |
testcase_03 | AC | 96 ms
40,496 KB |
testcase_04 | AC | 91 ms
40,240 KB |
testcase_05 | AC | 100 ms
40,436 KB |
testcase_06 | AC | 104 ms
40,952 KB |
testcase_07 | AC | 108 ms
40,824 KB |
testcase_08 | AC | 99 ms
40,352 KB |
testcase_09 | AC | 97 ms
40,628 KB |
testcase_10 | AC | 100 ms
40,832 KB |
testcase_11 | AC | 99 ms
40,776 KB |
testcase_12 | AC | 96 ms
40,844 KB |
testcase_13 | AC | 99 ms
40,808 KB |
testcase_14 | AC | 179 ms
42,936 KB |
testcase_15 | AC | 179 ms
42,716 KB |
testcase_16 | AC | 178 ms
43,000 KB |
testcase_17 | AC | 178 ms
42,760 KB |
testcase_18 | AC | 181 ms
42,968 KB |
testcase_19 | AC | 170 ms
42,832 KB |
testcase_20 | AC | 199 ms
42,880 KB |
testcase_21 | AC | 165 ms
42,532 KB |
testcase_22 | AC | 189 ms
43,204 KB |
testcase_23 | AC | 176 ms
42,828 KB |
testcase_24 | AC | 1,706 ms
134,476 KB |
testcase_25 | AC | 1,400 ms
135,036 KB |
testcase_26 | AC | 1,431 ms
135,148 KB |
testcase_27 | AC | 1,389 ms
135,160 KB |
testcase_28 | AC | 1,423 ms
135,124 KB |
testcase_29 | AC | 1,361 ms
134,528 KB |
testcase_30 | AC | 1,424 ms
135,184 KB |
testcase_31 | AC | 1,491 ms
134,752 KB |
testcase_32 | AC | 1,405 ms
135,080 KB |
testcase_33 | AC | 1,398 ms
135,184 KB |
testcase_34 | AC | 906 ms
115,960 KB |
testcase_35 | AC | 125 ms
41,640 KB |
testcase_36 | AC | 82 ms
40,332 KB |
ソースコード
import java.io.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.StringTokenizer; import java.util.Map.Entry; import java.util.Scanner; public class Main { public static final long INF = Long.MAX_VALUE / 2 - 1; public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); final int N = sc.nextInt(); final int M = sc.nextInt(); ArrayList<HashMap<Integer, Long>> fwd_adj = new ArrayList<HashMap<Integer, Long>>(); ArrayList<HashMap<Integer, Long>> rev_adj = new ArrayList<HashMap<Integer, Long>>(); for(int i = 0; i < N; i++){ fwd_adj.add(new HashMap<Integer, Long>()); rev_adj.add(new HashMap<Integer, Long>()); } int[] in_degs = new int[N]; int[] out_degs = new int[N]; for(int i = 0; i < M; i++){ final int A = sc.nextInt(); final int B = sc.nextInt(); final long C = sc.nextLong(); fwd_adj.get(A).put(B, C); rev_adj.get(B).put(A, C); in_degs[B]++; out_degs[A]++; } long[] max_path = new long[N]; long[] rev_max_path = new long[N]; Arrays.fill(max_path, -1); Arrays.fill(rev_max_path, -1); max_path[0] = 0; LinkedList<Integer> queue = new LinkedList<>(); for(int i = 0; i < N; i++){ if(in_degs[i] != 0){ continue; } queue.add(i); } while(!queue.isEmpty()){ final int node = queue.poll(); for(final Entry<Integer, Long> entry : fwd_adj.get(node).entrySet()){ final int next = entry.getKey(); final long cost = max_path[node] + entry.getValue(); if(max_path[next] < 0 || max_path[next] < cost){ max_path[next] = cost; } in_degs[next]--; if(in_degs[next] == 0){ queue.add(next); } } } rev_max_path[N - 1] = max_path[N - 1]; for(int i = 0; i < N; i++){ if(out_degs[i] != 0){ continue; } queue.add(i); } while(!queue.isEmpty()){ final int node = queue.poll(); for(final Entry<Integer, Long> entry : rev_adj.get(node).entrySet()){ final int prev = entry.getKey(); final long cost = rev_max_path[node] - entry.getValue(); if(rev_max_path[prev] < 0 || rev_max_path[prev] > cost){ rev_max_path[prev] = cost; } out_degs[prev]--; if(out_degs[prev] == 0){ queue.add(prev); } } } int count = 0; for(int i = 0; i < N; i++){ if(max_path[i] != rev_max_path[i]){ count++; } } System.out.println(max_path[N-1] + " " + count + "/" + N); //System.out.println(Arrays.toString(max_path)); //System.out.println(Arrays.toString(rev_max_path)); } public static class Scanner implements Closeable { private BufferedReader br; private StringTokenizer tok; public Scanner(InputStream is) throws IOException { br = new BufferedReader(new InputStreamReader(is)); } private void getLine() throws IOException { while (!hasNext()) { tok = new StringTokenizer(br.readLine()); } } private boolean hasNext() { return tok != null && tok.hasMoreTokens(); } public String next() throws IOException { getLine(); return tok.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public int[] nextIntArray(int n) throws IOException { final int[] ret = new int[n]; for (int i = 0; i < n; i++) { ret[i] = this.nextInt(); } return ret; } public long[] nextLongArray(int n) throws IOException { final long[] ret = new long[n]; for (int i = 0; i < n; i++) { ret[i] = this.nextLong(); } return ret; } public void close() throws IOException { br.close(); } } }