Submission #117602

# Submission time Handle Problem Language Result Execution time Memory
117602 2019-06-16T18:18:05 Z KieranHorgan Mecho (IOI09_mecho) C++17
53 / 100
111 ms 11768 KB
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define ll long long
// #define int short
#define ld double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int MOD = 1000000007;

// char grid[805][805];
// int dist1[805][805];
// pair<int,int> dist3[805][805];
// int dist2[805][805];
// int dist[805][805];
// int vis[805][805];

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	long long seed;
	asm("rdtsc" : "=A"(seed));
	srand(seed);

	int n, s;
	cin >> n >> s;
	int si, sj;
	int fi, fj;
	vector<pair<int,int>> p;
	vector<vector<char>> grid(n+2, vector<char>(n+2,0));
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++) {
			cin >> grid[i][j];
			if(grid[i][j]=='M') {
				si = i;
				sj = j;
			}
			if(grid[i][j]=='D') {
				fi = i;
				fj = j;
			}
			if(grid[i][j]=='H') {
				p.push_back({i,j});
			}
			if(grid[i][j]=='T') grid[i][j]=0;
		}
	}

	vector<int> di = {1,0,-1,0};
	vector<int> dj = {0,1,0,-1};

	queue<pair<int,pair<int,int>>> q;

	vector<vector<int>> dist1(n+2, vector<int>(n+2,-1));
	dist1[si][sj]=0;
	q.push({0,{si,sj}});
	while(!q.empty()) {
		int d = q.front().first;
		int i = q.front().second.first;
		int j = q.front().second.second;
		q.pop();
		for(int x = 0; x < 4; x++) {
			if(grid[i+di[x]][j+dj[x]] == 0) continue;
			if(dist1[i+di[x]][j+dj[x]] != -1) continue;
			dist1[i+di[x]][j+dj[x]] = d+1;
			q.push({d+1,{i+di[x],j+dj[x]}});
		}
	}
	vector<vector<pair<int,int>>> dist3(n+2, vector<pair<int,int>>(n+2));
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= n; j++)
			if(dist1[i][j]>0) {
				dist3[i][j] = {(dist1[i][j]+s-1)/s,(dist1[i][j]+s-1)%s};
			}



	vector<vector<int>> dist2(n+2, vector<int>(n+2,-1));
	for(auto pp: p) {
		int pi = pp.first;
		int pj = pp.second;
		dist2[pi][pj]=0;
		q.push({0,{pi,pj}});
	}
	while(!q.empty()) {
		int d = q.front().first;
		int i = q.front().second.first;
		int j = q.front().second.second;
		q.pop();
		for(int x = 0; x < 4; x++) {
			if(grid[i+di[x]][j+dj[x]] == 0) continue;
			if(dist2[i+di[x]][j+dj[x]] != -1) continue;
			if(grid[i+di[x]][j+dj[x]] == 'D') continue;
			dist2[i+di[x]][j+dj[x]] = d+1;
			q.push({d+1,{i+di[x],j+dj[x]}});
		}
	}


	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= n; j++)
			if(dist1[i][j]>=0) {
				dist2[i][j] = dist2[i][j]-dist3[i][j].first-(dist3[i][j].second==s-1);
			}

	// for(int i = 1; i <= n; i++) {
		// for(int j = 1; j <= n; j++) {
			// cerr << dist3[i][j].first << ":" << dist3[i][j].second << " ";
		// }
		// cerr << endl;
	// }
	// cerr << endl;
	// for(int i = 1; i <= n; i++) {
		// for(int j = 1; j <= n; j++) {
			// cerr << dist2[i][j] << " ";
		// }
		// cerr << endl;
	// }
	// cerr << endl;
	// for(int i = 1; i <= n; i++) {
		// for(int j = 1; j <= n; j++) {
			// cerr << dist2[i][j] << " ";
		// }
		// cerr << endl;
	// }
	// cerr << endl;

	vector<vector<bool>> vis(n+2, vector<bool>(n+2,0));
	priority_queue<pair<int,pair<int,int>>> pq;
	pq.push({dist2[si][sj], {si, sj}});
	int ans = dist2[si][sj];
	bool toBreak = 0;
	while(!pq.empty()) {
		int d = pq.top().first;
		int i = pq.top().second.first;
		int j = pq.top().second.second;
		pq.pop();
		ans = min(ans, d);
		if(i==fi && j == fj) break;
		for(int x = 0; x < 4; x++) {
			if(grid[i+di[x]][j+dj[x]] == 'D') {toBreak = 1; break;}
			if(grid[i+di[x]][j+dj[x]] == 0) continue;
			if(dist1[i+di[x]][j+dj[x]] <= 0) continue;
			if(vis[i+di[x]][j+dj[x]]) continue;
			vis[i+di[x]][j+dj[x]] = 1;
			pq.push({dist2[i+di[x]][j+dj[x]],{i+di[x],j+dj[x]}});
		}
		if(toBreak)break;
	}
	cout << ans << endl;
}

Compilation message

mecho.cpp:3:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
 #pragma GCC optimization ("unroll-loops")
 
mecho.cpp: In function 'int main()':
mecho.cpp:144:17: warning: 'fj' may be used uninitialized in this function [-Wmaybe-uninitialized]
   if(i==fi && j == fj) break;
               ~~^~~~~
mecho.cpp:144:7: warning: 'fi' may be used uninitialized in this function [-Wmaybe-uninitialized]
   if(i==fi && j == fj) break;
      ~^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 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 384 KB Output is correct
7 Correct 100 ms 11256 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Incorrect 3 ms 384 KB Output isn't correct
14 Incorrect 3 ms 384 KB Output isn't correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 2 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 2 ms 384 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 13 ms 384 KB Output is correct
27 Correct 2 ms 512 KB Output is correct
28 Correct 2 ms 384 KB Output is correct
29 Correct 2 ms 384 KB Output is correct
30 Correct 2 ms 384 KB Output is correct
31 Correct 2 ms 512 KB Output is correct
32 Correct 3 ms 384 KB Output is correct
33 Correct 8 ms 2432 KB Output is correct
34 Correct 8 ms 2560 KB Output is correct
35 Incorrect 21 ms 2560 KB Output isn't correct
36 Correct 10 ms 3200 KB Output is correct
37 Correct 9 ms 3200 KB Output is correct
38 Incorrect 25 ms 3200 KB Output isn't correct
39 Correct 11 ms 3840 KB Output is correct
40 Correct 11 ms 3840 KB Output is correct
41 Incorrect 33 ms 3840 KB Output isn't correct
42 Correct 15 ms 4608 KB Output is correct
43 Correct 14 ms 4736 KB Output is correct
44 Incorrect 40 ms 4608 KB Output isn't correct
45 Correct 16 ms 5504 KB Output is correct
46 Correct 15 ms 5504 KB Output is correct
47 Incorrect 48 ms 5632 KB Output isn't correct
48 Correct 19 ms 6528 KB Output is correct
49 Correct 18 ms 6528 KB Output is correct
50 Incorrect 60 ms 6528 KB Output isn't correct
51 Correct 22 ms 7652 KB Output is correct
52 Correct 21 ms 7680 KB Output is correct
53 Incorrect 71 ms 7644 KB Output isn't correct
54 Correct 25 ms 8676 KB Output is correct
55 Correct 26 ms 8832 KB Output is correct
56 Incorrect 81 ms 8824 KB Output isn't correct
57 Correct 29 ms 10104 KB Output is correct
58 Correct 27 ms 9976 KB Output is correct
59 Incorrect 96 ms 9956 KB Output isn't correct
60 Correct 32 ms 11256 KB Output is correct
61 Correct 33 ms 11256 KB Output is correct
62 Incorrect 111 ms 11356 KB Output isn't correct
63 Correct 92 ms 11256 KB Output is correct
64 Correct 91 ms 11308 KB Output is correct
65 Correct 92 ms 11304 KB Output is correct
66 Correct 92 ms 11384 KB Output is correct
67 Incorrect 91 ms 11256 KB Output isn't correct
68 Correct 77 ms 11256 KB Output is correct
69 Correct 74 ms 11256 KB Output is correct
70 Correct 74 ms 11256 KB Output is correct
71 Correct 74 ms 11256 KB Output is correct
72 Incorrect 69 ms 11256 KB Output isn't correct
73 Incorrect 82 ms 11640 KB Output isn't correct
74 Correct 77 ms 11640 KB Output is correct
75 Correct 84 ms 11768 KB Output is correct
76 Correct 78 ms 11640 KB Output is correct
77 Correct 77 ms 11640 KB Output is correct
78 Incorrect 95 ms 11680 KB Output isn't correct
79 Correct 74 ms 11512 KB Output is correct
80 Correct 83 ms 11512 KB Output is correct
81 Correct 83 ms 11512 KB Output is correct
82 Correct 75 ms 11640 KB Output is correct
83 Correct 94 ms 11520 KB Output is correct
84 Correct 86 ms 11512 KB Output is correct
85 Correct 94 ms 11644 KB Output is correct
86 Correct 92 ms 11492 KB Output is correct
87 Correct 91 ms 11512 KB Output is correct
88 Correct 102 ms 11476 KB Output is correct
89 Correct 104 ms 11556 KB Output is correct
90 Correct 98 ms 11384 KB Output is correct
91 Correct 98 ms 11384 KB Output is correct
92 Correct 99 ms 11384 KB Output is correct