結果

問題 No.1473 おでぶなおばけさん
ユーザー 小野寺健
提出日時 2021-04-26 08:18:55
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 1,346 bytes
コンパイル時間 3,340 ms
コンパイル使用メモリ 78,968 KB
実行使用メモリ 86,020 KB
最終ジャッジ日時 2024-07-04 16:28:00
合計ジャッジ時間 8,861 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20 TLE * 4 -- * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;

public class No1473_2 {
	
	private static int n;
	private static List<List<Integer[]>> V;

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		n = scan.nextInt();
		int m = scan.nextInt();
		V = new ArrayList<List<Integer[]>>();
		for (int i = 0; i < n; i++) {
			V.add(new ArrayList<Integer[]>());
		}
		int max_d = 0;
		for (int i=0; i < m; i++) {
			int s = scan.nextInt() - 1;
			int t = scan.nextInt() - 1;
			int d = scan.nextInt();
			V.get(s).add(new Integer[] {t, d});
			V.get(t).add(new Integer[] {s, d});
			max_d = Math.max(max_d,  d);
		}
		
		int l = 0, r = max_d + 1;
		while (r - l > 1) {
			int mid = (r + l) / 2;
			int c = bfs(mid);
			if (c == 0) {
				r = mid;
			} else {
				l = mid;
			}
		}
		System.out.println(String.format("%d %d", l, bfs(l)));
	}
	
	private static int bfs(int w) {
		boolean[] B = new boolean[n];
		List<Integer[]> q = new ArrayList<Integer[]>();
		q.add(new Integer[] {0, 0});
		while (q.size() > 0) {
			Integer[] v = q.remove(0);
			if (v[0] == n-1) {
				return v[1];
			}
			if (B[v[0]]) {
				continue;
			}
			B[v[0]] = true;
			for (Integer[] i : V.get(v[0])) {
				if (i[1] >= w) {
					q.add(new Integer[] {i[0], v[1] + 1});
				}
			}
		}
		return 0;
	}

}
0