結果

問題 No.1473 おでぶなおばけさん
ユーザー 小野寺健
提出日時 2021-04-26 09:49:58
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 2,143 bytes
コンパイル時間 3,525 ms
コンパイル使用メモリ 79,124 KB
実行使用メモリ 108,448 KB
最終ジャッジ日時 2024-07-04 18:25:36
合計ジャッジ時間 17,382 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 5 TLE * 2 -- * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

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

public class No1473_2 {
	
	private static class Q {
		int i;
		int d;
		public Q(int i, int d) {
			this.i = i;
			this.d = d;
		}
	}
	
	private static int n;
	private static List<List<Q>> V;

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		n = scan.nextInt();
		int m = scan.nextInt();
		V = new ArrayList<List<Q>>();
		for (int i = 0; i < n; i++) {
			V.add(new ArrayList<Q>());
		}
		int max_d = 0;
		int min_d = Integer.MAX_VALUE;
		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 Q(t, d));
			V.get(t).add(new Q(s, d));
			max_d = Math.max(max_d,  d);
			min_d = Math.min(min_d,  d);
		}
		scan.close();
		Q l = new Q(min_d - 1, 0), r = new Q(max_d + 1, 0);
		while (r.i - l.i > 1) {
			int mid = (r.i + l.i) / 2;
			int c = bfs(mid);
			if (c == 0) {
				r.i = mid;
				r.d = 0;
			} else {
				l.i = mid;
				l.d = c;
			}
		}
		System.out.println(String.format("%d %d", l.i,l.d));
	}
	
	private static int bfs(int w) {
		boolean[][] B = new boolean[n][2];
		Set<Integer> fw = new HashSet<Integer>();
		Set<Integer> bw = new HashSet<Integer>();
		fw.add(0);
		bw.add(n-1);
		int depth = 1;
		while (true) {
			Set<Integer> newfw = new HashSet<Integer>();
			for (int i : fw) {
				B[i][0] = true;
				for (Q n : V.get(i)) {
					if (n.d >= w && !B[n.i][0]) {
						newfw.add(n.i);
					}
				}
			}
			if (newfw.isEmpty()) {
				depth = 0;
				break;
			}
			fw = new HashSet<Integer>(newfw);
			newfw.retainAll(bw);
			if (!newfw.isEmpty()) {
				break; 
			}
			depth++;
			Set<Integer> newbw = new HashSet<Integer>();
			for (int i : bw) {
				B[i][0] = true;
				for (Q n : V.get(i)) {
					if (n.d >= w && !B[n.i][0]) {
						newbw.add(n.i);
					}
				}
			}
			if (newbw.isEmpty()) {
				depth = 0;
				break;
			}
			bw = new HashSet<Integer>(newbw);
			newbw.retainAll(fw);
			if (!newbw.isEmpty()) {
				break; 
			}
			depth++;
		}
		return depth;
	}

}
0