結果
問題 | No.807 umg tours |
ユーザー |
![]() |
提出日時 | 2020-02-17 10:15:20 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 1,632 ms / 4,000 ms |
コード長 | 2,251 bytes |
コンパイル時間 | 3,542 ms |
コンパイル使用メモリ | 79,440 KB |
実行使用メモリ | 114,076 KB |
最終ジャッジ日時 | 2024-11-23 19:50:27 |
合計ジャッジ時間 | 21,744 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
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; } } } }