結果

問題 No.2565 はじめてのおつかい
ユーザー norikamenorikame
提出日時 2023-12-07 02:07:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 110 ms / 2,000 ms
コード長 1,073 bytes
コンパイル時間 4,570 ms
コンパイル使用メモリ 267,740 KB
実行使用メモリ 14,336 KB
最終ジャッジ日時 2023-12-07 02:08:01
合計ジャッジ時間 9,164 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 110 ms
14,336 KB
testcase_04 AC 76 ms
14,336 KB
testcase_05 AC 1 ms
6,548 KB
testcase_06 AC 56 ms
6,548 KB
testcase_07 AC 29 ms
6,548 KB
testcase_08 AC 29 ms
6,548 KB
testcase_09 AC 50 ms
6,548 KB
testcase_10 AC 35 ms
6,548 KB
testcase_11 AC 84 ms
9,728 KB
testcase_12 AC 20 ms
6,548 KB
testcase_13 AC 34 ms
6,548 KB
testcase_14 AC 58 ms
7,552 KB
testcase_15 AC 61 ms
6,784 KB
testcase_16 AC 66 ms
12,160 KB
testcase_17 AC 14 ms
6,548 KB
testcase_18 AC 20 ms
6,912 KB
testcase_19 AC 74 ms
8,704 KB
testcase_20 AC 65 ms
8,320 KB
testcase_21 AC 63 ms
8,320 KB
testcase_22 AC 35 ms
6,784 KB
testcase_23 AC 42 ms
6,656 KB
testcase_24 AC 8 ms
6,548 KB
testcase_25 AC 63 ms
8,576 KB
testcase_26 AC 40 ms
6,548 KB
testcase_27 AC 60 ms
8,960 KB
testcase_28 AC 70 ms
8,448 KB
testcase_29 AC 85 ms
10,368 KB
testcase_30 AC 68 ms
9,600 KB
testcase_31 AC 66 ms
8,576 KB
testcase_32 AC 35 ms
6,548 KB
testcase_33 AC 63 ms
9,216 KB
testcase_34 AC 65 ms
7,680 KB
testcase_35 AC 70 ms
10,752 KB
testcase_36 AC 67 ms
9,216 KB
testcase_37 AC 40 ms
6,548 KB
testcase_38 AC 62 ms
7,680 KB
testcase_39 AC 62 ms
6,912 KB
testcase_40 AC 77 ms
10,240 KB
testcase_41 AC 82 ms
11,008 KB
testcase_42 AC 39 ms
6,548 KB
testcase_43 AC 55 ms
7,424 KB
testcase_44 AC 29 ms
6,548 KB
testcase_45 AC 16 ms
6,548 KB
testcase_46 AC 66 ms
7,040 KB
testcase_47 AC 43 ms
6,548 KB
testcase_48 AC 35 ms
6,548 KB
testcase_49 AC 20 ms
6,548 KB
testcase_50 AC 53 ms
6,548 KB
testcase_51 AC 2 ms
6,548 KB
testcase_52 AC 44 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// 

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;

using ll = long long;

#define rep(i, n) for (int i=0; i<(int)(n); ++(i))
#define rep3(i, m, n) for (int i=(m); (i)<(int)(n); ++(i))
#define repr(i, n) for (int i=(int)(n)-1; (i)>=0; --(i))
#define rep3r(i, m, n) for (int i=(int)(n)-1; (i)>=(int)(m); --(i))
#define all(x) (x).begin(), (x).end()

const int INF = (int)(1e9);

int main() {
	int n, m;
	cin >> n >> m;
	vector<vector<int>> g(n);
	rep(i, m) {
		int ui, vi;
		cin >> ui >> vi;
		--ui; --vi;
		g[ui].push_back(vi);
	}
	vector<vector<int>> dist(n, vector<int>(4, INF));
	queue<pair<int, int>> que;
	dist[0][0] = 0;
	que.emplace(0, 0);
	while (!que.empty()) {
		auto [vi, si] = que.front(); que.pop();
		int di = dist[vi][si], ndi = di + 1;
		for (const int& ti : g[vi]) {
			int tsi = si | (ti == n-1 ? 1 : ti == n-2 ? 2 : 0);
			if (dist[ti][tsi] <= ndi) continue;
			dist[ti][tsi] = ndi;
			que.emplace(ti, tsi);
		}
	}
	if (dist[0][3] == INF) cout << -1 << endl;
	else cout << dist[0][3] << endl;
	return 0;
}
0