結果

問題 No.807 umg tours
ユーザー htensaihtensai
提出日時 2020-02-17 10:15:20
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,558 ms / 4,000 ms
コード長 2,251 bytes
コンパイル時間 2,216 ms
コンパイル使用メモリ 79,904 KB
実行使用メモリ 113,748 KB
最終ジャッジ日時 2024-05-03 00:11:15
合計ジャッジ時間 20,779 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
37,960 KB
testcase_01 AC 68 ms
37,968 KB
testcase_02 AC 84 ms
39,000 KB
testcase_03 AC 83 ms
39,096 KB
testcase_04 AC 61 ms
37,536 KB
testcase_05 AC 78 ms
37,536 KB
testcase_06 AC 93 ms
39,004 KB
testcase_07 AC 79 ms
38,416 KB
testcase_08 AC 48 ms
37,000 KB
testcase_09 AC 51 ms
37,148 KB
testcase_10 AC 52 ms
37,040 KB
testcase_11 AC 1,232 ms
87,496 KB
testcase_12 AC 932 ms
79,684 KB
testcase_13 AC 1,279 ms
90,836 KB
testcase_14 AC 714 ms
66,644 KB
testcase_15 AC 589 ms
60,328 KB
testcase_16 AC 1,372 ms
96,132 KB
testcase_17 AC 1,528 ms
113,196 KB
testcase_18 AC 1,525 ms
111,820 KB
testcase_19 AC 1,496 ms
113,748 KB
testcase_20 AC 869 ms
74,916 KB
testcase_21 AC 942 ms
76,476 KB
testcase_22 AC 530 ms
56,404 KB
testcase_23 AC 424 ms
53,260 KB
testcase_24 AC 915 ms
89,476 KB
testcase_25 AC 1,558 ms
112,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
	public static void main (String[] args) throws Exception {
	    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	    String[] first = br.readLine().split(" ", 2);
		int n = Integer.parseInt(first[0]);
		int m = Integer.parseInt(first[1]);
		ArrayList<Path>[] graph = new ArrayList[n];
		for (int i = 0; i < n; i++) {
		    graph[i] = new ArrayList<>();
		}
		for (int i = 0; i < m; i++) {
		    String[] line = br.readLine().split(" ", 3);
		    int a = Integer.parseInt(line[0]) - 1;
		    int b = Integer.parseInt(line[1]) - 1;
		    int c = Integer.parseInt(line[2]);
		    graph[a].add(new Path(b, c));
		    graph[b].add(new Path(a, c));
		}
		long[] visited = new long[n];
		long[] tickets = new long[n];
		Arrays.fill(visited, Long.MAX_VALUE);
		Arrays.fill(tickets, Long.MAX_VALUE);
		PriorityQueue<Path> queue = new PriorityQueue<>();
		PriorityQueue<Path> next = new PriorityQueue<>();
		tickets[0] = 0;
		queue.add(new Path(0, 0));
		while (queue.size() > 0) {
		    Path p = queue.poll();
		    if (visited[p.idx] <= p.value) {
		        continue;
		    }
		    visited[p.idx] = p.value;
		    for (Path q : graph[p.idx]) {
		        queue.add(new Path(q.idx, p.value + q.value));
		        next.add(new Path(q.idx, p.value));
		    }
		}
		while (next.size() > 0) {
		    Path p = next.poll();
		    if (tickets[p.idx] <= p.value) {
		        continue;
		    }
		    tickets[p.idx] = p.value;
		    for (Path q : graph[p.idx]) {
		        next.add(new Path(q.idx, p.value + q.value));
		    }
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < n; i++) {
		    sb.append(visited[i] + tickets[i]).append("\n");
		}
		System.out.print(sb);
    }
    
    static class Path implements Comparable<Path> {
        int idx;
        long value;
        
        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;
            }
        }
    }
}
0