結果

問題 No.1301 Strange Graph Shortest Path
ユーザー tentententen
提出日時 2023-06-07 10:18:52
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 4,438 bytes
コンパイル時間 2,780 ms
コンパイル使用メモリ 93,640 KB
実行使用メモリ 93,908 KB
最終ジャッジ日時 2023-08-28 14:30:00
合計ジャッジ時間 33,469 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,620 KB
testcase_01 AC 43 ms
49,636 KB
testcase_02 WA -
testcase_03 AC 686 ms
79,592 KB
testcase_04 WA -
testcase_05 AC 792 ms
79,540 KB
testcase_06 WA -
testcase_07 AC 849 ms
78,748 KB
testcase_08 AC 703 ms
79,196 KB
testcase_09 AC 779 ms
80,048 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 810 ms
82,148 KB
testcase_14 AC 837 ms
77,876 KB
testcase_15 AC 786 ms
80,324 KB
testcase_16 AC 1,010 ms
91,304 KB
testcase_17 WA -
testcase_18 AC 787 ms
81,724 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 853 ms
75,988 KB
testcase_24 WA -
testcase_25 AC 890 ms
89,972 KB
testcase_26 AC 796 ms
84,888 KB
testcase_27 AC 848 ms
86,372 KB
testcase_28 AC 747 ms
81,212 KB
testcase_29 WA -
testcase_30 AC 833 ms
86,872 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        ArrayList<ArrayList<Path>> graph = new ArrayList<>();
        Path[] pathes = new Path[m * 2];
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            int c = sc.nextInt();
            int d = sc.nextInt();
            pathes[i * 2] = new Path(i * 2, b, a, c, d);
            graph.get(a).add(pathes[i * 2]);
            pathes[i * 2 + 1] = new Path(i * 2 + 1, a, b, c, d);
            graph.get(b).add(pathes[i * 2 + 1]);
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        queue.add(new Path(-1, 0, -1, 0, 0));
        long[] costs1 = new long[n];
        Arrays.fill(costs1, Long.MAX_VALUE);
        int[] idxes = new int[n];
        int[] pIdxes = new int[n];
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (costs1[p.idx] <= p.value) {
                continue;
            }
            costs1[p.idx] = p.value;
            idxes[p.idx] = p.last;
            pIdxes[p.idx] = p.path;
            for (Path x : graph.get(p.idx)) {
                queue.add(new Path(x.path, x.idx, p.idx, p.value + x.value, 0));
            }
        }
        int current = n - 1;
        while (current > 0) {
            int p = pIdxes[current];
            if (p % 2 == 0) {
                pathes[p].up();
                pathes[p + 1].up();
            } else {
                pathes[p].up();
                pathes[p + 1].up();
            }
            current = idxes[current];
        }
        long[] costs2 = new long[n];
        Arrays.fill(costs2, Long.MAX_VALUE);
        queue.add(new Path(n - 1, costs1[n - 1]));
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (costs2[p.idx] <= p.value) {
                continue;
            }
            costs2[p.idx] = p.value;
            for (Path x : graph.get(p.idx)) {
                queue.add(new Path(x.idx, p.value + x.value));
            }
        }
        System.out.println(costs2[0]);
    }
    
    static class Path implements Comparable<Path> {
        int path;
        int idx;
        int last;
        long value;
        int sub;
        
        public Path(int path, int idx, int last, long value, int sub) {
            this.path = path;
            this.idx = idx;
            this.last = last;
            this.value = value;
            this.sub = sub;
        }
        
        public Path(int idx, long value) {
            this.idx = idx;
            this.value = value;
        }
        
        public int compareTo(Path another) {
            if (value == another.value) {
                return 0;
            } else if (value < another.value) {
                return -1;
            } else {
                return 1;
            }
        }
        
        public void up() {
            value = sub;
        }
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0