제출 #9178

#제출 시각아이디문제언어결과실행 시간메모리
9178lemonsqueezeYour life (kriii2_Y)C++98
4 / 4
88 ms10208 KiB
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;

const int N = 200000;

int n, m, d[N];
bool vst[N];
vector<int> graph[N];
queue<int> q;

int main(void) {
	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; i++)
		d[i] = 2*N;
	for (int i = 0; i < m; i++) {
		int u, v;
		scanf("%d %d", &u, &v);
		u--, v--;
		graph[u].push_back(v);
	}
	d[0] = 0;
	q.push(0);
	while (!q.empty()) {
		int u = q.front();
		q.pop();
		if (vst[u]) continue;
		vst[u] = true;
		for (int ed = 0; ed < (int)graph[u].size(); ed++) {
			int v = graph[u][ed];
			if (vst[v]) continue;
			d[v] = min(d[v], d[u]+1);
			q.push(v);
		}
	}
	if (vst[n-1]) printf("%d\n", d[n-1]);
	else printf("-1\n");
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...