Submission #269720

# Submission time Handle Problem Language Result Execution time Memory
269720 2020-08-17T09:10:44 Z Lam_lai_cuoc_doi Portals (BOI14_portals) C++17
100 / 100
314 ms 38968 KB
#define NguyenDangQuan the_author
#define my_heart love_you_to_the_moon_and_back

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;

bool typetest;
inline void fastIOfileinput()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	typetest = 0;
}

const int N = 1e3 + 3;
const int Inf = 1e9 + 7;
int m, n, xs, ys, xc, yc, d[N][N];
char s[N][N];
int directx[N][N][4], directy[N][N][4];
int x[] = {1, 0, -1, 0}, y[] = {0, 1, 0, -1};
bool ck[N][N];

void Read()
{
	cin >> m >> n;
	fill_n(&s[0][0], N * N, '#');
	for (int i = 1; i <= m; ++i)
		for (int j = 1; j <= n; ++j)
		{
			cin >> s[i][j];
			if (s[i][j] == 'S')
			{
				xs = i;
				ys = j;
			}
			if (s[i][j] == 'C')
			{
				xc = i;
				yc = j;
			}
		}
}

bool Relax(int x, int y, int a, int b, int w)
{
	if (d[a][b] > d[x][y] + w)
	{
		d[a][b] = d[x][y] + w;
		return true;
	}
	return false;
}

int Dijkstra()
{
	struct Tque
	{
		int x, y, w;
		Tque() {}
		Tque(int x, int y, int w)
		{
			this->x = x;
			this->y = y;
			this->w = w;
		}
		bool operator<(const Tque &a) const
		{
			return w > a.w;
		}
		bool Valid()
		{
			return d[x][y] == w;
		}
	};
	priority_queue<Tque> pq;
	fill_n(&d[0][0], N * N, Inf);
	d[xs][ys] = 0;
	pq.push(Tque(xs, ys, 0));
	while (pq.size())
	{
		Tque c = pq.top();
		pq.pop();
		if (!c.Valid() || ck[c.x][c.y])
			continue;
		ck[c.x][c.y] = 1;
		if (c.x == xc && c.y == yc)
			return c.w;
		int maxn = Inf, pos;
		for (int i = 0; i < 4; ++i)
		{
			int a = c.x + x[i],
				b = c.y + y[i];
			if (maxn > abs(c.x - directx[c.x][c.y][i]) + abs(c.y - directy[c.x][c.y][i]))
			{
				maxn = abs(c.x - directx[c.x][c.y][i]) + abs(c.y - directy[c.x][c.y][i]);
				pos = i;
			}
			if (s[a][b] != '#' && !ck[a][b] && Relax(c.x, c.y, a, b, 1))
				pq.push(Tque(a, b, d[a][b]));
		}
		for (int i = 0; i < 4; ++i)
			if (pos != i)
			{
				int a = directx[c.x][c.y][i],
					b = directy[c.x][c.y][i];
				if (s[a][b] != '#' && !ck[a][b] && Relax(c.x, c.y, a, b, maxn + 1))
					pq.push(Tque(a, b, d[a][b]));
			}
	}
}

void Solve()
{
	/// Calculate up and dow
	for (int j = 1; j <= n; ++j)
	{
		int last = 1;
		for (int i = 1; i <= m; ++i)
		{
			if (s[i][j] == '#')
				last = i + 1;
			else
				directx[i][j][0] = last;
			directy[i][j][0] = j;
		}
		last = m;
		for (int i = m; i; --i)
		{
			if (s[i][j] == '#')
				last = i - 1;
			else
				directx[i][j][1] = last;
			directy[i][j][1] = j;
		}
	}
	/// Calculate lef and rig
	for (int i = 1; i <= m; ++i)
	{
		int last = 1;
		for (int j = 1; j <= n; ++j)
		{
			if (s[i][j] == '#')
				last = j + 1;
			else
				directy[i][j][2] = last;
			directx[i][j][2] = i;
		}
		last = n;
		for (int j = n; j; --j)
		{
			if (s[i][j] == '#')
				last = j - 1;
			else
				directy[i][j][3] = last;
			directx[i][j][3] = i;
		}
	}
	cout << Dijkstra();
}

int32_t main()
{
	fastIOfileinput();
	if (typetest)
	{
		int t;
		cin >> t;
		while (t--)
		{
			Read();
			Solve();
		}
	}
	else
	{
		Read();
		Solve();
	}
}

Compilation message

portals.cpp: In function 'int Dijkstra()':
portals.cpp:79:23: warning: control reaches end of non-void function [-Wreturn-type]
   79 |  priority_queue<Tque> pq;
      |                       ^~
portals.cpp:106:4: warning: 'pos' may be used uninitialized in this function [-Wmaybe-uninitialized]
  106 |    if (pos != i)
      |    ^~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5248 KB Output is correct
2 Correct 3 ms 5376 KB Output is correct
3 Correct 3 ms 5376 KB Output is correct
4 Correct 3 ms 5376 KB Output is correct
5 Correct 3 ms 5376 KB Output is correct
6 Correct 5 ms 5376 KB Output is correct
7 Correct 4 ms 5376 KB Output is correct
8 Correct 5 ms 5376 KB Output is correct
9 Correct 4 ms 5376 KB Output is correct
10 Correct 3 ms 5248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5248 KB Output is correct
2 Correct 4 ms 5376 KB Output is correct
3 Correct 4 ms 5376 KB Output is correct
4 Correct 4 ms 5376 KB Output is correct
5 Correct 4 ms 5376 KB Output is correct
6 Correct 4 ms 5376 KB Output is correct
7 Correct 4 ms 5376 KB Output is correct
8 Correct 4 ms 5376 KB Output is correct
9 Correct 5 ms 5760 KB Output is correct
10 Correct 5 ms 5760 KB Output is correct
11 Correct 4 ms 5760 KB Output is correct
12 Correct 4 ms 5760 KB Output is correct
13 Correct 4 ms 5760 KB Output is correct
14 Correct 4 ms 5376 KB Output is correct
15 Correct 4 ms 5760 KB Output is correct
16 Correct 3 ms 5248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5248 KB Output is correct
2 Correct 3 ms 5376 KB Output is correct
3 Correct 4 ms 5376 KB Output is correct
4 Correct 4 ms 5376 KB Output is correct
5 Correct 9 ms 8320 KB Output is correct
6 Correct 12 ms 8320 KB Output is correct
7 Correct 9 ms 8320 KB Output is correct
8 Correct 8 ms 8320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5248 KB Output is correct
2 Correct 4 ms 5376 KB Output is correct
3 Correct 3 ms 5376 KB Output is correct
4 Correct 5 ms 5264 KB Output is correct
5 Correct 4 ms 5376 KB Output is correct
6 Correct 3 ms 5376 KB Output is correct
7 Correct 3 ms 5376 KB Output is correct
8 Correct 4 ms 5376 KB Output is correct
9 Correct 4 ms 5760 KB Output is correct
10 Correct 4 ms 5760 KB Output is correct
11 Correct 4 ms 5760 KB Output is correct
12 Correct 4 ms 5776 KB Output is correct
13 Correct 4 ms 5760 KB Output is correct
14 Correct 9 ms 8320 KB Output is correct
15 Correct 10 ms 8320 KB Output is correct
16 Correct 9 ms 8320 KB Output is correct
17 Correct 10 ms 8320 KB Output is correct
18 Correct 10 ms 8320 KB Output is correct
19 Correct 6 ms 8192 KB Output is correct
20 Correct 9 ms 8448 KB Output is correct
21 Correct 9 ms 8448 KB Output is correct
22 Correct 9 ms 8320 KB Output is correct
23 Correct 10 ms 8320 KB Output is correct
24 Correct 11 ms 8320 KB Output is correct
25 Correct 3 ms 5376 KB Output is correct
26 Correct 4 ms 5760 KB Output is correct
27 Correct 4 ms 5248 KB Output is correct
28 Correct 8 ms 8320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5248 KB Output is correct
2 Correct 5 ms 5376 KB Output is correct
3 Correct 5 ms 5376 KB Output is correct
4 Correct 3 ms 5248 KB Output is correct
5 Correct 4 ms 5408 KB Output is correct
6 Correct 4 ms 5376 KB Output is correct
7 Correct 4 ms 5376 KB Output is correct
8 Correct 4 ms 5376 KB Output is correct
9 Correct 5 ms 5888 KB Output is correct
10 Correct 4 ms 5760 KB Output is correct
11 Correct 4 ms 5760 KB Output is correct
12 Correct 4 ms 5760 KB Output is correct
13 Correct 4 ms 5760 KB Output is correct
14 Correct 10 ms 8320 KB Output is correct
15 Correct 12 ms 8320 KB Output is correct
16 Correct 9 ms 8320 KB Output is correct
17 Correct 10 ms 8320 KB Output is correct
18 Correct 11 ms 8448 KB Output is correct
19 Correct 6 ms 8320 KB Output is correct
20 Correct 10 ms 8448 KB Output is correct
21 Correct 12 ms 8320 KB Output is correct
22 Correct 9 ms 8320 KB Output is correct
23 Correct 10 ms 8320 KB Output is correct
24 Correct 204 ms 38776 KB Output is correct
25 Correct 314 ms 38968 KB Output is correct
26 Correct 91 ms 38136 KB Output is correct
27 Correct 182 ms 38904 KB Output is correct
28 Correct 160 ms 38648 KB Output is correct
29 Correct 183 ms 38648 KB Output is correct
30 Correct 168 ms 38732 KB Output is correct
31 Correct 12 ms 8320 KB Output is correct
32 Correct 258 ms 38764 KB Output is correct
33 Correct 4 ms 5376 KB Output is correct
34 Correct 5 ms 5760 KB Output is correct
35 Correct 175 ms 38776 KB Output is correct
36 Correct 5 ms 5248 KB Output is correct
37 Correct 9 ms 8320 KB Output is correct
38 Correct 129 ms 38648 KB Output is correct
39 Correct 117 ms 38648 KB Output is correct