結果

問題 No.2565 はじめてのおつかい
コンテスト
ユーザー Solalyth
提出日時 2026-06-19 13:11:22
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 872 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,335 ms
コンパイル使用メモリ 216,632 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2026-06-19 13:11:29
合計ジャッジ時間 6,058 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25 WA * 25
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

#define LL long long
#define rep(i, a, b) for(LL i=(a); i<(b); i++)
template <typename T> bool chmin(T& a, const T& b) { return a>b ? a=b,1 : 0; }

int main() {
	int N, M; cin >> N >> M;
	vector<int> E[1<<17];
	rep(_, 0, M) {
		int u, v; cin >> u >> v; u--; v--;
		E[u].push_back(v);
	}
	
	const int inf = 1<<27;
    
    auto bfs = [&](int s) {
    	deque<int> que = {{0, 0}};
    	vector<int> d(N, inf); d[0] = 0;
    	
    	while (!que.empty()) {
    		int i = que.front(); que.pop_front();
    		for(auto j: E[i]) {
    			if (chmin(d[j], d[i]+1)) { que.push_back(j); }
    		}
    	}
    	return d;
    };
    
    auto d0 = bfs(0), d1 = bfs(N-2), d2 = bfs(N-1);
    int ans = inf;
    chmin(ans, d0[N-2] + d1[N-1] + d2[0]);
    chmin(ans, d0[N-1] + d2[N-2] + d1[0]);
    if (ans == inf) { ans = -1; }
    cout << ans;
}
0