Submission #668364

# Submission time Handle Problem Language Result Execution time Memory
668364 2022-12-03T17:25:40 Z Do_you_copy Portals (BOI14_portals) C++17
100 / 100
510 ms 157132 KB
/*
    I find it wholesome to be alone the greater part of the time.
    To be in company, even with the best, is soon wearisome and dissipating.
    I love to be alone.
    I never found the companion that was so companionable as solitude.
*/
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(0); cin.tie(0);

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
mt19937_64 Rand(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1000 + 10;
//const int Mod = 1e9 + 7;
//const int inf =
int n, m;
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
int far[maxN][maxN][4];
string s[maxN];
int ans[maxN][maxN];

struct TNode{
    int fi, se, w;
    bool operator < (const TNode &other) const{
        return w > other.w;
    }
};

vector <TNode> adj[maxN][maxN];
void Dijkstra(pii u, pii v){
    priority_queue <TNode> PQ;
    PQ.push({u.fi, u.se, 0});
    memset(ans, 0x3f, sizeof(ans));
    ans[u.fi][u.se] = 0;
    while (!PQ.empty()){
        auto r = PQ.top(); PQ.pop();
        if (ans[r.fi][r.se] != r.w) continue;
        for (auto i: adj[r.fi][r.se]){
            if (ans[i.fi][i.se] > ans[r.fi][r.se] + i.w){
                ans[i.fi][i.se] = ans[r.fi][r.se] + i.w;
                PQ.push({i.fi, i.se, ans[i.fi][i.se]});
            }
        }
    }
}

void Solve(int d){
    if (d & 1){
        for (int i = 1; i <= n; ++i){
            for (int j = 1; j <= m; ++j){
                if (s[i][j] == '#'){
                    far[i][j][d] = -1;
                    continue;
                }
                far[i][j][d] = far[i - dx[d]][j - dy[d]][d] + 1;
            }
        }
    }
    else{
        for (int i = n; i >= 1; --i){
            for (int j = m; j >= 1; --j){
                if (s[i][j] == '#'){
                    far[i][j][d] = -1;
                    continue;
                }
                far[i][j][d] = far[i - dx[d]][j - dy[d]][d] + 1;
            }
        }
    }
}

void Init(){
    cin >> n >> m;
    for (int i = 0; i < m + 2; ++i) s[0].pb('#');
    for (int i = 0; i < m + 2; ++i) s[n + 1].pb('#');
    pii u, v;
    for (int i = 1; i <= n; ++i){
        cin >> s[i];
        s[i] = "#" + s[i] + "#";
        for (int j = 1; j <= m; ++j){
            if (s[i][j] == 'S') u = {i, j};
            else if (s[i][j] == 'C') v = {i, j};
        }
    }
    memset(far, -1, sizeof(far));
    for (int i = 0; i < 4; ++i)
        Solve(i);
    for (int i = 1; i <= n; ++i){
        for (int j = 1; j <= m; ++j){
            pii minn = {0x3f3f3f3f, 0};
            for (int d = 0; d < 4; ++d){
                minn = min(minn, {far[i][j][d], d});
                if (s[i + dx[d]][j + dy[d]] != '#'){
                    adj[i][j].pb({i + dx[d], j + dy[d], 1});
                }
            }
            for (int d = 0; d < 4; ++d){
                if (far[i][j][d] == minn.fi) continue;
                int dist = minn.fi + 1;
                assert(dist > 0);
                adj[i][j].pb({i - far[i][j][d] * dx[d], j - far[i][j][d] * dy[d], dist});
            }
        }
    }
    Dijkstra(u, v);
    cout << ans[v.fi][v.se];
}

#define debug
#define taskname "test"
signed main(){
    faster
    if (fopen(taskname".inp", "r")){
        freopen(taskname".inp", "r", stdin);
        freopen(taskname".out", "w", stdout);
    }
    int tt = 1;
    //cin >> tt;
    while (tt--){
        Init();
    }
    if (fopen("timeout.txt", "r")){
        ofstream timeout("timeout.txt");
        timeout << signed(double(clock()) / CLOCKS_PER_SEC * 1000);
        timeout.close();
        #ifndef debug
        cerr << "Time elapsed: " << signed(double(clock()) / CLOCKS_PER_SEC * 1000) << "ms\n";
        #endif // debug
    }
}

Compilation message

portals.cpp: In function 'int main()':
portals.cpp:120:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  120 |         freopen(taskname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
portals.cpp:121:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  121 |         freopen(taskname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 20 ms 44260 KB Output is correct
2 Correct 22 ms 44268 KB Output is correct
3 Correct 20 ms 44164 KB Output is correct
4 Correct 19 ms 44180 KB Output is correct
5 Correct 20 ms 44216 KB Output is correct
6 Correct 21 ms 44284 KB Output is correct
7 Correct 20 ms 44168 KB Output is correct
8 Correct 23 ms 44244 KB Output is correct
9 Correct 20 ms 44168 KB Output is correct
10 Correct 20 ms 44256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 44248 KB Output is correct
2 Correct 20 ms 44244 KB Output is correct
3 Correct 20 ms 44284 KB Output is correct
4 Correct 20 ms 44248 KB Output is correct
5 Correct 21 ms 44232 KB Output is correct
6 Correct 19 ms 44172 KB Output is correct
7 Correct 20 ms 44244 KB Output is correct
8 Correct 20 ms 44172 KB Output is correct
9 Correct 21 ms 44556 KB Output is correct
10 Correct 21 ms 44492 KB Output is correct
11 Correct 21 ms 44316 KB Output is correct
12 Correct 21 ms 44372 KB Output is correct
13 Correct 21 ms 44404 KB Output is correct
14 Correct 19 ms 44196 KB Output is correct
15 Correct 21 ms 44424 KB Output is correct
16 Correct 19 ms 44168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 44244 KB Output is correct
2 Correct 22 ms 44244 KB Output is correct
3 Correct 19 ms 44168 KB Output is correct
4 Correct 20 ms 44156 KB Output is correct
5 Correct 33 ms 46772 KB Output is correct
6 Correct 31 ms 46940 KB Output is correct
7 Correct 32 ms 47240 KB Output is correct
8 Correct 26 ms 46868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 44244 KB Output is correct
2 Correct 21 ms 44164 KB Output is correct
3 Correct 20 ms 44144 KB Output is correct
4 Correct 21 ms 44244 KB Output is correct
5 Correct 20 ms 44244 KB Output is correct
6 Correct 20 ms 44244 KB Output is correct
7 Correct 21 ms 44288 KB Output is correct
8 Correct 22 ms 44244 KB Output is correct
9 Correct 21 ms 44500 KB Output is correct
10 Correct 21 ms 44500 KB Output is correct
11 Correct 21 ms 44432 KB Output is correct
12 Correct 20 ms 44372 KB Output is correct
13 Correct 21 ms 44420 KB Output is correct
14 Correct 31 ms 46720 KB Output is correct
15 Correct 31 ms 46912 KB Output is correct
16 Correct 32 ms 47296 KB Output is correct
17 Correct 30 ms 47212 KB Output is correct
18 Correct 32 ms 47948 KB Output is correct
19 Correct 33 ms 48776 KB Output is correct
20 Correct 32 ms 48796 KB Output is correct
21 Correct 29 ms 46676 KB Output is correct
22 Correct 29 ms 46668 KB Output is correct
23 Correct 30 ms 46932 KB Output is correct
24 Correct 32 ms 48700 KB Output is correct
25 Correct 20 ms 44172 KB Output is correct
26 Correct 21 ms 44484 KB Output is correct
27 Correct 22 ms 44196 KB Output is correct
28 Correct 28 ms 46864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 44244 KB Output is correct
2 Correct 20 ms 44244 KB Output is correct
3 Correct 22 ms 44172 KB Output is correct
4 Correct 23 ms 44264 KB Output is correct
5 Correct 22 ms 44244 KB Output is correct
6 Correct 19 ms 44208 KB Output is correct
7 Correct 20 ms 44252 KB Output is correct
8 Correct 20 ms 44176 KB Output is correct
9 Correct 21 ms 44500 KB Output is correct
10 Correct 22 ms 44544 KB Output is correct
11 Correct 21 ms 44328 KB Output is correct
12 Correct 21 ms 44420 KB Output is correct
13 Correct 21 ms 44424 KB Output is correct
14 Correct 29 ms 46672 KB Output is correct
15 Correct 30 ms 46916 KB Output is correct
16 Correct 31 ms 47316 KB Output is correct
17 Correct 30 ms 47188 KB Output is correct
18 Correct 34 ms 47888 KB Output is correct
19 Correct 35 ms 48772 KB Output is correct
20 Correct 33 ms 48728 KB Output is correct
21 Correct 29 ms 46676 KB Output is correct
22 Correct 30 ms 46756 KB Output is correct
23 Correct 30 ms 46916 KB Output is correct
24 Correct 336 ms 124976 KB Output is correct
25 Correct 510 ms 157132 KB Output is correct
26 Correct 423 ms 156876 KB Output is correct
27 Correct 423 ms 157004 KB Output is correct
28 Correct 286 ms 105224 KB Output is correct
29 Correct 308 ms 106488 KB Output is correct
30 Correct 341 ms 108616 KB Output is correct
31 Correct 35 ms 48660 KB Output is correct
32 Correct 410 ms 156768 KB Output is correct
33 Correct 19 ms 44244 KB Output is correct
34 Correct 22 ms 44372 KB Output is correct
35 Correct 315 ms 125568 KB Output is correct
36 Correct 20 ms 44244 KB Output is correct
37 Correct 27 ms 46820 KB Output is correct
38 Correct 230 ms 109900 KB Output is correct
39 Correct 226 ms 94384 KB Output is correct