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 <iostream>
#include <vector>
#include <queue>
using namespace std;
typedef vector<int> vi;
typedef vector<char> vc;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
#define sep ' '
#define F first
#define S second
#define fastIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define pq priority_queue
const int N = 3e4+1;
const int T = 175;
int n, m;
int dis[N];
int b[N], p[N];
vector<int> D[N];
bool X[N][T];
void solve() {
// input in 0-base itself
cin >> n >> m;
for(int i = 0 ; i < m ; i ++) {
cin >> b[i] >> p[i];
if (p[i] < T) X[b[i]][p[i]] = true;
D[b[i]].pb(p[i]);
}
fill(dis, dis+n, n*n+1);
dis[b[0]] = 0;
priority_queue<pii, vector<pii>, greater<pii>> q;
q.push({0, b[0]});
while(!q.empty()) {
int u = q.top().S;
q.pop();
int i = u;
for(int d: D[u]) {
for(int neigh = i+d, w = 1 ; neigh < n ; neigh += d, w ++) {
if (dis[neigh] > dis[u] + w) {
dis[neigh] = dis[u] + w;
q.push({dis[neigh], neigh});
}
if (d < T && X[neigh][d]) break;
}
for(int neigh = i-d, w = 1 ; neigh >= 0 ; neigh -= d, w ++) {
if (dis[neigh] > dis[u] + w) {
dis[neigh] = dis[u] + w;
q.push({dis[neigh], neigh});
}
if (d < T && X[neigh][d]) break;
}
}
}
cout << (dis[b[1]] == n*n+1 ? -1 : dis[b[1]]) << endl;
}
// check MAXN
int main() {
fastIO;
solve();
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... |