結果

問題 No.2565 はじめてのおつかい
ユーザー achapiachapi
提出日時 2023-12-02 15:38:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 80 ms / 2,000 ms
コード長 737 bytes
コンパイル時間 2,385 ms
コンパイル使用メモリ 213,292 KB
実行使用メモリ 11,956 KB
最終ジャッジ日時 2023-12-02 15:39:02
合計ジャッジ時間 6,581 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 80 ms
11,956 KB
testcase_04 AC 69 ms
11,956 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 53 ms
6,676 KB
testcase_07 AC 28 ms
6,676 KB
testcase_08 AC 26 ms
6,676 KB
testcase_09 AC 48 ms
6,676 KB
testcase_10 AC 32 ms
6,676 KB
testcase_11 AC 70 ms
8,492 KB
testcase_12 AC 20 ms
6,676 KB
testcase_13 AC 32 ms
6,676 KB
testcase_14 AC 51 ms
6,800 KB
testcase_15 AC 57 ms
6,676 KB
testcase_16 AC 56 ms
10,048 KB
testcase_17 AC 13 ms
6,676 KB
testcase_18 AC 16 ms
6,676 KB
testcase_19 AC 64 ms
7,780 KB
testcase_20 AC 58 ms
7,360 KB
testcase_21 AC 56 ms
7,388 KB
testcase_22 AC 32 ms
6,676 KB
testcase_23 AC 36 ms
6,676 KB
testcase_24 AC 7 ms
6,676 KB
testcase_25 AC 57 ms
7,556 KB
testcase_26 AC 37 ms
6,676 KB
testcase_27 AC 56 ms
7,856 KB
testcase_28 AC 64 ms
7,556 KB
testcase_29 AC 71 ms
8,952 KB
testcase_30 AC 60 ms
8,352 KB
testcase_31 AC 60 ms
7,628 KB
testcase_32 AC 33 ms
6,676 KB
testcase_33 AC 58 ms
8,060 KB
testcase_34 AC 64 ms
7,048 KB
testcase_35 AC 64 ms
9,108 KB
testcase_36 AC 60 ms
8,060 KB
testcase_37 AC 35 ms
6,676 KB
testcase_38 AC 60 ms
7,060 KB
testcase_39 AC 49 ms
6,676 KB
testcase_40 AC 69 ms
8,892 KB
testcase_41 AC 70 ms
9,348 KB
testcase_42 AC 37 ms
6,676 KB
testcase_43 AC 54 ms
6,824 KB
testcase_44 AC 28 ms
6,676 KB
testcase_45 AC 15 ms
6,676 KB
testcase_46 AC 59 ms
6,676 KB
testcase_47 AC 42 ms
6,676 KB
testcase_48 AC 33 ms
6,676 KB
testcase_49 AC 19 ms
6,676 KB
testcase_50 AC 51 ms
6,676 KB
testcase_51 AC 2 ms
6,676 KB
testcase_52 AC 43 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
	int N, M;
	cin >> N >> M;
	vector<vector<long long>> G(N);
	for (int i = 0; i < M; i++){
		int u, v;
		cin >> u >> v;
		u--;
		v--;
		G[u].push_back(v);
	}
	long long ans = 1e18;
	vector<vector<long long>> D;
	for (int i : {0, N - 2, N - 1}){
		queue<int> q;
		vector<long long> d(N, 1e18);
		q.push(i);
		d[i] = 0;
		while (!q.empty()){
			int v = q.front();
			q.pop();
			for (int i : G[v]){
				if (d[i] == 1e18){
					d[i] = d[v] + 1;
					q.push(i);
				}
			}
		}
		D.push_back(d);
	}
	ans = min(ans, D[0][N - 2] + D[1][N - 1] + D[2][0]);
	ans = min(ans, D[0][N - 1] + D[2][N - 2] + D[1][0]);
	if (ans != 1e18){
		cout << ans << endl;
	} else {
		cout << -1 << endl;
	}
}
0