제출 #82345

#제출 시각아이디문제언어결과실행 시간메모리
82345combi1k1007 (CEOI14_007)C++14
0 / 100
172 ms22580 KiB
#pragma GCC optimize "-O3"
#include<bits/stdc++.h>

using namespace std;

#define pb  push_back

const int   N   = 1e5 + 1;

vector<int> g[N];

int n, m;
int X, S, A, B;
int d[N];

int  bfs(int s) {
	queue<int>  q;
	for(int i = 1 ; i <= n ; ++i)
		d[i] = N;
	d[s] = 0;   q.push(s);
	
	while(q.size()) {
		int u = q.front();  q.pop();
		for(int v : g[u])
			if (d[v] > d[u] + 1)    {
				d[v] = d[u] + 1;
				q.push(v);
			}
	}
	
	if (d[X] > d[S])    {
		puts("-1");
		exit(0);
	}
	
	return d[S] - d[X];
}

int main()  {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	
	//freopen("SPY.inp","r",stdin);
	//freopen("SPY.out","w",stdout);
	
	cin >> n >> m >> X >> S >> A >> B;
	
	while(m--)  {
		int x, y;
		cin >> x >> y;
		g[x].pb(y);
		g[y].pb(x);
	}
	
	cout << min(bfs(A),bfs(B));
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...