답안 #101178

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
101178 2019-03-17T08:19:27 Z square1001 007 (CEOI14_007) C++14
100 / 100
419 ms 17144 KB
#include <queue>
#include <vector>
#include <iostream>
using namespace std;
const int inf = 1012345678;
vector<int> shortest_path(int src, vector<vector<int> > &G) {
	int N = G.size();
	vector<int> dist(N, inf);
	queue<int> que;
	que.push(src);
	dist[src] = 0;
	while (!que.empty()) {
		int u = que.front(); que.pop();
		for (int i : G[u]) {
			if (dist[i] == inf) {
				dist[i] = dist[u] + 1;
				que.push(i);
			}
		}
	}
	return dist;
}
int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	int N, M;
	cin >> N >> M;
	int S, D, A, B;
	cin >> S >> D >> A >> B; --S, --D, --A, --B;
	vector<vector<int> > G(N);
	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);
	}
	vector<int> da = shortest_path(A, G);
	vector<int> db = shortest_path(B, G);
	if (da[S] != db[S] || da[D] != db[D]) {
		int ans = min(da[D] - da[S], db[D] - db[S]);
		cout << (ans < 0 ? -1 : ans) << endl;
	}
	else {
		vector<int> ds = shortest_path(S, G);
		vector<int> dd = shortest_path(D, G);
		int mins = inf, mind = inf;
		for (int i = 0; i < N; ++i) {
			if (da[i] == db[i]) {
				if (da[i] + ds[i] == da[S]) mins = min(mins, da[i]);
				if (da[i] + dd[i] == da[D]) mind = min(mind, da[i]);
			}
		}
		int ans = da[D] - da[S] - (mins > mind ? 1 : 0);
		cout << (ans < 0 ? -1 : ans) << endl;
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 356 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 3 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 3 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 3 ms 384 KB Output is correct
22 Correct 3 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 3 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 3276 KB Output is correct
2 Correct 34 ms 4472 KB Output is correct
3 Correct 27 ms 3320 KB Output is correct
4 Correct 31 ms 4736 KB Output is correct
5 Correct 36 ms 2816 KB Output is correct
6 Correct 27 ms 3200 KB Output is correct
7 Correct 26 ms 3968 KB Output is correct
8 Correct 30 ms 3832 KB Output is correct
9 Correct 42 ms 4216 KB Output is correct
10 Correct 186 ms 8892 KB Output is correct
11 Correct 63 ms 6748 KB Output is correct
12 Correct 89 ms 8284 KB Output is correct
13 Correct 83 ms 7288 KB Output is correct
14 Correct 66 ms 6212 KB Output is correct
15 Correct 97 ms 8568 KB Output is correct
16 Correct 132 ms 7952 KB Output is correct
17 Correct 92 ms 8056 KB Output is correct
18 Correct 118 ms 8004 KB Output is correct
19 Correct 137 ms 9252 KB Output is correct
20 Correct 268 ms 12352 KB Output is correct
21 Correct 151 ms 11652 KB Output is correct
22 Correct 125 ms 10156 KB Output is correct
23 Correct 125 ms 10336 KB Output is correct
24 Correct 129 ms 11384 KB Output is correct
25 Correct 125 ms 10872 KB Output is correct
26 Correct 107 ms 9208 KB Output is correct
27 Correct 140 ms 11640 KB Output is correct
28 Correct 170 ms 11640 KB Output is correct
29 Correct 229 ms 11640 KB Output is correct
30 Correct 298 ms 13432 KB Output is correct
31 Correct 186 ms 13432 KB Output is correct
32 Correct 161 ms 11512 KB Output is correct
33 Correct 127 ms 10772 KB Output is correct
34 Correct 211 ms 12412 KB Output is correct
35 Correct 150 ms 12076 KB Output is correct
36 Correct 176 ms 12472 KB Output is correct
37 Correct 161 ms 12508 KB Output is correct
38 Correct 209 ms 13808 KB Output is correct
39 Correct 251 ms 13812 KB Output is correct
40 Correct 301 ms 15096 KB Output is correct
41 Correct 419 ms 17144 KB Output is correct