답안 #101143

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
101143 2019-03-17T05:22:59 Z E869120 007 (CEOI14_007) C++14
100 / 100
648 ms 62580 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
#pragma warning (disable: 4996)

long long N, M, p, q, a, b, dist1[1 << 18], dist2[1 << 18], dist3[1 << 18], dist4[1 << 18], dist[1 << 18]; bool used[1 << 18];
vector<int>X[1 << 18]; vector<pair<int, long long>>G[1 << 18];

vector<long long> dijkstra(int pos) {
	queue<int>Q;
	for (int i = 1; i <= N; i++) dist[i] = (1 << 30);
	dist[pos] = 0; Q.push(pos);

	while (!Q.empty()) {
		int pos = Q.front(); Q.pop();
		for (int i = 0; i < X[pos].size(); i++) {
			if (dist[X[pos][i]] > dist[pos] + 1) {
				dist[X[pos][i]] = dist[pos] + 1;
				Q.push(X[pos][i]);
			}
		}
	}

	vector<long long>vec;
	for (int i = 1; i <= N; i++) vec.push_back(dist[i]);
	return vec;
}

vector<long long> dijkstra2(int pos) {
	priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>>Q;
	for (int i = 1; i <= N; i++) dist[i] = (1LL << 60);
	Q.push(make_pair(0, pos)); dist[pos] = 0;

	while (!Q.empty()) {
		int pos = Q.top().second; Q.pop();
		for (int i = 0; i < G[pos].size(); i++) {
			int to = G[pos][i].first; long long cost = G[pos][i].second;
			if (dist[to] > dist[pos] + cost) {
				dist[to] = dist[pos] + cost;
				Q.push(make_pair(dist[to], to));
			}
		}
	}

	vector<long long> I;
	for (int i = 1; i <= N; i++) I.push_back(dist[i]);
	return I;
}

int main() {
	scanf("%d%d", &N, &M);
	scanf("%d%d%d%d", &p, &q, &a, &b);
	for (int i = 1; i <= M; i++) {
		int v1, v2; scanf("%d%d", &v1, &v2);
		X[v1].push_back(v2);
		X[v2].push_back(v1);
	}
	vector<long long> V1 = dijkstra(a); for (int i = 0; i < V1.size(); i++) dist1[i + 1] = V1[i];
	vector<long long> V2 = dijkstra(b); for (int i = 0; i < V2.size(); i++) dist2[i + 1] = V2[i];
	vector<long long> V3 = dijkstra(q); for (int i = 0; i < V3.size(); i++) dist3[i + 1] = V3[i];
	for (int i = 1; i <= N; i++) {
		for (int j = 0; j < X[i].size(); j++) {
			long long cost = 1000001;
			if (dist1[X[i][j]] == dist2[X[i][j]]) cost = 1000000;
			G[i].push_back(make_pair(X[i][j], cost));
		}
	}
	vector<long long> V4 = dijkstra2(a); for (int i = 0; i < V1.size(); i++) dist4[i + 1] = V4[i];

	long long minx = (1 << 30);
	for (int i = 1; i <= N; i++) {
		if (dist1[i] < dist1[p] || dist2[i] < dist2[p] || (dist1[i] == dist1[p] && dist1[i] == dist2[i] && dist2[i] == dist2[p] && dist4[i] < dist4[p])) {
			minx = min(minx, dist3[i]);
		}
	}
	cout << minx - 1 << endl;
	return 0;
}

Compilation message

007.cpp:6:0: warning: ignoring #pragma warning  [-Wunknown-pragmas]
 #pragma warning (disable: 4996)
 
007.cpp: In function 'std::vector<long long int> dijkstra(int)':
007.cpp:18:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < X[pos].size(); i++) {
                   ~~^~~~~~~~~~~~~~~
007.cpp: In function 'std::vector<long long int> dijkstra2(int)':
007.cpp:38:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < G[pos].size(); i++) {
                   ~~^~~~~~~~~~~~~~~
007.cpp: In function 'int main()':
007.cpp:53:22: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'long long int*' [-Wformat=]
  scanf("%d%d", &N, &M);
                ~~    ^
007.cpp:53:22: warning: format '%d' expects argument of type 'int*', but argument 3 has type 'long long int*' [-Wformat=]
007.cpp:54:34: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'long long int*' [-Wformat=]
  scanf("%d%d%d%d", &p, &q, &a, &b);
                    ~~            ^
007.cpp:54:34: warning: format '%d' expects argument of type 'int*', but argument 3 has type 'long long int*' [-Wformat=]
007.cpp:54:34: warning: format '%d' expects argument of type 'int*', but argument 4 has type 'long long int*' [-Wformat=]
007.cpp:54:34: warning: format '%d' expects argument of type 'int*', but argument 5 has type 'long long int*' [-Wformat=]
007.cpp:60:56: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  vector<long long> V1 = dijkstra(a); for (int i = 0; i < V1.size(); i++) dist1[i + 1] = V1[i];
                                                      ~~^~~~~~~~~~~
007.cpp:61:56: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  vector<long long> V2 = dijkstra(b); for (int i = 0; i < V2.size(); i++) dist2[i + 1] = V2[i];
                                                      ~~^~~~~~~~~~~
007.cpp:62:56: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  vector<long long> V3 = dijkstra(q); for (int i = 0; i < V3.size(); i++) dist3[i + 1] = V3[i];
                                                      ~~^~~~~~~~~~~
007.cpp:64:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 0; j < X[i].size(); j++) {
                   ~~^~~~~~~~~~~~~
007.cpp:70:57: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  vector<long long> V4 = dijkstra2(a); for (int i = 0; i < V1.size(); i++) dist4[i + 1] = V4[i];
                                                       ~~^~~~~~~~~~~
007.cpp:53:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &M);
  ~~~~~^~~~~~~~~~~~~~~~
007.cpp:54:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d%d", &p, &q, &a, &b);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
007.cpp:56:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int v1, v2; scanf("%d%d", &v1, &v2);
               ~~~~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 12672 KB Output is correct
2 Correct 12 ms 12672 KB Output is correct
3 Correct 14 ms 12800 KB Output is correct
4 Correct 12 ms 12672 KB Output is correct
5 Correct 14 ms 12644 KB Output is correct
6 Correct 15 ms 12672 KB Output is correct
7 Correct 15 ms 12720 KB Output is correct
8 Correct 16 ms 12672 KB Output is correct
9 Correct 18 ms 12672 KB Output is correct
10 Correct 15 ms 12800 KB Output is correct
11 Correct 16 ms 12672 KB Output is correct
12 Correct 15 ms 12672 KB Output is correct
13 Correct 17 ms 12800 KB Output is correct
14 Correct 16 ms 12800 KB Output is correct
15 Correct 16 ms 12672 KB Output is correct
16 Correct 14 ms 12800 KB Output is correct
17 Correct 14 ms 12800 KB Output is correct
18 Correct 15 ms 12928 KB Output is correct
19 Correct 13 ms 12800 KB Output is correct
20 Correct 14 ms 12800 KB Output is correct
21 Correct 17 ms 12800 KB Output is correct
22 Correct 16 ms 12928 KB Output is correct
23 Correct 15 ms 12800 KB Output is correct
24 Correct 15 ms 12800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 79 ms 19008 KB Output is correct
2 Correct 100 ms 21744 KB Output is correct
3 Correct 99 ms 19468 KB Output is correct
4 Correct 130 ms 21992 KB Output is correct
5 Correct 68 ms 18932 KB Output is correct
6 Correct 87 ms 19572 KB Output is correct
7 Correct 93 ms 20360 KB Output is correct
8 Correct 101 ms 20252 KB Output is correct
9 Correct 117 ms 22516 KB Output is correct
10 Correct 355 ms 44252 KB Output is correct
11 Correct 191 ms 26600 KB Output is correct
12 Correct 212 ms 30540 KB Output is correct
13 Correct 186 ms 27784 KB Output is correct
14 Correct 156 ms 25224 KB Output is correct
15 Correct 203 ms 30204 KB Output is correct
16 Correct 204 ms 31060 KB Output is correct
17 Correct 156 ms 29180 KB Output is correct
18 Correct 205 ms 29208 KB Output is correct
19 Correct 256 ms 35440 KB Output is correct
20 Correct 461 ms 50384 KB Output is correct
21 Correct 341 ms 37184 KB Output is correct
22 Correct 349 ms 34540 KB Output is correct
23 Correct 306 ms 36832 KB Output is correct
24 Correct 334 ms 36672 KB Output is correct
25 Correct 364 ms 35880 KB Output is correct
26 Correct 306 ms 34636 KB Output is correct
27 Correct 352 ms 37116 KB Output is correct
28 Correct 385 ms 37252 KB Output is correct
29 Correct 403 ms 42204 KB Output is correct
30 Correct 471 ms 52736 KB Output is correct
31 Correct 375 ms 40288 KB Output is correct
32 Correct 369 ms 37344 KB Output is correct
33 Correct 408 ms 37684 KB Output is correct
34 Correct 401 ms 39076 KB Output is correct
35 Correct 332 ms 37852 KB Output is correct
36 Correct 364 ms 38600 KB Output is correct
37 Correct 429 ms 41852 KB Output is correct
38 Correct 399 ms 41216 KB Output is correct
39 Correct 429 ms 41700 KB Output is correct
40 Correct 523 ms 50372 KB Output is correct
41 Correct 648 ms 62580 KB Output is correct