제출 #602952

#제출 시각아이디문제언어결과실행 시간메모리
602952BT21tataJakarta Skyscrapers (APIO15_skyscraper)C++17
0 / 100
1 ms468 KiB
#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// #pragma GCC target ("avx,avx2,fma")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define SPEED ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0)
#define rall(v) (v).rbegin(),(v).rend()
#define all(v) (v).begin(),(v).end()
#define setp fixed<<setprecision
#define OK cerr<<"OK"<<endl<<flush
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define F first
#define S second
#define y0 jahdakdh
#define y1 jahsadakdakdh
#define endl '\n'
const ll MOD=1e9+7;
const ll mod=(1ll<<31)-1;
const ld eps=1e-8;
using namespace std;
mt19937 rng(std::chrono::system_clock::now().time_since_epoch().count());

int n, m, x[30005], pos[30005], pw[30005];
queue<pii>q;
bool used[30005][2005];
ll dis[30005];

int main()
{
    SPEED;
    memset(x, -1, sizeof(x));
    cin>>n>>m;
    for(int i=0; i<m; i++)
    {
        int b, p;
        cin>>b>>p;
        pos[i]=b;
        pw[i]=p;
        x[b]=i;
    }
    q.push({0, pos[0]});
    used[0][pos[0]]=1;
    while(!q.empty())
    {
        int s=q.front().F, bd=q.front().S; q.pop();
        if(bd+pw[s]<n and !used[s][bd+pw[s]])
        {
            used[s][bd+pw[s]]=1;
            dis[bd+pw[s]]=dis[bd]+1;
            q.push({s, bd+pw[s]});
        }
        if(bd+pw[s]<n and x[bd+pw[s]]!=-1 and !used[x[bd+pw[s]]][bd+pw[s]])
        {
            used[x[bd+pw[s]]][bd+pw[s]]=1;
            dis[bd+pw[s]]=dis[bd]+1;
            q.push({x[bd+pw[s]], bd+pw[s]});
        }
        if(bd-pw[s]>=0 and !used[s][bd-pw[s]])
        {
            used[s][bd-pw[s]]=1;
            dis[bd-pw[s]]=dis[bd]+1;
            q.push({s, bd-pw[s]});
        }
        if(bd-pw[s]>=0 and x[bd-pw[s]]!=-1 and !used[x[bd-pw[s]]][bd-pw[s]])
        {
            used[x[bd-pw[s]]][bd-pw[s]]=1;
            dis[bd-pw[s]]=dis[bd]+1;
            q.push({x[bd-pw[s]], bd-pw[s]});
        }
    }
    if(dis[pos[1]]) cout<<dis[pos[1]];
    else cout<<-1;
    return 0;
}

/*

5 3 
0 2 
1 1 
4 1
*/
#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...