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 <algorithm>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <vector>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <stack>
#include <queue>
#include <assert.h>
#include <limits>
#include <cstdio>
using namespace std;
//#define RDEBUG 1
#ifdef RDEBUG
#define D(x) x
#else
#define D(x)
#endif
#define inf 0x7fffffff
#define MOD 1000000007
int add(int a, int b) {
a += b;
if(a >= MOD) {
a -= MOD;
}
return a;
}
int sub(int a, int b) {
a -= b;
if(a < 0) {
a += MOD;
}
return a;
}
int mul(int a, int b) {
return (a * b)%MOD;
}
void add_self(int& a, int b) {
a = add(a, b);
}
void sub_self(int& a, int b) {
a = sub(a, b);
}
void mul_self(int& a, int b) {
a = mul(a, b);
}
const int MAXN = 30010;
int sp[MAXN][175];
int N, M;
pair<int, int> doge[MAXN];
vector<pair<pair<int, int>, int>> adj[MAXN][2];
vector<int> gadj[MAXN];
void dij(int start) {
for (int i = 0; i<N; i++) {
for (int j = 0; j<=sqrt(N); j++) {
sp[i][j] = 1e9;
}
}
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq;
pq.push({0, {doge[start].first, 0}});
sp[doge[start].first][0] = 0;
while (pq.size() != 0) {
int d = pq.top().first, u = pq.top().second.first, p = pq.top().second.second;
if (u == 1 && p == 0) {
break;
}
pq.pop();
if (sp[u][p] != d) {
continue;
}
if (p != 0) {
if (u-p >= 0) {
adj[u][1].push_back({{u-p, p}, 1});
}
if (u+p < N) {
adj[u][1].push_back({{u+p, p}, 1});
}
adj[u][1].push_back({{u, 0}, 0});
}
for (auto edge : adj[u][p != 0]) {
int v = edge.first.first, np = edge.first.second, w = edge.second;
if (d+w < sp[v][np]) {
sp[v][np] = d+w;
pq.push({d+w, {v, np}});
}
}
if (p == 0) {
for (auto e : gadj[u]) {
for (int j = 1; j<=N; j++) {
int hold = 0;
if (u-e*j >= 0) {
hold++;
pair<pair<int, int>, int> edge = {{u-e*j, 0}, j};
int v = edge.first.first, np = edge.first.second, w = edge.second;
if (d+w < sp[v][np]) {
sp[v][np] = d+w;
pq.push({d+w, {v, np}});
}
}
if (u+e*j < N) {
hold++;
pair<pair<int, int>, int> edge = {{u+e*j, 0}, j};
int v = edge.first.first, np = edge.first.second, w = edge.second;
if (d+w < sp[v][np]) {
sp[v][np] = d+w;
pq.push({d+w, {v, np}});
}
}
if (!hold) {
break;
}
}
}
}
if (p != 0) {
if (u-p >= 0) {
adj[u][1].pop_back();
}
if (u+p < N) {
adj[u][1].pop_back();
}
adj[u][1].push_back({{u, 0}, 0});
}
}
}
int main() {
ios_base :: sync_with_stdio(false);
cin >> N >> M;
for (int i = 0; i<M; i++) {
int a, b;
cin >> a >> b;
doge[i] = {a, b};
if (b<=sqrt(N)) {
adj[a][0].push_back({{a, b}, 0});
} else {
gadj[a].push_back(b);
// for (int j = 1; j<=N; j++) {
// int hold = 0;
// if (a-b*j >= 0) {
// hold++;
// adj[a][0].push_back({{a-b*j, 0}, j});
// }
// if (a+b*j < N) {
// hold++;
// adj[a][0].push_back({{a+b*j, 0}, j});
// }
// if (!hold) {
// break;
// }
// }
}
}
dij(0);
cout << (sp[doge[1].first][0] == 1e9 ? -1 : sp[doge[1].first][0]) << "\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... |