結果

問題 No.848 なかよし旅行
ユーザー tentententen
提出日時 2020-08-31 18:26:03
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,810 ms / 2,000 ms
コード長 2,594 bytes
コンパイル時間 2,267 ms
コンパイル使用メモリ 76,348 KB
実行使用メモリ 94,808 KB
最終ジャッジ日時 2023-08-10 13:54:57
合計ジャッジ時間 26,551 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,680 ms
94,808 KB
testcase_01 AC 147 ms
56,036 KB
testcase_02 AC 121 ms
56,088 KB
testcase_03 AC 139 ms
56,188 KB
testcase_04 AC 138 ms
56,028 KB
testcase_05 AC 131 ms
55,780 KB
testcase_06 AC 195 ms
59,396 KB
testcase_07 AC 153 ms
55,780 KB
testcase_08 AC 214 ms
59,712 KB
testcase_09 AC 252 ms
60,276 KB
testcase_10 AC 216 ms
59,872 KB
testcase_11 AC 915 ms
67,112 KB
testcase_12 AC 1,047 ms
73,012 KB
testcase_13 AC 1,406 ms
80,896 KB
testcase_14 AC 524 ms
62,776 KB
testcase_15 AC 1,260 ms
79,544 KB
testcase_16 AC 1,810 ms
93,996 KB
testcase_17 AC 1,489 ms
79,716 KB
testcase_18 AC 883 ms
69,184 KB
testcase_19 AC 780 ms
67,688 KB
testcase_20 AC 276 ms
60,472 KB
testcase_21 AC 1,640 ms
85,840 KB
testcase_22 AC 1,644 ms
94,512 KB
testcase_23 AC 315 ms
60,420 KB
testcase_24 AC 116 ms
56,480 KB
testcase_25 AC 1,774 ms
91,248 KB
testcase_26 AC 117 ms
56,320 KB
testcase_27 AC 117 ms
55,920 KB
testcase_28 AC 116 ms
56,180 KB
testcase_29 AC 119 ms
56,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static final long MAX = Long.MAX_VALUE / 10;
	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;
    	int 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<>();
    	queue.add(new Path(p, 0));
    	long[] pCosts = getCost(n, queue, graph);
    	queue.add(new Path(q, 0));
    	long[] qCosts = getCost(n, queue, graph);
    	queue.add(new Path(0, 0));
    	long[] zCosts = getCost(n, queue, graph);
    	if (Math.max(pCosts[0], qCosts[0]) * 2 > t) {
    	    System.out.println(-1);
    	    return;
    	}
    	if (pCosts[0] + pCosts[q] + qCosts[0] <= t) {
    	    System.out.println(t);
    	    return;
    	}
    	long max = 0;
    	for (int i = 0; i < n; i++) {
    	    for (int j = 0; j < n; j++) {
    	        if (Math.max(pCosts[i] + pCosts[j], qCosts[i] + qCosts[j]) + zCosts[i] + zCosts[j] <= t) {
    	            max = Math.max(max, t - Math.max(pCosts[i] + pCosts[j], qCosts[i] + qCosts[j]));
    	        }
    	    }
    	}
    	System.out.println(max);
    }
    
    static long[] getCost(int size, PriorityQueue<Path> queue, ArrayList<HashMap<Integer, Integer>> graph) {
        long[] costs = new long[size];
        Arrays.fill(costs, MAX);
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (costs[p.idx] <= p.value) {
                continue;
            }
            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)));
            }
        }
        return costs;
    }
    
    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