This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define watch(x) cout<<(#x)<<"="<<(x)<<'\n'
#define mset(d,val) memset(d,val,sizeof(d))
#define setp(x) cout<<fixed<<setprecision(x)
#define forn(i,a,b) for(int i=(a);i<(b);i++)
#define fore(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
#define F first
#define S second
#define pqueue priority_queue
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
void amin(ll &a, ll b){ a=min(a,b); }
void amax(ll &a, ll b){ a=max(a,b); }
void YES(){cout<<"YES\n";} void NO(){cout<<"NO\n";}
void SD(int t=0){ cout<<"PASSED "<<t<<endl; }
const ll INF = ll(1e18);
const int MOD = 998244353;
const bool DEBUG = 0;
const int MAXN = 2005;
const int MAXP = 2005;
const int MAXM = 30005;
int n,m;
int a[MAXM],p[MAXM];
vi v[MAXN];
ll dist[MAXN][MAXP];
deque<ii> q;
void add(ll x, ll y, ll w, ll d)
{
if(x<0 || x>=n) return;
if(dist[x][y]>d+w){
dist[x][y]=d+w;
if(w==0) q.push_front({x,y});
else q.push_back({x,y});
}
}
void bfs(ii src)
{
q.clear();
dist[src.F][src.S]=0;
q.push_front(src);
while(!q.empty())
{
ii u=q.front(); q.pop_front();
if(u.S)
{
add(u.F-u.S, u.S, 1, dist[u.F][u.S]);
add(u.F+u.S, u.S, 1, dist[u.F][u.S]);
add(u.F, 0, 0, dist[u.F][u.S]);
}
else
{
for(int P: v[u.F])
{
add(u.F, P, 0, dist[u.F][u.S]);
}
}
}
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>m;
forn(i,0,m){
cin>>a[i]>>p[i]; a[i]--;
v[a[i]].pb(p[i]);
}
forn(i,0,n) forn(j,0,MAXP) dist[i][j]=INF;
bfs({a[0],p[0]});
if(1) cout<<dist[a[1]][0]<<'\n';
else cout<<-1<<'\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |