結果

問題 No.848 なかよし旅行
ユーザー htensaihtensai
提出日時 2020-05-08 17:38:51
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,198 bytes
コンパイル時間 2,652 ms
コンパイル使用メモリ 76,220 KB
実行使用メモリ 85,572 KB
最終ジャッジ日時 2023-09-16 08:33:55
合計ジャッジ時間 23,733 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,636 ms
84,260 KB
testcase_01 AC 159 ms
55,488 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 142 ms
55,532 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 147 ms
55,548 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 1,202 ms
75,264 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 282 ms
60,324 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 128 ms
55,584 KB
testcase_25 WA -
testcase_26 AC 125 ms
55,632 KB
testcase_27 AC 125 ms
55,616 KB
testcase_28 AC 123 ms
55,516 KB
testcase_29 AC 128 ms
55,620 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();
		int p = sc.nextInt() - 1;
		int q = sc.nextInt() - 1;
		long t = 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<>();
		long[] fromS = new long[n];
		queue.add(new Path(0, 0));
		search(queue, fromS, graph);
		long[] fromP = new long[n];
		queue.add(new Path(p, 0));
		search(queue, fromP, graph);
		long[] fromQ = new long[n];
		queue.add(new Path(q, 0));
		search(queue, fromQ, graph);
		if (fromS[p] + fromS[q] + fromP[q] <= t) {
		    System.out.println(t);
		    return;
		}
		long max = -1;
		for (int i = 0; i < n; i++) {
		    if ((fromS[i] + Math.max(fromP[i], fromQ[i])) * 2 <= t) {
		        max = Math.max(max, t - Math.max(fromP[i], fromQ[i]) * 2);
		    }
		}
		System.out.println(max);
	}
	
	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;
	        }
	    }
	}
	
	static void search(PriorityQueue<Path> queue, long[] costs, ArrayList<HashMap<Integer, Integer>> graph) {
		Arrays.fill(costs, Long.MAX_VALUE / 10);
		while (queue.size() > 0) {
		    Path p = queue.poll();
		    if (costs[p.idx] <= p.value) {
		        continue;
		    }
		    costs[p.idx] = p.value;
		    for (Map.Entry<Integer, Integer> entry : graph.get(p.idx).entrySet()) {
		        if (costs[entry.getKey()] == Long.MAX_VALUE / 10) {
		            queue.add(new Path(entry.getKey(), p.value + entry.getValue()));
		        }
		    }
		}
	}
}
0