Submission #499464

# Submission time Handle Problem Language Result Execution time Memory
499464 2021-12-28T13:09:28 Z Lobo Portals (BOI14_portals) C++17
100 / 100
667 ms 92728 KB
#include<bits/stdc++.h>
using namespace std;

/*for ordered_set
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<pair<int,int>, null_type,less<pair<int,int>>, rb_tree_tag,tree_order_statistics_node_update>
*/

const long long INFll = (long long) 1e18 + 10;
const int INFii = (int) 1e9 + 10;
typedef long long ll;
typedef int ii;
typedef long double dbl;
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()

#define maxn 1100

ii n, m, a[maxn][maxn], d[maxn][maxn], dp[maxn][maxn];
ii dx[] = {0,0,1,-1};
ii dy[] = {1,-1,0,0};
vector<pair<ii,ii>> wall[maxn][maxn];

void msbfs() {
    queue<pair<ii,ii>> q;
    for(ii i = 0; i <= n+1; i++) {
        for(ii j = 0; j <= m+1; j++) {
            if(a[i][j] == 0) {
                q.push(mp(i,j));
                dp[i][j] = 0;
            }
            else {
                dp[i][j] = INFii;
            }
        }
    }

    while(q.size()) {
        ii x = q.front().fr;
        ii y = q.front().sc;   
        q.pop();

        for(ii i = 0; i < 4; i++) {
            ii x1 = x+dx[i];
            ii y1 = y+dy[i];

            if(x1 == -1 || x1 == n+2 || y1 == -1 || y1 == m+2) continue;


            if(dp[x1][y1] > dp[x][y]+1) {
                dp[x1][y1] = dp[x][y]+1;
                q.push(mp(x1,y1));
            }
        }
    }
}

void shortp(ii sx, ii sy) {
    for(ii i = 0; i <= n+1; i++) {
        for(ii j = 0; j <= m+1; j++) {
            d[i][j] = INFii;
        }
    }

    d[sx][sy] = 0;
    priority_queue<pair<ii,pair<ii,ii>>> pq;
    pq.push(mp(0,mp(sx,sy)));

    while(pq.size()) {
        ii x = pq.top().sc.fr;
        ii y = pq.top().sc.sc;
        ii dist = -pq.top().fr;
        pq.pop();

        if(d[x][y] != dist) continue;

        //adj
        for(ii i = 0; i < 4; i++) {
            ii x1 = x+dx[i];
            ii y1 = y+dy[i];

            if(x1 == -1 || x1 == n+2 || y1 == -1 || y1 == m+2 || a[x1][y1] == 0) continue;

            if(d[x1][y1] > d[x][y]+1) {
                d[x1][y1] = d[x][y]+1;
                pq.push(mp(-d[x1][y1],mp(x1,y1)));
            }
        }

        //wall
        for(auto X : wall[x][y]) {
            ii x1 = X.fr;
            ii y1 = X.sc;

            if(d[x1][y1] > d[x][y]+dp[x][y]) {
                d[x1][y1] = d[x][y]+dp[x][y];
                pq.push(mp(-d[x1][y1],mp(x1,y1)));
            }
        }
    }
}

void solve() {
    cin >> n >> m;

    pair<ii,ii> S, C;

    for(ii i = 1; i <= n; i++) {
        string s; cin >> s;
        for(ii j = 1; j <= m; j++) {
            if(s[j-1] == '#') {
                a[i][j] = 0;
            }
            else {
                a[i][j] = 1;
            }

            if(s[j-1] == 'S') {
                S = mp(i,j);
            }
            if(s[j-1] == 'C') {
                C = mp(i,j);
            }
        }
    }

    //left
    for(ii i = 1; i <= n; i++) {
        pair<ii,ii> act = mp(-1,0);
        for(ii j = 1; j <= m; j++) {
            if(a[i][j] == 0) {
                act.fr = -1;
                continue;
            }

            if(act.fr == -1) {
                act = mp(i,j);
            }

            wall[i][j].pb(act);
        }
    }

    //right
    for(ii i = 1; i <= n; i++) {
        pair<ii,ii> act = mp(-1,0);
        for(ii j = m; j >= 1; j--) {
            if(a[i][j] == 0) {
                act.fr = -1;
                continue;
            }

            if(act.fr == -1) {
                act = mp(i,j);
            }

            wall[i][j].pb(act);
        }
    }

    //up
    for(ii j = 1; j <= m; j++) {
        pair<ii,ii> act = mp(-1,0);
        for(ii i = 1; i <= n; i++) {
            if(a[i][j] == 0) {
                act.fr = -1;
                continue;
            }

            if(act.fr == -1) {
                act = mp(i,j);
            }

            wall[i][j].pb(act);
        }
    }

    //down
    for(ii j = 1; j <= m; j++) {
        pair<ii,ii> act = mp(-1,0);
        for(ii i = n; i >= 1; i--) {
            if(a[i][j] == 0) {
                act.fr = -1;
                continue;
            }

            if(act.fr == -1) {
                act = mp(i,j);
            }

            wall[i][j].pb(act);
        }
    }

    msbfs();
    shortp(S.fr,S.sc);

    // for(ii i = 0; i <= n+1; i++) {
    //     for(ii j = 0; j <= m+1; j++) {
    //         cout << dp[i][j] << " ";
    //     }cout << endl;
    // }

    cout << d[C.fr][C.sc] << endl;

}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);

    // freopen("in.in", "r", stdin);
    //freopen("out.out", "w", stdout);

    ii tt = 1;
    // cin >> tt;
    while(tt--) solve();

}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 28728 KB Output is correct
2 Correct 16 ms 28856 KB Output is correct
3 Correct 15 ms 28876 KB Output is correct
4 Correct 15 ms 28720 KB Output is correct
5 Correct 16 ms 28876 KB Output is correct
6 Correct 15 ms 28868 KB Output is correct
7 Correct 18 ms 28876 KB Output is correct
8 Correct 20 ms 28876 KB Output is correct
9 Correct 17 ms 28748 KB Output is correct
10 Correct 18 ms 28736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 28732 KB Output is correct
2 Correct 16 ms 28748 KB Output is correct
3 Correct 20 ms 28744 KB Output is correct
4 Correct 15 ms 28736 KB Output is correct
5 Correct 16 ms 28876 KB Output is correct
6 Correct 15 ms 28876 KB Output is correct
7 Correct 14 ms 28796 KB Output is correct
8 Correct 15 ms 28876 KB Output is correct
9 Correct 17 ms 29508 KB Output is correct
10 Correct 16 ms 29460 KB Output is correct
11 Correct 17 ms 29404 KB Output is correct
12 Correct 17 ms 29504 KB Output is correct
13 Correct 18 ms 29376 KB Output is correct
14 Correct 16 ms 28736 KB Output is correct
15 Correct 21 ms 29372 KB Output is correct
16 Correct 17 ms 28684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 28732 KB Output is correct
2 Correct 15 ms 28876 KB Output is correct
3 Correct 15 ms 28876 KB Output is correct
4 Correct 16 ms 28864 KB Output is correct
5 Correct 26 ms 32640 KB Output is correct
6 Correct 26 ms 32716 KB Output is correct
7 Correct 29 ms 32844 KB Output is correct
8 Correct 31 ms 32832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 28740 KB Output is correct
2 Correct 15 ms 28812 KB Output is correct
3 Correct 15 ms 28876 KB Output is correct
4 Correct 15 ms 28740 KB Output is correct
5 Correct 15 ms 28816 KB Output is correct
6 Correct 15 ms 28784 KB Output is correct
7 Correct 15 ms 28868 KB Output is correct
8 Correct 15 ms 28816 KB Output is correct
9 Correct 16 ms 29508 KB Output is correct
10 Correct 16 ms 29544 KB Output is correct
11 Correct 16 ms 29516 KB Output is correct
12 Correct 20 ms 29396 KB Output is correct
13 Correct 19 ms 29464 KB Output is correct
14 Correct 35 ms 32696 KB Output is correct
15 Correct 34 ms 32712 KB Output is correct
16 Correct 30 ms 32832 KB Output is correct
17 Correct 27 ms 32716 KB Output is correct
18 Correct 30 ms 32984 KB Output is correct
19 Correct 31 ms 33232 KB Output is correct
20 Correct 30 ms 33312 KB Output is correct
21 Correct 26 ms 32716 KB Output is correct
22 Correct 28 ms 32716 KB Output is correct
23 Correct 34 ms 32856 KB Output is correct
24 Correct 29 ms 33264 KB Output is correct
25 Correct 16 ms 28736 KB Output is correct
26 Correct 17 ms 29440 KB Output is correct
27 Correct 16 ms 28748 KB Output is correct
28 Correct 23 ms 32836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 28752 KB Output is correct
2 Correct 16 ms 28864 KB Output is correct
3 Correct 19 ms 28864 KB Output is correct
4 Correct 16 ms 28800 KB Output is correct
5 Correct 15 ms 28784 KB Output is correct
6 Correct 18 ms 28876 KB Output is correct
7 Correct 18 ms 28876 KB Output is correct
8 Correct 15 ms 28876 KB Output is correct
9 Correct 16 ms 29516 KB Output is correct
10 Correct 16 ms 29500 KB Output is correct
11 Correct 16 ms 29492 KB Output is correct
12 Correct 16 ms 29388 KB Output is correct
13 Correct 19 ms 29388 KB Output is correct
14 Correct 26 ms 32644 KB Output is correct
15 Correct 35 ms 32712 KB Output is correct
16 Correct 32 ms 32844 KB Output is correct
17 Correct 27 ms 32716 KB Output is correct
18 Correct 30 ms 33068 KB Output is correct
19 Correct 37 ms 33444 KB Output is correct
20 Correct 31 ms 33360 KB Output is correct
21 Correct 26 ms 32656 KB Output is correct
22 Correct 27 ms 32716 KB Output is correct
23 Correct 35 ms 32696 KB Output is correct
24 Correct 432 ms 78880 KB Output is correct
25 Correct 667 ms 92668 KB Output is correct
26 Correct 644 ms 92728 KB Output is correct
27 Correct 606 ms 92612 KB Output is correct
28 Correct 368 ms 74724 KB Output is correct
29 Correct 431 ms 76352 KB Output is correct
30 Correct 460 ms 76944 KB Output is correct
31 Correct 31 ms 33228 KB Output is correct
32 Correct 578 ms 92412 KB Output is correct
33 Correct 17 ms 28748 KB Output is correct
34 Correct 18 ms 29376 KB Output is correct
35 Correct 437 ms 81660 KB Output is correct
36 Correct 16 ms 28748 KB Output is correct
37 Correct 24 ms 32848 KB Output is correct
38 Correct 380 ms 78996 KB Output is correct
39 Correct 279 ms 70740 KB Output is correct