Submission #1248355

#TimeUsernameProblemLanguageResultExecution timeMemory
1248355DedibeatJakarta Skyscrapers (APIO15_skyscraper)C++20
0 / 100
2 ms2376 KiB
#include<bits/stdc++.h>
using namespace std;
#define F first 
#define S second 
#define all(x) (x).begin(), (x).end()
template<typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p)
{
    return os << "(" << p.F << "," << p.S << ")";
}
template<typename T> 
void print(const T &v, int lim = 1e9)
{
    for(auto x : v)
        if(lim-- > 0) cout << x << " ";
    cout << endl;
}
template<typename T>
void print(T *begin, const T *end)
{
    for(T *it = begin; it < end; it++)
        cout << *it << " ";
    cout << endl;
}

vector<array<int, 2>> adj[30000];
set<int> st[30000];

int32_t main()
{
    ios::sync_with_stdio(0); cin.tie(NULL); 
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < m; i++)
    {
        int pos, d;
        cin >> pos >> d;
        if(pos >= n || d == 0)
        {
            for(;;);
        }
 
        for(int i = 1; pos + i * d < n; i++)
        {
            adj[pos].push_back({i, pos + i * d});
        }
        for(int i = 1; pos - i * d >= 0; i++)
        {
            adj[pos].push_back({i, pos - i * d});
        }
    }
    

    vector<int> dist(n, 1e9);
    using nd = pair<int, int>;
    priority_queue<nd, vector<nd>, greater<>> q;

    dist[0] = 0;
    q.push({0, 0});

    while(!q.empty())
    {
        auto [c, i] = q.top(); q.pop();
        for(auto [w, j] : adj[i])
        {
            if(c + w < dist[j])
            {
                dist[j] = c + w;
                q.push({c + w, j});
            }
        }
    }


    int ans = dist[1];
    if(ans == 1e9) cout << "-1\n";
    else cout << ans << "\n";

}
#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...