結果

問題 No.2565 はじめてのおつかい
ユーザー k82bk82b
提出日時 2023-12-02 15:58:39
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 1,318 bytes
コンパイル時間 3,686 ms
コンパイル使用メモリ 163,196 KB
実行使用メモリ 13,384 KB
最終ジャッジ日時 2023-12-02 15:58:51
合計ジャッジ時間 9,168 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,548 KB
testcase_01 AC 1 ms
6,548 KB
testcase_02 AC 1 ms
6,548 KB
testcase_03 AC 112 ms
13,384 KB
testcase_04 WA -
testcase_05 AC 1 ms
6,548 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 34 ms
7,280 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 80 ms
13,256 KB
testcase_17 WA -
testcase_18 AC 23 ms
6,960 KB
testcase_19 AC 95 ms
13,256 KB
testcase_20 AC 86 ms
13,256 KB
testcase_21 WA -
testcase_22 AC 48 ms
11,592 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 89 ms
13,256 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 97 ms
13,256 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 AC 1 ms
6,548 KB
testcase_52 AC 73 ms
12,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std, core.bitop;
string[] _R;
void readM(T)(ref T x) { while (_R.empty) { _R = readln.chomp.split; } x = _R.front.to!T; _R.popFront; }
bool chmin(T)(ref T A, T B) { if (A > B) { A = B; return true; } else { return false; } }
bool chmax(T)(ref T A, T B) { if (A < B) { A = B; return true; } else { return false; } }
int lowerBound(T)(T[] A, T x) { int lo = -1, hi = cast(int)(A.length); while (hi - lo > 1) { int mid = lo + hi >> 1; (A[mid] < x ? lo : hi) = mid; } return hi; }
int upperBound(T)(T[] A, T x) { int lo = -1, hi = cast(int)(A.length); while (hi - lo > 1) { int mid = lo + hi >> 1; (A[mid] > x ? hi : lo) = mid; } return hi; }

enum INF = 10 ^^ 8;

int N, M;
int[][] G;

int[] BFS(int s) {
	auto dist = new int[N];
	dist[] = INF;
	dist[s] = 0;
	int[] que;
	que ~= s;
	while (!que.empty) {
		int u = que.front;
		que.popFront;
		if (!u) {
			continue;
		}
		foreach (v; G[u]) {
			if (chmin(dist[v], dist[u] + 1)) {
				que ~= v;
			}
		}
	}
	return dist;
}

void main() {
	N.readM;
	M.readM;
	G = new int[][](N, 0);
	foreach (i; 0 .. M) {
		int u, v;
		u.readM;
		v.readM;
		--u, --v;
		G[u] ~= v;
		G[v] ~= u;
	}
	auto A = BFS(N - 2);
	auto B = BFS(N - 1);
	int ans = INF;
	chmin(ans, A[0] + A[N - 1] + B[0]);
	chmin(ans, B[0] + B[N - 2] + A[0]);
	if (ans == INF) {
		ans = -1;
	}
	writeln(ans);
}
0