답안 #394850

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
394850 2021-04-27T11:16:22 Z Sara 포탈들 (BOI14_portals) C++14
100 / 100
583 ms 128088 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define ll long long
#define F first
#define S second
#define pb push_back
#define add if (!A[x][y]) {g[id[i][j]].pb({id[x][y], w});}

const ll N = 1000 + 3;
const ll LOG = 17;
const ll INF = 1e9 + 5;
const ll MOD = 1e9 + 7;

int n, m;
string s;
bool A[N][N];
int id[N][N], ind = 1;

vector < pair <int, int> > g[N * N];
pair <int, int> st, en;

int le[N][N], ri[N][N];
int up[N][N], dw[N][N];

inline void pre(){
    for (int i = 0; i <= n + 1; i ++){
        for (int j = 0; j <= m + 1; j ++){
            if (A[i][j]){
                le[i][j] = j;
                up[i][j] = i;
            }
            else {
                le[i][j] = le[i][j - 1];
                up[i][j] = up[i - 1][j];
            }
        }
    }
    for (int i = n + 1; i >= 0; i --){
        for (int j = m + 1; j >= 0; j --){
            if (A[i][j]){
                ri[i][j] = j;
                dw[i][j] = i;
            }
            else {
                ri[i][j] = ri[i][j + 1];
                dw[i][j] = dw[i + 1][j];
            }
        }
    }
    return;
}


int dis[N * N];
inline void add_edges(){
    for (int i = 1; i <= n; i ++){
        for (int j = 1; j <= m; j ++){
            if (A[i][j]) continue;
            int x, y;

            int w = min({j - le[i][j], ri[i][j] - j, i - up[i][j], dw[i][j] - i});
            x = i, y = le[i][j] + 1; add
            x = i, y = ri[i][j] - 1; add
            x = up[i][j] + 1, y = j; add
            x = dw[i][j] - 1, y = j; add

            w = 1;
            x = i, y = j - 1; add
            x = i, y = j + 1; add
            x = i - 1, y = j; add
            x = i + 1, y = j; add
        }
    }
    return;
}

bool mark[N * N];
priority_queue < pair <int, int> > pq;
inline void dij(){
    for (int i = 0; i <= n + 1; i ++){
        for (int j = 0; j <= m + 1; j ++){
            dis[id[i][j]] = INF;
        }
    }
    dis[id[st.F][st.S]] = 0;
    pq.push({0, id[st.F][st.S]});
    while (pq.size()){
        int v = pq.top().S;
        pq.pop();
        if (mark[v]) continue;
        mark[v] = 1;
        for (auto it : g[v]){
            int w = it.S;
            int u = it.F;
            if (dis[v] + w < dis[u]){
                dis[u] = dis[v] + w;
                pq.push({-dis[u], u});
            }
        }
    }
    cout << dis[id[en.F][en.S]] << '\n';
    return;
}

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i ++){
        cin >> s; s = ' ' + s;
        for (int j = 1; j <= m; j ++){
            if (s[j] == 'S'){
                st = {i, j};
            }
            else if (s[j] == 'C'){
                en = {i, j};
            }
            A[i][j] = (s[j] == '#');
        }
    }
    for (int i = 0; i <= n + 1; i ++){
        A[i][0] = A[i][m + 1] = 1;
    }
    for (int j = 0; j <= m + 1; j ++){
        A[0][j] = A[n + 1][j] = 1;
    }
    for (int i = 0; i <= n +1 ; i ++){
        for (int j = 0; j <= m + 1; j ++){
            id[i][j] = ind; 
            ind ++;
        }
    }
    pre();
    add_edges();
    dij();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 24012 KB Output is correct
2 Correct 13 ms 24140 KB Output is correct
3 Correct 13 ms 24136 KB Output is correct
4 Correct 13 ms 24120 KB Output is correct
5 Correct 14 ms 24268 KB Output is correct
6 Correct 13 ms 24140 KB Output is correct
7 Correct 13 ms 24164 KB Output is correct
8 Correct 13 ms 24140 KB Output is correct
9 Correct 13 ms 24012 KB Output is correct
10 Correct 13 ms 24076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 24012 KB Output is correct
2 Correct 14 ms 24144 KB Output is correct
3 Correct 14 ms 24140 KB Output is correct
4 Correct 13 ms 24140 KB Output is correct
5 Correct 14 ms 24148 KB Output is correct
6 Correct 13 ms 24188 KB Output is correct
7 Correct 13 ms 24116 KB Output is correct
8 Correct 13 ms 24140 KB Output is correct
9 Correct 15 ms 25236 KB Output is correct
10 Correct 15 ms 25164 KB Output is correct
11 Correct 16 ms 25120 KB Output is correct
12 Correct 14 ms 25164 KB Output is correct
13 Correct 15 ms 25184 KB Output is correct
14 Correct 13 ms 24012 KB Output is correct
15 Correct 15 ms 25164 KB Output is correct
16 Correct 13 ms 23996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23976 KB Output is correct
2 Correct 13 ms 24140 KB Output is correct
3 Correct 14 ms 24140 KB Output is correct
4 Correct 13 ms 24144 KB Output is correct
5 Correct 24 ms 30156 KB Output is correct
6 Correct 23 ms 30212 KB Output is correct
7 Correct 25 ms 30296 KB Output is correct
8 Correct 23 ms 30428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 24012 KB Output is correct
2 Correct 14 ms 24140 KB Output is correct
3 Correct 13 ms 24140 KB Output is correct
4 Correct 14 ms 24060 KB Output is correct
5 Correct 13 ms 24140 KB Output is correct
6 Correct 14 ms 24132 KB Output is correct
7 Correct 13 ms 24232 KB Output is correct
8 Correct 14 ms 24140 KB Output is correct
9 Correct 14 ms 25256 KB Output is correct
10 Correct 15 ms 25164 KB Output is correct
11 Correct 15 ms 25184 KB Output is correct
12 Correct 14 ms 25132 KB Output is correct
13 Correct 14 ms 25164 KB Output is correct
14 Correct 22 ms 30108 KB Output is correct
15 Correct 24 ms 30220 KB Output is correct
16 Correct 24 ms 30284 KB Output is correct
17 Correct 24 ms 30340 KB Output is correct
18 Correct 27 ms 30796 KB Output is correct
19 Correct 29 ms 31436 KB Output is correct
20 Correct 28 ms 31472 KB Output is correct
21 Correct 23 ms 30156 KB Output is correct
22 Correct 24 ms 30156 KB Output is correct
23 Correct 24 ms 30260 KB Output is correct
24 Correct 28 ms 31440 KB Output is correct
25 Correct 13 ms 24080 KB Output is correct
26 Correct 14 ms 25164 KB Output is correct
27 Correct 13 ms 24072 KB Output is correct
28 Correct 22 ms 30440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23980 KB Output is correct
2 Correct 13 ms 24140 KB Output is correct
3 Correct 14 ms 24200 KB Output is correct
4 Correct 15 ms 24012 KB Output is correct
5 Correct 14 ms 24140 KB Output is correct
6 Correct 14 ms 24140 KB Output is correct
7 Correct 14 ms 24232 KB Output is correct
8 Correct 13 ms 24236 KB Output is correct
9 Correct 14 ms 25164 KB Output is correct
10 Correct 15 ms 25164 KB Output is correct
11 Correct 14 ms 25180 KB Output is correct
12 Correct 15 ms 25236 KB Output is correct
13 Correct 14 ms 25164 KB Output is correct
14 Correct 23 ms 30088 KB Output is correct
15 Correct 24 ms 30196 KB Output is correct
16 Correct 24 ms 30384 KB Output is correct
17 Correct 25 ms 30284 KB Output is correct
18 Correct 27 ms 30796 KB Output is correct
19 Correct 29 ms 31460 KB Output is correct
20 Correct 28 ms 31476 KB Output is correct
21 Correct 23 ms 30156 KB Output is correct
22 Correct 24 ms 30164 KB Output is correct
23 Correct 25 ms 30272 KB Output is correct
24 Correct 308 ms 103184 KB Output is correct
25 Correct 583 ms 128068 KB Output is correct
26 Correct 485 ms 128088 KB Output is correct
27 Correct 467 ms 127840 KB Output is correct
28 Correct 239 ms 94252 KB Output is correct
29 Correct 256 ms 95364 KB Output is correct
30 Correct 289 ms 96324 KB Output is correct
31 Correct 28 ms 31444 KB Output is correct
32 Correct 463 ms 127704 KB Output is correct
33 Correct 13 ms 24048 KB Output is correct
34 Correct 14 ms 25172 KB Output is correct
35 Correct 335 ms 108316 KB Output is correct
36 Correct 13 ms 24012 KB Output is correct
37 Correct 22 ms 30420 KB Output is correct
38 Correct 219 ms 101728 KB Output is correct
39 Correct 208 ms 88612 KB Output is correct