結果

問題 No.1565 Union
ユーザー kotatsugamekotatsugame
提出日時 2021-06-29 20:11:28
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 225 ms / 2,000 ms
コード長 504 bytes
コンパイル時間 751 ms
コンパイル使用メモリ 77,148 KB
実行使用メモリ 16,640 KB
最終ジャッジ日時 2024-06-25 22:36:15
合計ジャッジ時間 6,208 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:8:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
    8 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int N,M;
vector<int>G[2<<17];
int dist[2<<17];
main()
{
	cin>>N>>M;
	for(int i=0;i<M;i++)
	{
		int a,b;cin>>a>>b;
		a--,b--;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	for(int i=1;i<N;i++)dist[i]=M+1;
	queue<int>P;
	P.push(0);
	while(!P.empty())
	{
		int u=P.front();P.pop();
		for(int v:G[u])if(dist[v]>dist[u]+1)
		{
			dist[v]=dist[u]+1;
			P.push(v);
		}
	}
	if(dist[N-1]==M+1)cout<<-1<<endl;
	else cout<<dist[N-1]<<endl;
}
0