結果

問題 No.807 umg tours
ユーザー tentententen
提出日時 2020-09-01 07:42:21
言語 Java21
(openjdk 21)
結果
AC  
実行時間 3,166 ms / 4,000 ms
コード長 1,880 bytes
コンパイル時間 3,233 ms
コンパイル使用メモリ 76,504 KB
実行使用メモリ 160,104 KB
最終ジャッジ日時 2023-08-11 03:21:14
合計ジャッジ時間 45,952 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 194 ms
56,176 KB
testcase_01 AC 199 ms
56,848 KB
testcase_02 AC 210 ms
58,880 KB
testcase_03 AC 204 ms
58,764 KB
testcase_04 AC 178 ms
56,544 KB
testcase_05 AC 202 ms
59,076 KB
testcase_06 AC 210 ms
58,736 KB
testcase_07 AC 204 ms
59,080 KB
testcase_08 AC 142 ms
54,076 KB
testcase_09 AC 158 ms
56,072 KB
testcase_10 AC 173 ms
56,104 KB
testcase_11 AC 2,834 ms
125,888 KB
testcase_12 AC 2,184 ms
114,760 KB
testcase_13 AC 2,534 ms
128,596 KB
testcase_14 AC 1,777 ms
93,856 KB
testcase_15 AC 1,784 ms
84,992 KB
testcase_16 AC 2,840 ms
131,040 KB
testcase_17 AC 3,161 ms
154,188 KB
testcase_18 AC 3,166 ms
149,308 KB
testcase_19 AC 3,080 ms
145,784 KB
testcase_20 AC 1,762 ms
101,776 KB
testcase_21 AC 1,920 ms
103,188 KB
testcase_22 AC 1,179 ms
81,104 KB
testcase_23 AC 1,058 ms
77,780 KB
testcase_24 AC 2,050 ms
128,312 KB
testcase_25 AC 3,106 ms
160,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
    	Scanner sc = new Scanner(System.in);
    	int n = sc.nextInt();
    	int m = sc.nextInt();
    	ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>();
    	for (int i = 0; i < n; i++) {
    	    graph.add(new HashMap<>());
    	}
    	for (int i = 0; i < m; i++) {
    	    int a = sc.nextInt() - 1;
    	    int b = sc.nextInt() - 1;
    	    int c = sc.nextInt();
    	    graph.get(a).put(b, c);
    	    graph.get(b).put(a, c);
    	}
    	PriorityQueue<Path> queue = new PriorityQueue<>();
    	queue.add(new Path(0, 0, 0));
    	queue.add(new Path(0, 1, 0));
    	long[][] costs = new long[2][n];
    	for (long[] arr : costs) {
    	    Arrays.fill(arr, Long.MAX_VALUE);
    	}
    	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 (int x : graph.get(p.idx).keySet()) {
    	        queue.add(new Path(x, p.type, p.value + graph.get(p.idx).get(x)));
    	        if (p.type == 0) {
    	            queue.add(new Path(x, 1, p.value));
    	        }
    	    }
    	}
    	StringBuilder sb = new StringBuilder();
    	for (int i = 0; i < n; i++) {
    	    sb.append(costs[0][i] + costs[1][i]).append("\n");
    	}
    	System.out.print(sb);
	}
	
	static class Path implements Comparable<Path> {
	    int idx;
	    int type;
	    long value;
	    
	    public Path(int idx, int type, long value) {
	        this.idx = idx;
	        this.type = type;
	        this.value = value;
	    }
	    
	    public int compareTo(Path another) {
	        if (value == another.value) {
	            return 0;
	        } else if (value < another.value) {
	            return -1;
	        } else {
	            return 1;
	        }
	    }
	}
}
0