結果

問題 No.2565 はじめてのおつかい
ユーザー may17may17
提出日時 2023-12-02 15:56:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 1,805 bytes
コンパイル時間 2,068 ms
コンパイル使用メモリ 208,812 KB
実行使用メモリ 12,224 KB
最終ジャッジ日時 2023-12-02 15:56:06
合計ジャッジ時間 5,924 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,744 KB
testcase_01 AC 2 ms
7,744 KB
testcase_02 AC 2 ms
7,744 KB
testcase_03 AC 75 ms
12,224 KB
testcase_04 AC 64 ms
12,224 KB
testcase_05 AC 2 ms
7,744 KB
testcase_06 AC 47 ms
9,280 KB
testcase_07 AC 25 ms
8,512 KB
testcase_08 AC 24 ms
8,640 KB
testcase_09 AC 44 ms
9,024 KB
testcase_10 AC 28 ms
8,768 KB
testcase_11 AC 62 ms
10,432 KB
testcase_12 AC 18 ms
8,256 KB
testcase_13 AC 30 ms
8,640 KB
testcase_14 AC 44 ms
9,536 KB
testcase_15 AC 50 ms
9,408 KB
testcase_16 AC 49 ms
10,560 KB
testcase_17 AC 13 ms
8,128 KB
testcase_18 AC 16 ms
8,640 KB
testcase_19 AC 62 ms
10,048 KB
testcase_20 AC 55 ms
9,792 KB
testcase_21 AC 49 ms
9,920 KB
testcase_22 AC 27 ms
9,024 KB
testcase_23 AC 32 ms
9,152 KB
testcase_24 AC 7 ms
8,000 KB
testcase_25 AC 49 ms
9,920 KB
testcase_26 AC 32 ms
9,024 KB
testcase_27 AC 50 ms
10,048 KB
testcase_28 AC 58 ms
10,048 KB
testcase_29 AC 62 ms
10,560 KB
testcase_30 AC 54 ms
10,304 KB
testcase_31 AC 53 ms
9,920 KB
testcase_32 AC 30 ms
9,024 KB
testcase_33 AC 51 ms
10,048 KB
testcase_34 AC 53 ms
9,792 KB
testcase_35 AC 53 ms
10,432 KB
testcase_36 AC 54 ms
10,048 KB
testcase_37 AC 33 ms
9,024 KB
testcase_38 AC 53 ms
9,792 KB
testcase_39 AC 45 ms
9,408 KB
testcase_40 AC 61 ms
10,560 KB
testcase_41 AC 66 ms
10,688 KB
testcase_42 AC 35 ms
9,024 KB
testcase_43 AC 51 ms
9,536 KB
testcase_44 AC 25 ms
8,640 KB
testcase_45 AC 16 ms
8,256 KB
testcase_46 AC 53 ms
9,536 KB
testcase_47 AC 39 ms
8,896 KB
testcase_48 AC 29 ms
8,640 KB
testcase_49 AC 18 ms
8,256 KB
testcase_50 AC 46 ms
9,152 KB
testcase_51 AC 3 ms
7,744 KB
testcase_52 AC 39 ms
9,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include<atcoder/all>
using namespace std;
//using namespace atcoder;
using ll = long long;
const ll INF = 1000000000000000000LL;
//const ll mod = 998244353;
int n, m, u[100009], v[100009];
vector<int>G[100009];
ll dist[3][100009];
queue<int>Q;
int main() {
	cin >> n >> m;
	for (int i = 1; i <= m; i++) {
		cin >> u[i] >> v[i];
		G[u[i]].push_back(v[i]);
	}
	for (int i = 0; i < 3; i++) {
		for (int j = 1; j <= n; j++)dist[i][j] = -1;
	}
	dist[0][1] = 0;
	Q.push(1);
	while (!Q.empty()) {
		int pos = Q.front(); Q.pop();
		for (int i = 0; i < G[pos].size(); i++) {
			int nex = G[pos][i];
			if (dist[0][nex] == -1) {
				dist[0][nex] = dist[0][pos] + 1;
				Q.push(nex);
			}
		}
	}
	dist[1][n - 1] = 0;
	Q.push(n - 1);
	while (!Q.empty()) {
		int pos = Q.front(); Q.pop();
		for (int i = 0; i < G[pos].size(); i++) {
			int nex = G[pos][i];
			if (dist[1][nex] == -1) {
				dist[1][nex] = dist[1][pos] + 1;
				Q.push(nex);
			}
		}
	}
	dist[2][n] = 0;
	Q.push(n);
	while (!Q.empty()) {
		int pos = Q.front(); Q.pop();
		for (int i = 0; i < G[pos].size(); i++) {
			int nex = G[pos][i];
			if (dist[2][nex] == -1) {
				dist[2][nex] = dist[2][pos] + 1;
				Q.push(nex);
			}
		}
	}
	ll ans1 = INF, ans2 = INF, ans3 = INF;
	if (dist[0][n - 1] != -1 && dist[1][n] != -1 && dist[2][1] != -1) {
		ans1 = dist[0][n - 1] + dist[1][n] + dist[2][1];
	}
	if (dist[0][n] != -1 && dist[2][n-1] != -1 && dist[1][1] != -1) {
		ans2 = dist[0][n] + dist[2][n-1] + dist[1][1];
	}
	if (dist[0][n - 1] != -1 && dist[1][1] != -1 && dist[0][n] != -1&&dist[2][1]!=-1) {
		ans3 = dist[0][n - 1] + dist[1][1] + dist[0][n]+dist[2][1];
	}
	//cout << ans1 << " " << ans2 << " " << ans3 << endl;
	ll ans = min(ans1, min(ans2, ans3));
	if (ans != INF)cout << ans << endl;
	else cout << "-1" << endl;
}
0