# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
49836 |
2018-06-03T14:55:52 Z |
aome |
Wall (CEOI14_wall) |
C++17 |
|
2000 ms |
51988 KB |
#include <bits/stdc++.h>
using namespace std;
const int N = 405;
const long long INF = 1e18;
struct Edge {
int x, y, w, d;
};
struct State {
long long v;
int x, y, d;
State (long long v, int x, int y, int d) :
v(v), x(x), y(y), d(d) {}
bool operator < (const State& rhs) const {
return d > rhs.d;
}
};
const int dx[] = {0, 0, -1, 1};
const int dy[] = {-1, 1, 0, 0};
int n, m;
bool a[N][N];
bool vis[N][N];
bool mark[4][N][N];
long long dis[N][N];
long long f[4][N][N];
vector<Edge> G[N][N];
void dijkstra() {
for (int i = 1; i <= n + 1; ++i) {
for (int j = 1; j <= m + 1; ++j) {
dis[i][j] = INF;
}
}
priority_queue<State> pq;
pq.push(State(dis[1][1] = 0, 1, 1, 0));
while (pq.size()) {
long long v = pq.top().v;
int x = pq.top().x, y = pq.top().y;
pq.pop();
if (v != dis[x][y]) continue;
for (auto i : G[x][y]) {
if (dis[i.x][i.y] > v + i.w) {
dis[i.x][i.y] = v + i.w;
pq.push(State(v + i.w, i.x, i.y, 0));
}
}
}
}
void rec(int x, int y) {
if (x == 1 && y == 1 || vis[x][y]) return;
vis[x][y] = 1;
for (auto i : G[x][y]) {
if (dis[i.x][i.y] + i.w != dis[x][y]) continue;
if (i.x == x) {
mark[1][x][min(i.y, y)] = mark[0][x][max(i.y, y)] = 1;
}
else {
mark[3][min(i.x, x)][y] = mark[2][max(i.x, x)][y] = 1;
}
rec(i.x, i.y);
return;
}
}
void solve() {
for (int i = 1; i <= n + 1; ++i) {
for (int j = 1; j <= m + 1; ++j) {
for (int k = 0; k < 4; ++k) {
f[k][i][j] = INF;
}
}
}
priority_queue<State> pq;
pq.push(State(f[1][1][2] = G[1][1][1].w, 1, 2, 1));
while (pq.size()) {
long long v = pq.top().v;
int x = pq.top().x, y = pq.top().y, d = pq.top().d;
pq.pop();
if (v != f[d][x][y]) continue;
// cout << x << ' ' << y << ' ' << d << ' ' << v << '\n';
if (d == 0) {
for (auto i : G[x][y]) {
if (i.d == 1) continue;
if (i.d == 0 && mark[3][x][y]) continue;
if (i.d == 2 && (mark[0][x][y] || mark[3][x][y])) continue;
if (f[i.d][i.x][i.y] > v + i.w) {
f[i.d][i.x][i.y] = v + i.w;
pq.push(State(v + i.w, i.x, i.y, i.d));
}
}
}
if (d == 1) {
for (auto i : G[x][y]) {
if (i.d == 0) continue;
if (i.d == 1 && mark[2][x][y]) continue;
if (i.d == 3 && (mark[1][x][y] || mark[2][x][y])) continue;
if (f[i.d][i.x][i.y] > v + i.w) {
f[i.d][i.x][i.y] = v + i.w;
pq.push(State(v + i.w, i.x, i.y, i.d));
}
}
}
if (d == 2) {
for (auto i : G[x][y]) {
if (i.d == 3) continue;
if (i.d == 2 && mark[0][x][y]) continue;
if (i.d == 1 && (mark[0][x][y] || mark[2][x][y])) continue;
if (f[i.d][i.x][i.y] > v + i.w) {
f[i.d][i.x][i.y] = v + i.w;
pq.push(State(v + i.w, i.x, i.y, i.d));
}
}
}
if (d == 3) {
for (auto i : G[x][y]) {
if (i.d == 2) continue;
if (i.d == 3 && mark[1][x][y]) continue;
if (i.d == 0 && (mark[1][x][y] || mark[3][x][y])) continue;
if (f[i.d][i.x][i.y] > v + i.w) {
f[i.d][i.x][i.y] = v + i.w;
pq.push(State(v + i.w, i.x, i.y, i.d));
}
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> a[i][j];
if (a[i][j]) {
mark[0][i][j + 1] = mark[0][i + 1][j + 1] = 1;
mark[1][i][j] = mark[1][i + 1][j] = 1;
mark[2][i + 1][j] = mark[2][i + 1][j + 1] = 1;
mark[3][i][j] = mark[3][i][j + 1] = 1;
}
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m + 1; ++j) {
int w; cin >> w;
G[i][j].push_back({i + 1, j, w, 3});
G[i + 1][j].push_back({i, j, w, 2});
}
}
for (int i = 1; i <= n + 1; ++i) {
for (int j = 1; j <= m; ++j) {
int w; cin >> w;
G[i][j].push_back({i, j + 1, w, 1});
G[i][j + 1].push_back({i, j, w, 0});
}
}
dijkstra();
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (a[i][j]) rec(i, j);
}
}
// for (int i = 1; i <= n + 1; ++i) {
// for (int j = 1; j <= m + 1; ++j) {
// for (int k = 1; k < 4; k += 2) {
// if (mark[k][i][j]) {
// cout << i << ' ' << j << ' ' << k << '\n';
// }
// }
// }
// }
solve();
cout << f[2][1][1];
}
Compilation message
wall.cpp: In function 'void rec(int, int)':
wall.cpp:58:13: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
if (x == 1 && y == 1 || vis[x][y]) return;
~~~~~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4420 KB |
Output is correct |
2 |
Correct |
6 ms |
4532 KB |
Output is correct |
3 |
Correct |
6 ms |
4568 KB |
Output is correct |
4 |
Correct |
5 ms |
4568 KB |
Output is correct |
5 |
Correct |
6 ms |
4592 KB |
Output is correct |
6 |
Correct |
31 ms |
5580 KB |
Output is correct |
7 |
Correct |
25 ms |
5580 KB |
Output is correct |
8 |
Correct |
35 ms |
5580 KB |
Output is correct |
9 |
Correct |
43 ms |
5580 KB |
Output is correct |
10 |
Correct |
45 ms |
5628 KB |
Output is correct |
11 |
Correct |
74 ms |
5668 KB |
Output is correct |
12 |
Correct |
152 ms |
6084 KB |
Output is correct |
13 |
Correct |
151 ms |
6464 KB |
Output is correct |
14 |
Correct |
196 ms |
6464 KB |
Output is correct |
15 |
Correct |
180 ms |
6464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
6464 KB |
Output is correct |
2 |
Correct |
160 ms |
6616 KB |
Output is correct |
3 |
Correct |
222 ms |
7372 KB |
Output is correct |
4 |
Correct |
82 ms |
7372 KB |
Output is correct |
5 |
Correct |
94 ms |
7372 KB |
Output is correct |
6 |
Correct |
425 ms |
7592 KB |
Output is correct |
7 |
Correct |
139 ms |
9032 KB |
Output is correct |
8 |
Correct |
189 ms |
9032 KB |
Output is correct |
9 |
Correct |
161 ms |
9032 KB |
Output is correct |
10 |
Correct |
75 ms |
9032 KB |
Output is correct |
11 |
Correct |
94 ms |
9032 KB |
Output is correct |
12 |
Correct |
368 ms |
9032 KB |
Output is correct |
13 |
Correct |
69 ms |
9032 KB |
Output is correct |
14 |
Correct |
145 ms |
9032 KB |
Output is correct |
15 |
Correct |
121 ms |
9032 KB |
Output is correct |
16 |
Correct |
103 ms |
9032 KB |
Output is correct |
17 |
Correct |
133 ms |
9032 KB |
Output is correct |
18 |
Correct |
291 ms |
9216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2064 ms |
11416 KB |
Time limit exceeded |
2 |
Execution timed out |
2062 ms |
11416 KB |
Time limit exceeded |
3 |
Execution timed out |
2060 ms |
11416 KB |
Time limit exceeded |
4 |
Execution timed out |
2059 ms |
11936 KB |
Time limit exceeded |
5 |
Execution timed out |
2065 ms |
20016 KB |
Time limit exceeded |
6 |
Execution timed out |
2054 ms |
20016 KB |
Time limit exceeded |
7 |
Execution timed out |
2065 ms |
24672 KB |
Time limit exceeded |
8 |
Execution timed out |
2060 ms |
24672 KB |
Time limit exceeded |
9 |
Execution timed out |
2064 ms |
24672 KB |
Time limit exceeded |
10 |
Execution timed out |
2047 ms |
24672 KB |
Time limit exceeded |
11 |
Execution timed out |
2054 ms |
26004 KB |
Time limit exceeded |
12 |
Execution timed out |
2045 ms |
26004 KB |
Time limit exceeded |
13 |
Execution timed out |
2058 ms |
27188 KB |
Time limit exceeded |
14 |
Execution timed out |
2060 ms |
36840 KB |
Time limit exceeded |
15 |
Execution timed out |
2064 ms |
38712 KB |
Time limit exceeded |
16 |
Execution timed out |
2065 ms |
38712 KB |
Time limit exceeded |
17 |
Execution timed out |
2071 ms |
43228 KB |
Time limit exceeded |
18 |
Execution timed out |
2066 ms |
51988 KB |
Time limit exceeded |
19 |
Execution timed out |
2074 ms |
51988 KB |
Time limit exceeded |
20 |
Execution timed out |
2065 ms |
51988 KB |
Time limit exceeded |