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>
#pragma GCC optimize ("O2")
#pragma GCC optimize ("unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;
const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod=1000000007;
const int SQ=180, MAXN=180*30000;
int n, m, k, u, v, x, y, t, a, b, ans;
int B[MAXN], P[MAXN];
int dist[MAXN];
bool mark[MAXN];
vector<pii> G[MAXN];
//priority_queue<pii, vector<pii>, greater<pii>> pq;
queue<int> pq;
inline void addedge(int v1, int j1, int v2, int j2, int w){
G[v1*SQ+j1].pb({v2*SQ+j2, w});
}
inline void upd(int v, int d){
if (dist[v]>d){
dist[v]=d;
if (!mark[v]) pq.push(v);
mark[v]=1;
}
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
memset(dist, 63, sizeof(dist));
cin>>n>>m;
for (int i=0; i<m; i++){
cin>>B[i]>>P[i];
if (P[i]<SQ) G[B[i]*SQ].pb({B[i]*SQ+P[i], 0});
else{
for (int j=1; B[i]-j*P[i]>=0; j++) addedge(B[i], 0, B[i]-P[i]*j, 0, j);
for (int j=1; B[i]+j*P[i]<n; j++) addedge(B[i], 0, B[i]+P[i]*j, 0, j);
}
}
upd(B[0]*SQ, 0);
while (pq.size()){
int v=pq.front();//.second;
pq.pop();
mark[v]=0;
for (pii p:G[v]) upd(p.first, dist[v]+p.second);
int i=v/SQ, j=v%SQ;
if (i>=j) upd((i-j)*SQ+j, dist[v]+1);
if (i+j<n) upd((i+j)*SQ+j, dist[v]+1);
upd(i*SQ, dist[v]+0);
}
ans=dist[SQ*B[1]];
if (ans>=inf) ans=-1;
cout<<ans<<'\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... |