Submission #599848

# Submission time Handle Problem Language Result Execution time Memory
599848 2022-07-20T02:04:52 Z alexz1205 Mecho (IOI09_mecho) C++14
22 / 100
1000 ms 8568 KB
#include <iostream>
#include <vector>
#include <set>
using namespace std;

int n, s, dist[802][802], dist2[802][802], dist3[802][802];
bool req[802][802];
pair<int, int> beg, home;
vector<pair<int, int> > hives;

void setup(){
	cin >> n >> s;
	for (int x = 1; x <= n; x ++){
		for (int y = 1; y <= n; y ++){
			dist[x][y] = dist2[x][y] = -1;
			req[x][y] = 1;
			char c;
			cin >> c;
			switch (c){
				case 'T':
					dist[x][y] = dist2[x][y] = req[x][y] = 0;
					break;
				case 'G':
					break;
				case 'M':
					beg = make_pair(x, y);
					break;
				case 'D':
					home = make_pair(x, y);
					break;
				case 'H':
					dist[x][y] = req[x][y] = dist2[x][y] = 0;
					hives.push_back(make_pair(x, y));
					break;
			}
		}
	}
	for (int x = 0; x <= n+1; x ++){
		dist[x][0] = dist[0][x] = dist[n+1][x] = dist[x][n+1] = 0;
	}
}

void calc(){
	vector<pair<int, int> > que = hives;
	while (!que.empty()){
		int i = que.front().first, j = que.front().second;
		que.erase(que.begin());
		if (dist[i-1][j] == -1){
			dist[i-1][j] = dist[i][j] + s;
			que.push_back(make_pair(i-1, j));
		}
		if (dist[i+1][j] == -1){
			dist[i+1][j] = dist[i][j] + s;
			que.push_back(make_pair(i+1, j));
		}
		if (dist[i][j-1] == -1){
			dist[i][j-1] = dist[i][j] + s;
			que.push_back(make_pair(i, j-1));
		}
		if (dist[i][j+1] == -1){
			dist[i][j+1] = dist[i][j] + s;
			que.push_back(make_pair(i, j+1));
		}
	}
}

void mecho(){
	vector<pair<int, int> > que;
	que.push_back(beg);
	while (!que.empty()){
		int i = que.front().first, j = que.front().second;
		que.erase(que.begin());
		if (dist2[i-1][j] == -1){
			dist2[i-1][j] = dist2[i][j]+1;
			que.push_back(make_pair(i-1, j));
		}
		if (dist2[i][j-1] == -1){
			dist2[i][j-1] = dist2[i][j]+1;
			que.push_back(make_pair(i, j-1));
		}
		if (dist2[i+1][j] == -1){
			dist2[i+1][j] = dist2[i][j]+1;
			que.push_back(make_pair(i+1, j));
		}
		if (dist2[i][j+1] == -1){
			dist2[i][j+1] = dist2[i][j]+1;
			que.push_back(make_pair(i, j+1));
		}
	}
}

int main() {
	setup();
	calc();
	mecho();
	for (int x = 0; x <= n+1; x ++){
		for (int y = 0; y <= n+1; y ++){
			dist3[x][y] = (dist[x][y] - dist2[x][y] >= 0 ? (dist[x][y] - dist2[x][y]) / s : -1);
		}
	}
	set<pair<int, pair<int, int> > > pri;
	pri.insert(make_pair(dist3[beg.first][beg.second], beg));
	while (!pri.empty()){
		int m = pri.rbegin()->first;
		int i = pri.rbegin()->second.first, j = pri.rbegin()->second.second;
		pri.erase(--pri.end());
		if (i == home.first && j == home.second){
			cout << m << endl;
			break;
		}
		if (req[i-1][j]){
			req[i-1][j] = 0;
			pri.insert(make_pair(min(m, dist3[i-1][j]), make_pair(i-1, j)));
		}
		if (req[i][j-1]){
			req[i][j-1] = 0;
			pri.insert(make_pair(min(m, dist3[i][j-1]), make_pair(i, j-1)));
		}
		if (req[i+1][j]){
			req[i+1][j] = 0;
			pri.insert(make_pair(min(m, dist3[i+1][j]), make_pair(i+1, j)));
		}
		if (req[i][j+1]){
			req[i][j+1] = 0;
			pri.insert(make_pair(min(m, dist3[i][j+1]), make_pair(i, j+1)));
		}
	}
	return 0;
}

Compilation message

mecho.cpp: In function 'void setup()':
mecho.cpp:32:43: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   32 |      dist[x][y] = req[x][y] = dist2[x][y] = 0;
      |                               ~~~~~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Correct 0 ms 340 KB Output is correct
3 Incorrect 1 ms 340 KB Output isn't correct
4 Correct 0 ms 340 KB Output is correct
5 Incorrect 1 ms 340 KB Output isn't correct
6 Incorrect 1 ms 340 KB Output isn't correct
7 Execution timed out 1094 ms 6212 KB Time limit exceeded
8 Incorrect 1 ms 468 KB Output isn't correct
9 Correct 0 ms 468 KB Output is correct
10 Incorrect 1 ms 468 KB Output isn't correct
11 Incorrect 0 ms 468 KB Output isn't correct
12 Incorrect 1 ms 852 KB Output isn't correct
13 Correct 1 ms 852 KB Output is correct
14 Correct 2 ms 852 KB Output is correct
15 Incorrect 1 ms 468 KB Output isn't correct
16 Correct 1 ms 468 KB Output is correct
17 Incorrect 1 ms 468 KB Output isn't correct
18 Correct 0 ms 596 KB Output is correct
19 Incorrect 1 ms 596 KB Output isn't correct
20 Correct 1 ms 596 KB Output is correct
21 Incorrect 1 ms 724 KB Output isn't correct
22 Correct 1 ms 724 KB Output is correct
23 Incorrect 1 ms 724 KB Output isn't correct
24 Correct 1 ms 724 KB Output is correct
25 Incorrect 1 ms 852 KB Output isn't correct
26 Correct 1 ms 852 KB Output is correct
27 Incorrect 1 ms 980 KB Output isn't correct
28 Correct 1 ms 980 KB Output is correct
29 Incorrect 1 ms 980 KB Output isn't correct
30 Correct 2 ms 980 KB Output is correct
31 Incorrect 1 ms 980 KB Output isn't correct
32 Correct 1 ms 980 KB Output is correct
33 Incorrect 11 ms 3896 KB Output isn't correct
34 Correct 11 ms 3796 KB Output is correct
35 Incorrect 46 ms 3932 KB Output isn't correct
36 Incorrect 14 ms 4308 KB Output isn't correct
37 Correct 15 ms 4308 KB Output is correct
38 Incorrect 66 ms 4432 KB Output isn't correct
39 Incorrect 17 ms 4912 KB Output isn't correct
40 Correct 19 ms 4908 KB Output is correct
41 Incorrect 91 ms 4908 KB Output isn't correct
42 Incorrect 20 ms 5376 KB Output isn't correct
43 Correct 20 ms 5396 KB Output is correct
44 Incorrect 112 ms 5384 KB Output isn't correct
45 Incorrect 26 ms 5856 KB Output isn't correct
46 Correct 24 ms 5836 KB Output is correct
47 Incorrect 157 ms 6000 KB Output isn't correct
48 Incorrect 29 ms 6428 KB Output isn't correct
49 Correct 28 ms 6348 KB Output is correct
50 Incorrect 182 ms 6604 KB Output isn't correct
51 Incorrect 34 ms 6888 KB Output isn't correct
52 Correct 34 ms 6952 KB Output is correct
53 Incorrect 273 ms 6960 KB Output isn't correct
54 Incorrect 39 ms 7348 KB Output isn't correct
55 Correct 38 ms 7396 KB Output is correct
56 Incorrect 284 ms 7560 KB Output isn't correct
57 Incorrect 49 ms 7956 KB Output isn't correct
58 Correct 47 ms 7944 KB Output is correct
59 Incorrect 333 ms 8052 KB Output isn't correct
60 Incorrect 53 ms 8380 KB Output isn't correct
61 Correct 50 ms 8376 KB Output is correct
62 Incorrect 412 ms 8484 KB Output isn't correct
63 Incorrect 227 ms 8400 KB Output isn't correct
64 Correct 200 ms 8392 KB Output is correct
65 Incorrect 197 ms 8496 KB Output isn't correct
66 Incorrect 196 ms 8436 KB Output isn't correct
67 Correct 186 ms 8396 KB Output is correct
68 Correct 738 ms 8568 KB Output is correct
69 Correct 747 ms 8564 KB Output is correct
70 Correct 687 ms 8440 KB Output is correct
71 Correct 695 ms 8440 KB Output is correct
72 Correct 672 ms 8464 KB Output is correct
73 Execution timed out 1081 ms 6508 KB Time limit exceeded
74 Execution timed out 1097 ms 6424 KB Time limit exceeded
75 Execution timed out 1096 ms 6496 KB Time limit exceeded
76 Execution timed out 1089 ms 6544 KB Time limit exceeded
77 Execution timed out 1081 ms 6476 KB Time limit exceeded
78 Execution timed out 1085 ms 6560 KB Time limit exceeded
79 Execution timed out 1050 ms 6448 KB Time limit exceeded
80 Execution timed out 1096 ms 6384 KB Time limit exceeded
81 Execution timed out 1075 ms 6424 KB Time limit exceeded
82 Execution timed out 1087 ms 6460 KB Time limit exceeded
83 Execution timed out 1099 ms 6352 KB Time limit exceeded
84 Execution timed out 1090 ms 6500 KB Time limit exceeded
85 Execution timed out 1094 ms 6480 KB Time limit exceeded
86 Execution timed out 1082 ms 6460 KB Time limit exceeded
87 Execution timed out 1072 ms 6468 KB Time limit exceeded
88 Execution timed out 1099 ms 6364 KB Time limit exceeded
89 Execution timed out 1090 ms 6564 KB Time limit exceeded
90 Execution timed out 1066 ms 6220 KB Time limit exceeded
91 Execution timed out 1100 ms 6204 KB Time limit exceeded
92 Execution timed out 1091 ms 6280 KB Time limit exceeded