結果

問題 No.807 umg tours
ユーザー tentententen
提出日時 2024-03-07 15:38:14
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,555 ms / 4,000 ms
コード長 2,844 bytes
コンパイル時間 2,369 ms
コンパイル使用メモリ 84,188 KB
実行使用メモリ 128,588 KB
最終ジャッジ日時 2024-03-07 15:38:41
合計ジャッジ時間 23,379 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
55,008 KB
testcase_01 AC 93 ms
55,388 KB
testcase_02 AC 105 ms
56,156 KB
testcase_03 AC 102 ms
56,032 KB
testcase_04 AC 83 ms
55,256 KB
testcase_05 AC 89 ms
55,244 KB
testcase_06 AC 105 ms
56,056 KB
testcase_07 AC 102 ms
56,164 KB
testcase_08 AC 70 ms
54,880 KB
testcase_09 AC 75 ms
55,128 KB
testcase_10 AC 83 ms
54,880 KB
testcase_11 AC 1,243 ms
108,304 KB
testcase_12 AC 1,001 ms
96,236 KB
testcase_13 AC 1,332 ms
114,400 KB
testcase_14 AC 720 ms
80,356 KB
testcase_15 AC 651 ms
77,528 KB
testcase_16 AC 1,352 ms
115,612 KB
testcase_17 AC 1,555 ms
126,260 KB
testcase_18 AC 1,539 ms
128,588 KB
testcase_19 AC 1,498 ms
120,524 KB
testcase_20 AC 865 ms
93,148 KB
testcase_21 AC 868 ms
91,808 KB
testcase_22 AC 574 ms
69,996 KB
testcase_23 AC 520 ms
69,136 KB
testcase_24 AC 1,162 ms
109,588 KB
testcase_25 AC 1,502 ms
121,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        List<List<Path>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
        int m = sc.nextInt();
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            int c = sc.nextInt();
            graph.get(a).add(new Path(0, b, c));
            graph.get(b).add(new Path(0, a, c));
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        queue.add(new Path(0, 0, 0));
        long[][] costs = new long[2][n];
        for (long[] arr : costs) {
            Arrays.fill(arr, Long.MAX_VALUE);
        }
        costs[1][0] = 0;
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (costs[p.type][p.idx] <= p.value) {
                continue;
            }
            costs[p.type][p.idx] = p.value;
            for (Path x : graph.get(p.idx)) {
                queue.add(new Path(p.type, x.idx, p.value + x.value));
                if (p.type == 0) {
                    queue.add(new Path(1, x.idx, p.value));
                }
            }
        }
        String result = IntStream.range(0, n)
            .mapToObj(i -> String.valueOf(costs[0][i] + costs[1][i]))
            .collect(Collectors.joining("\n"));
        System.out.println(result);
    }
    
    static class Path implements Comparable<Path> {
        int type;
        int idx;
        long value;
        
        public Path(int type, int idx, long value) {
            this.idx = idx;
            this.type = type;
            this.value = value;
        }
        
        public int compareTo(Path another) {
            return Long.compare(value, another.value);
        }
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}
0