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 pair<ll,ii> iii;
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 = 30005;
const int MAXP = 30005;
const int MAXM = 30005;
const int LIM = 173;
int n,m;
int a[MAXM],p[MAXM];
vi v[MAXN];
ll dist[MAXN][LIM];
priority_queue<iii,vector<iii>,greater<iii>> q;
void add(ll x, ll y, ll w, ll d)
{
if(x<0 || x>=n) return;
if(y>=LIM)
{
for(int i=x%y;i<n;i+=y){
add(i, 0, abs(x-i)/y, d);
}
}
else
{
if(dist[x][y]>d+w){
dist[x][y]=d+w;
q.push({dist[x][y],{x,y}});
}
}
}
void dijkstra(ii src)
{
dist[src.F][src.S]=0;
q.push({0,src});
while(!q.empty())
{
if(DEBUG && dist[1][0]<INF) cout<<"trigger 1\n";
ii u=q.top().S; ll td=q.top().F; q.pop();
if(DEBUG) cout<<"u="<<u.F<<","<<u.S<<" dist="<<td<<'\n';
if(u.S)
{
add(u.F-u.S, u.S, 1, dist[u.F][u.S]);if(DEBUG && dist[1][0]<INF) cout<<"trigger 3\n";
add(u.F+u.S, u.S, 1, dist[u.F][u.S]);if(DEBUG && dist[1][0]<INF) cout<<"trigger 4\n";
add(u.F, 0, 0, dist[u.F][u.S]);if(DEBUG && dist[1][0]<INF) cout<<"trigger 5\n";
}
else
{
for(int P: v[u.F])
{
add(u.F, P, 0, dist[u.F][u.S]);if(DEBUG && dist[1][0]<INF) cout<<"trigger 6\n";
}
}
}
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>m;
forn(i,0,m){
cin>>a[i]>>p[i];
v[a[i]].pb(p[i]);
}
forn(i,0,n) forn(j,0,LIM) dist[i][j]=INF;
dijkstra({a[0],0});
if(DEBUG){ forn(i,0,n){ forn(j,0,LIM) cout<<dist[i][j]<<" "; cout<<'\n'; } }
if(dist[a[1]][0]<INF) 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... |