結果

問題 No.2565 はじめてのおつかい
ユーザー 沙耶花
提出日時 2023-12-06 00:45:41
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 109 ms / 2,000 ms
コード長 820 bytes
コンパイル時間 4,854 ms
コンパイル使用メモリ 255,432 KB
最終ジャッジ日時 2025-02-18 08:03:56
ジャッジサーバーID
(参考情報)
judge2 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 4000000000000000001

int main(){
	
	int n,m;
	cin>>n>>m;
	vector<vector<int>> E(n);
	rep(i,m){
		int a,b;
		cin>>a>>b;
		a--,b--;
		E[a].push_back(b);
	}
	vector d(n,vector<int>(4,Inf32));
	d[0][0] = 0;
	queue<pair<int,int>> Q;
	Q.emplace(0,0);
	while(Q.size()>0){
		int x = Q.front().first,y = Q.front().second;
		Q.pop();
		rep(i,E[x].size()){
			int tx = E[x][i];
			int ty = y;
			if(tx==n-2)ty |= 1;
			if(tx==n-1)ty |= 2;
			if(d[tx][ty]==Inf32){
				d[tx][ty] = d[x][y]+1;
				Q.emplace(tx,ty);
			}
		}
	}
	int ans = d[0][3];
	if(ans==Inf32)ans =-1;
	cout<<ans<<endl;
	
	return 0;
}
0