Submission #244272

# Submission time Handle Problem Language Result Execution time Memory
244272 2020-07-03T13:06:07 Z Matteo_Verz Awesome Arrowland Adventure (eJOI19_adventure) C++11
100 / 100
97 ms 21112 KB
/*
                `-/oo+/-   ``
              .oyhhhhhhyo.`od
             +hhhhyyoooos. h/
            +hhyso++oosy- /s
           .yoooossyyo:``-y`
            ..----.` ``.-/+:.`
                   `````..-::/.
                  `..```.-::///`
                 `-.....--::::/:
                `.......--::////:
               `...`....---:::://:
             `......``..--:::::///:`
            `---.......--:::::////+/`
            ----------::::::/::///++:
            ----:---:::::///////////:`
            .----::::::////////////:-`
            `----::::::::::/::::::::-
             `.-----:::::::::::::::-
               ...----:::::::::/:-`
                 `.---::/+osss+:`
                   ``.:://///-.
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cmath>

using namespace std;

const int INF = 2e9;
const int N = 500;

int n, m;

vector <pair <int, int>> g[5 + N * N];
bool viz[5 + N * N];
int dp[5 + N * N];

class Min_Heap {
    private:
        pair <int, int> h[5 + N * N];
        int sz;

        void Sift_Up(int pos) {
            int parent;
            parent = pos >> 1;

            while(h[pos].second < h[parent].second && parent > 0) {
                swap(h[pos], h[parent]);

                pos = parent;
                parent = pos >> 1;
            }
        }

        void Sift_Down(int pos) {
            int child;
            child = pos << 1;

            if(child + 1 <= sz && h[child].second > h[child + 1].second)
                child++;
            while(child <= sz && h[child].second < h[pos].second) {
                swap(h[pos], h[child]);

                pos = child;
                child = pos << 1;

                if(child + 1 <= sz && h[child].second > h[child + 1].second) child++;
            }
        }

    public:
        void Clear() {
            sz = 0;
        }

        void Push(pair <int, int> val) {
            h[++sz] = val;
            Sift_Up(sz);
        }

        void Pop() {
            swap(h[1], h[sz]);
            sz--;
            Sift_Down(1);
        }

        pair <int, int> Top() {
            return h[1];
        }

        int Size() {
            return sz;
        }
};
Min_Heap heap;

void Dijkstra(int nodStart) {
    for(int i = 1; i <= n * m; i++) dp[i] = INF;

    dp[nodStart] = 0;
    heap.Push(make_pair(nodStart, 0));

    while(heap.Size()) {
        pair <int, int> top = heap.Top();
        heap.Pop();

        if(viz[top.first] == false) {
            viz[top.first] = true;

            for(auto to : g[top.first]) {
                if(dp[to.first] > to.second + dp[top.first]) {
                    dp[to.first] = to.second + dp[top.first];
                    heap.Push(make_pair(to.first, dp[to.first]));
                }
            }
        }
    }
}

int main() {
#ifdef BLAT
    freopen("input.in", "r", stdin);
    freopen("output.out", "w", stdout);
#endif // BLAT

    cin >> n >> m;

    for(int i = 1; i <= n; i++) {
        string s;
        cin >> s;

        for(int j = 0; j < s.size(); j++) {
            int idx = (i - 1) * m + j + 1;
            int idxn, idxe, idxs, idxw;
            idxn = idx - m;
            idxs = idx + m;
            idxe = idx + 1;
            idxw = idx - 1;

            if(s[j] == 'N') {
                if(i > 1) g[idx].push_back(make_pair(idxn, 0));
                if(j < m - 1) g[idx].push_back(make_pair(idxe, 1));
                if(i < n) g[idx].push_back(make_pair(idxs, 2));
                if(j > 0) g[idx].push_back(make_pair(idxw, 3));
            } else if(s[j] == 'E') {
                if(i > 1) g[idx].push_back(make_pair(idxn, 3));
                if(j < m - 1) g[idx].push_back(make_pair(idxe, 0));
                if(i < n) g[idx].push_back(make_pair(idxs, 1));
                if(j > 0) g[idx].push_back(make_pair(idxw, 2));
            } else if(s[j] == 'S') {
                if(i > 1) g[idx].push_back(make_pair(idxn, 2));
                if(j < m - 1) g[idx].push_back(make_pair(idxe, 3));
                if(i < n) g[idx].push_back(make_pair(idxs, 0));
                if(j > 0) g[idx].push_back(make_pair(idxw, 1));
            } else if(s[j] == 'W') {
                if(i > 1) g[idx].push_back(make_pair(idxn, 1));
                if(j < m - 1) g[idx].push_back(make_pair(idxe, 2));
                if(i < n) g[idx].push_back(make_pair(idxs, 3));
                if(j > 0) g[idx].push_back(make_pair(idxw, 0));
            }
        }
    }

    Dijkstra(1);

    if(dp[n * m] == INF) cout << -1 << '\n';
    else cout << dp[n * m] << '\n';
    return 0;
}

Compilation message

adventure.cpp: In function 'int main()':
adventure.cpp:141:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j = 0; j < s.size(); j++) {
                        ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 8192 KB Output is correct
2 Correct 9 ms 8192 KB Output is correct
3 Correct 9 ms 8192 KB Output is correct
4 Correct 9 ms 8192 KB Output is correct
5 Correct 9 ms 8192 KB Output is correct
6 Correct 9 ms 8192 KB Output is correct
7 Correct 9 ms 8192 KB Output is correct
8 Correct 9 ms 8192 KB Output is correct
9 Correct 9 ms 8192 KB Output is correct
10 Correct 9 ms 8192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 8192 KB Output is correct
2 Correct 9 ms 8192 KB Output is correct
3 Correct 9 ms 8192 KB Output is correct
4 Correct 9 ms 8192 KB Output is correct
5 Correct 9 ms 8192 KB Output is correct
6 Correct 9 ms 8192 KB Output is correct
7 Correct 9 ms 8192 KB Output is correct
8 Correct 9 ms 8192 KB Output is correct
9 Correct 9 ms 8192 KB Output is correct
10 Correct 9 ms 8192 KB Output is correct
11 Correct 9 ms 8192 KB Output is correct
12 Correct 9 ms 8192 KB Output is correct
13 Correct 10 ms 8192 KB Output is correct
14 Correct 9 ms 8192 KB Output is correct
15 Correct 9 ms 8192 KB Output is correct
16 Correct 9 ms 8192 KB Output is correct
17 Correct 9 ms 8192 KB Output is correct
18 Correct 9 ms 8192 KB Output is correct
19 Correct 9 ms 8192 KB Output is correct
20 Correct 9 ms 8192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 8192 KB Output is correct
2 Correct 9 ms 8192 KB Output is correct
3 Correct 10 ms 8192 KB Output is correct
4 Correct 9 ms 8192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 8192 KB Output is correct
2 Correct 9 ms 8092 KB Output is correct
3 Correct 10 ms 8192 KB Output is correct
4 Correct 9 ms 8192 KB Output is correct
5 Correct 9 ms 8192 KB Output is correct
6 Correct 9 ms 8192 KB Output is correct
7 Correct 9 ms 8192 KB Output is correct
8 Correct 9 ms 8192 KB Output is correct
9 Correct 9 ms 8192 KB Output is correct
10 Correct 9 ms 8192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 8192 KB Output is correct
2 Correct 9 ms 8192 KB Output is correct
3 Correct 9 ms 8192 KB Output is correct
4 Correct 9 ms 8192 KB Output is correct
5 Correct 9 ms 8192 KB Output is correct
6 Correct 9 ms 8192 KB Output is correct
7 Correct 9 ms 8192 KB Output is correct
8 Correct 9 ms 8192 KB Output is correct
9 Correct 9 ms 8192 KB Output is correct
10 Correct 9 ms 8192 KB Output is correct
11 Correct 9 ms 8192 KB Output is correct
12 Correct 9 ms 8192 KB Output is correct
13 Correct 10 ms 8192 KB Output is correct
14 Correct 9 ms 8192 KB Output is correct
15 Correct 9 ms 8192 KB Output is correct
16 Correct 9 ms 8192 KB Output is correct
17 Correct 9 ms 8192 KB Output is correct
18 Correct 9 ms 8192 KB Output is correct
19 Correct 9 ms 8192 KB Output is correct
20 Correct 9 ms 8192 KB Output is correct
21 Correct 9 ms 8192 KB Output is correct
22 Correct 9 ms 8192 KB Output is correct
23 Correct 10 ms 8192 KB Output is correct
24 Correct 9 ms 8192 KB Output is correct
25 Correct 11 ms 8192 KB Output is correct
26 Correct 9 ms 8092 KB Output is correct
27 Correct 10 ms 8192 KB Output is correct
28 Correct 9 ms 8192 KB Output is correct
29 Correct 9 ms 8192 KB Output is correct
30 Correct 9 ms 8192 KB Output is correct
31 Correct 9 ms 8192 KB Output is correct
32 Correct 9 ms 8192 KB Output is correct
33 Correct 9 ms 8192 KB Output is correct
34 Correct 9 ms 8192 KB Output is correct
35 Correct 15 ms 9088 KB Output is correct
36 Correct 9 ms 8192 KB Output is correct
37 Correct 17 ms 9376 KB Output is correct
38 Correct 17 ms 9856 KB Output is correct
39 Correct 85 ms 18808 KB Output is correct
40 Correct 86 ms 18808 KB Output is correct
41 Correct 57 ms 18616 KB Output is correct
42 Correct 88 ms 18808 KB Output is correct
43 Correct 97 ms 21112 KB Output is correct
44 Correct 56 ms 18552 KB Output is correct