結果

問題 No.807 umg tours
ユーザー neko_the_shadowneko_the_shadow
提出日時 2019-05-17 12:55:01
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,246 bytes
コンパイル時間 2,800 ms
コンパイル使用メモリ 83,680 KB
実行使用メモリ 143,844 KB
最終ジャッジ日時 2023-08-15 12:25:22
合計ジャッジ時間 32,082 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
54,108 KB
testcase_01 AC 89 ms
52,112 KB
testcase_02 AC 97 ms
52,316 KB
testcase_03 AC 97 ms
52,108 KB
testcase_04 AC 89 ms
54,020 KB
testcase_05 AC 89 ms
51,976 KB
testcase_06 AC 98 ms
51,948 KB
testcase_07 AC 94 ms
51,952 KB
testcase_08 AC 73 ms
51,508 KB
testcase_09 AC 76 ms
51,412 KB
testcase_10 AC 78 ms
51,584 KB
testcase_11 AC 1,978 ms
109,760 KB
testcase_12 AC 1,610 ms
109,288 KB
testcase_13 AC 1,781 ms
126,872 KB
testcase_14 AC 1,110 ms
91,760 KB
testcase_15 AC 927 ms
83,320 KB
testcase_16 AC 2,105 ms
129,668 KB
testcase_17 AC 2,195 ms
143,756 KB
testcase_18 AC 2,241 ms
143,400 KB
testcase_19 AC 1,946 ms
143,844 KB
testcase_20 AC 1,292 ms
105,596 KB
testcase_21 AC 1,387 ms
106,196 KB
testcase_22 AC 764 ms
81,460 KB
testcase_23 AC 647 ms
76,752 KB
testcase_24 TLE -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

package _0807;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
        String[] first = stdin.readLine().split(" ");
        int n = Integer.parseInt(first[0]);
        int m = Integer.parseInt(first[1]);
        
        Map<Integer, Map<Integer, Long>> costs = new HashMap<>();
        for (int i = 0; i < m; i++) {
            String[] line = stdin.readLine().split(" ");
            int a = Integer.parseInt(line[0]) - 1;
            int b = Integer.parseInt(line[1]) - 1;
            long c = Long.parseLong(line[2]);
            
            costs.computeIfAbsent(a, unused -> new HashMap<>()).put(b, c);
            costs.computeIfAbsent(b, unused -> new HashMap<>()).put(a, c);
        }

        
        long[] d1 = new long[n];
        long[] d2 = new long[n];
        PriorityQueue<Tuple> q = new PriorityQueue<>();
        Arrays.fill(d1, Long.MAX_VALUE);
        Arrays.fill(d2, Long.MAX_VALUE);
        d1[0] = 0;
        d2[0] = 0;
        q.add(new Tuple(0, 0, false));
        q.add(new Tuple(0, 0, true));
        
        while (!q.isEmpty()) {
            Tuple t = q.poll();
            int u = t.ptr;
            if (!costs.containsKey(u)) {
                continue;
            }
            
            if (t.skipped) {
                for (Entry<Integer, Long> e : costs.get(u).entrySet()) {
                    int v = e.getKey();
                    long d = e.getValue();
                    if (d2[u] + d < d2[v]) {
                        d2[v] = d2[u] + d;
                        q.add(new Tuple(d2[v], v, true));
                    }
                }
            } else {
                for (Entry<Integer, Long> e : costs.get(u).entrySet()) {
                    int v = e.getKey();
                    long d = e.getValue();
                    
                    if (d1[u] + d < d1[v]) {
                        d1[v] = d1[u] + d;
                        q.add(new Tuple(d1[v], v, false));
                    }
                    
                    if (d1[u] < d2[v]) {
                        d2[v] = d1[u];
                        q.add(new Tuple(d2[v], v, true));
                    }
                }
            }
        }
        
        PrintWriter stdout = new PrintWriter(System.out);
        IntStream.range(0, n).mapToLong(i -> d1[i] + d2[i]).forEach(stdout::println);
        stdout.flush();
    }
    
    private static class Tuple implements Comparable<Tuple>{
        private long d;
        private int ptr;
        private boolean skipped;
        
        
        public Tuple(long d, int ptr, boolean skipped) {
            super();
            this.d = d;
            this.ptr = ptr;
            this.skipped = skipped;
        }

        @Override
        public int compareTo(Tuple other) {
            return Long.compare(this.d, other.d);
        }
    }
}
0