結果

問題 No.845 最長の切符
ユーザー tenten
提出日時 2020-08-31 19:38:00
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 1,859 bytes
コンパイル時間 2,428 ms
コンパイル使用メモリ 80,652 KB
実行使用メモリ 57,308 KB
最終ジャッジ日時 2024-11-17 01:41:50
合計ジャッジ時間 7,818 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16 WA * 11
権限があれば一括ダウンロードができます

ソースコード

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();
    	    if (!graph.get(a).containsKey(b) || graph.get(a).get(b) < c) {
    	        graph.get(a).put(b, c);
    	        graph.get(b).put(a, c);
    	    }
    	}
    	PriorityQueue<Path> queue = new PriorityQueue<>();
    	int[] costs = new int[n];
    	int max = 0;
    	for (int i = 0; i < n; i++) {
    	    queue.add(new Path(i, 0));
    	    getCost(costs, queue, graph);
    	    for (int x : costs) {
    	        max = Math.max(max, x);
    	    }
    	}
    	System.out.println(max);
    }
    
    static void getCost(int[] costs, PriorityQueue<Path> queue, ArrayList<HashMap<Integer, Integer>> graph) {
        Arrays.fill(costs, -1);
        boolean[] used = new boolean[costs.length];
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (used[p.idx]) {
                continue;
            }
            used[p.idx] = true;
            costs[p.idx] = p.value;
            for (int x : graph.get(p.idx).keySet()) {
                queue.add(new Path(x, p.value + graph.get(p.idx).get(x)));
            }
        }
    }
    
    static class Path implements Comparable<Path> {
        int idx;
        int value;
        
        public Path(int idx, int value) {
            this.idx = idx;
            this.value = value;
        }
        
        public int compareTo(Path another) {
            return another.value - value;
        }
    }
}
0