結果

問題 No.807 umg tours
ユーザー neko_the_shadowneko_the_shadow
提出日時 2019-05-17 12:07:49
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,439 bytes
コンパイル時間 3,747 ms
コンパイル使用メモリ 95,264 KB
実行使用メモリ 167,480 KB
最終ジャッジ日時 2023-10-17 07:16:23
合計ジャッジ時間 49,859 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 204 ms
58,280 KB
testcase_01 AC 209 ms
58,048 KB
testcase_02 AC 220 ms
58,300 KB
testcase_03 AC 218 ms
58,232 KB
testcase_04 AC 210 ms
58,336 KB
testcase_05 AC 213 ms
58,180 KB
testcase_06 AC 224 ms
58,208 KB
testcase_07 AC 219 ms
58,208 KB
testcase_08 AC 153 ms
55,908 KB
testcase_09 AC 163 ms
57,748 KB
testcase_10 AC 193 ms
57,964 KB
testcase_11 AC 2,838 ms
135,000 KB
testcase_12 AC 2,881 ms
126,364 KB
testcase_13 AC 3,480 ms
145,532 KB
testcase_14 AC 2,003 ms
104,608 KB
testcase_15 AC 1,591 ms
97,088 KB
testcase_16 AC 3,341 ms
146,640 KB
testcase_17 AC 3,555 ms
162,392 KB
testcase_18 AC 3,568 ms
163,684 KB
testcase_19 AC 3,486 ms
167,480 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 TLE -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner stdin = new Scanner(System.in);
        int n = Integer.parseInt(stdin.next());
        int m = Integer.parseInt(stdin.next());
        
        Map<Integer, Map<Integer, Integer>> costs = new HashMap<>();
        for (int i = 0; i < m; i++) {
            int a = Integer.parseInt(stdin.next()) - 1;
            int b = Integer.parseInt(stdin.next()) - 1;
            int c = Integer.parseInt(stdin.next());
            
            costs.computeIfAbsent(a, unused -> new HashMap<>()).put(b, c);
            costs.computeIfAbsent(b, unused -> new HashMap<>()).put(a, c);
        }
        
        int[] d2 = new int[n * 2];
        PriorityQueue<Integer> q2 = new PriorityQueue<>(Comparator.comparing(i -> d2[i]));
        
        Arrays.fill(d2, Integer.MAX_VALUE);
        d2[n * 0 + 0] = 0;
        d2[n * 1 + 0] = 0;
        q2.add(n * 0 + 0);
        q2.add(n * 1 + 0);
        
        while (!q2.isEmpty()) {
            int u = q2.poll();
            int x = u % n;
            boolean skipped = (u / n == 1);
            if (skipped) {
                for (int y : costs.getOrDefault(x, Collections.emptyMap()).keySet()) {
                    int v = n * 1 + y;
                    if (d2[u] + costs.get(x).get(y) < d2[v]) {
                        d2[v] = d2[u] + costs.get(x).get(y);
                        q2.add(v);
                    }
                }
            } else {
                for (int y : costs.getOrDefault(x, Collections.emptyMap()).keySet()) {
                    int v = n * 0 + y;
                    if (d2[u] + costs.get(x).get(y) < d2[v]) {
                        d2[v] = d2[u] + costs.get(x).get(y);
                        q2.add(v);
                    }
                }
                
                for (int y : costs.getOrDefault(x, Collections.emptyMap()).keySet()) {
                    int v = n * 1 + y;
                    if (d2[u] < d2[v]) {
                        d2[v] = d2[u];
                        q2.add(v);
                    }
                }
            }
        }
        
        for (int i = 0; i < n; i++) {
            System.out.println(d2[n * 0 + i] + d2[n * 1 + i]);
        }
    }
}
0