結果

問題 No.848 なかよし旅行
ユーザー ks2mks2m
提出日時 2019-07-06 21:16:40
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,621 bytes
コンパイル時間 2,391 ms
コンパイル使用メモリ 79,644 KB
実行使用メモリ 126,496 KB
最終ジャッジ日時 2024-04-25 00:30:55
合計ジャッジ時間 9,037 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		int n = Integer.parseInt(sa[0]);
		int m = Integer.parseInt(sa[1]);
		int p = Integer.parseInt(sa[2]) - 1;
		int q = Integer.parseInt(sa[3]) - 1;
		int t = Integer.parseInt(sa[4]);
		Map<Integer, List<Road>> map = new HashMap<Integer, List<Road>>();
		for (int i = 0; i < m; i++) {
			sa = br.readLine().split(" ");
			int a = Integer.parseInt(sa[0]) - 1;
			int b = Integer.parseInt(sa[1]) - 1;
			int c = Integer.parseInt(sa[2]);

			List<Road> list1 = map.get(a);
			if (list1 == null) {
				list1 = new ArrayList<Road>();
				map.put(a, list1);
			}
			Road r = new Road();
			r.v = b;
			r.c = c;
			list1.add(r);

			List<Road> list2 = map.get(b);
			if (list2 == null) {
				list2 = new ArrayList<Road>();
				map.put(b, list2);
			}
			r = new Road();
			r.v = a;
			r.c = c;
			list2.add(r);
		}
		br.close();

		int[] x = {0, p, q};
		long[][] d = new long[3][n];
		for (int i = 0; i < x.length; i++) {
			Arrays.fill(d[i], Long.MAX_VALUE);
			d[i][x[i]] = 0;
			PriorityQueue<City> que = new PriorityQueue<City>();
			City f = new City();
			f.v = x[i];
			f.d = 0;
			que.add(f);

			while (!que.isEmpty()) {
				City cur = que.poll();
				List<Road> list = map.get(cur.v);
				for (Road next : list) {
					long alt = d[i][cur.v] + next.c;
					if (d[i][next.v] > alt) {
						d[i][next.v] = alt;
						City c = new City();
						c.v = next.v;
						c.d = alt;
						que.add(c);
					}
				}
			}
		}

		if (d[1][0] * 2 > t || d[2][0] * 2 > t) {
			System.out.println(-1);
			return;
		}
		if (d[0][p] + d[1][q] + d[0][q] <= t) {
			System.out.println(t);
			return;
		}

		long min = Long.MAX_VALUE;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				long val = Math.max(d[1][i] + d[1][j], d[2][i] + d[2][j]);
				if (d[0][i] + val + d[0][j] <= t) {
					min = Math.min(min, val);
				}
			}
		}
		System.out.println(t - min);
	}

	static class Road implements Comparable<Road> {
		int v, c;

		public int compareTo(Road o) {
			return c - o.c;
		}
	}

	static class City implements Comparable<City> {
		int v;
		long d;

		public int compareTo(City o) {
			if (d > o.d) {
				return 1;
			} else if (d == o.d) {
				return 0;
			} else {
				return -1;
			}
		}
	}
}
0