Submission #838781

#TimeUsernameProblemLanguageResultExecution timeMemory
838781nononoJakarta Skyscrapers (APIO15_skyscraper)C++14
100 / 100
734 ms28228 KiB
#include <bits/stdc++.h>
using namespace std;

const int INF = 1e8;
const int mxN = 30005;

int n, m;
vector<vector<int>> adj(mxN);
int b[mxN];
int f[mxN][200];
int vis[mxN];

signed main() {
#define taskname ""

    if(fopen(taskname".inp", "r")) {
        freopen(taskname".inp", "r", stdin);
        freopen(taskname".out", "w", stdout);
    }

    cin.tie(0)->sync_with_stdio(0);
    
    cin >> n >> m;
    int N = sqrt(n);
    
    for(int i = 0; i < m; i ++) {
        int x, y;
        cin >> x >> y;
        
        adj[x].push_back(y);
        b[i] = x;
    }
    
    for(int i = 0; i < n; i ++) {
        for(int j = 0; j <= N; j ++) {
            f[i][j] = INF;
        }
    }
    
    priority_queue<array<int, 3>, vector<array<int, 3>>, greater<array<int, 3>>> pq;
    
    f[b[0]][0] = 0;
    pq.push({0, b[0], 0});
    
    while(!pq.empty()) {
        int u = pq.top()[1];
        int d = pq.top()[0];
        int k = pq.top()[2];
        pq.pop();
        
        if(d > f[u][k]) continue ;
        
        if(u == b[1]) {
            cout << d << "\n";
            return 0;
        }
        
        if(k) {
            for(int i : {-1, 1}) {
                int v = u + k * i;
                if(v >= 0 && v < n && f[v][k] > d + 1) {
                    f[v][k] = d + 1;
                    pq.push({f[v][k], v, k});
                }
            }
        }
        
        if(vis[u]) continue ;
        vis[u] = 1;
        
        for(int p : adj[u]) {
            if(p <= N) {
                for(int i : {-1, 1}) {
                    int v = u + p * i;
                    if(v >= 0 && v < n && f[v][p] > d + 1) {
                        f[v][p] = d + 1;
                        pq.push({f[v][p], v, p});
                    }
                    
                    if(v >= 0 && v < n && f[v][0] > d + 1) {
                        f[v][0] = d + 1;
                        pq.push({f[v][0], v, 0});
                    }
                }
            } else {
                for(int i = 1; u + i * p < n; i ++) {
                    int right = u + i * p;
                    if(f[right][0] > d + i) {
                        f[right][0] = d + i;
                        pq.push({d + i, right, 0});
                    }
                }        
                
                for(int i = 1; u - i * p >= 0; i ++) {
                    int left = u - i * p;
                    if(f[left][0] > d + i) {
                        f[left][0] = d + i;
                        pq.push({d + i, left, 0});
                    }
                }
            }
        }
    }
        
    cout << -1 << "\n";
    return 0;
}

Compilation message (stderr)

skyscraper.cpp: In function 'int main()':
skyscraper.cpp:17:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |         freopen(taskname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:18:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |         freopen(taskname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...