結果

問題 No.1301 Strange Graph Shortest Path
ユーザー tentententen
提出日時 2023-06-07 10:18:52
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 4,438 bytes
コンパイル時間 2,435 ms
コンパイル使用メモリ 84,848 KB
実行使用メモリ 90,204 KB
最終ジャッジ日時 2024-06-09 09:40:21
合計ジャッジ時間 29,170 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
37,368 KB
testcase_01 AC 52 ms
37,368 KB
testcase_02 WA -
testcase_03 AC 673 ms
67,028 KB
testcase_04 WA -
testcase_05 AC 710 ms
69,700 KB
testcase_06 WA -
testcase_07 AC 748 ms
72,788 KB
testcase_08 AC 683 ms
67,588 KB
testcase_09 AC 718 ms
69,620 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 738 ms
67,668 KB
testcase_14 AC 676 ms
68,604 KB
testcase_15 AC 736 ms
72,452 KB
testcase_16 AC 869 ms
77,444 KB
testcase_17 WA -
testcase_18 AC 730 ms
69,244 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 757 ms
70,692 KB
testcase_24 WA -
testcase_25 AC 815 ms
76,280 KB
testcase_26 AC 760 ms
70,348 KB
testcase_27 AC 794 ms
71,604 KB
testcase_28 AC 694 ms
66,688 KB
testcase_29 WA -
testcase_30 AC 794 ms
75,184 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