This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//I forgot you...
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int MOD = (int)1e9 + 7;
const int MAXN = 3e4 + 3;
const int SQRT = (int)1;
const int infint = (int)1e9 + 3;
const ll inf = (ll)1e18;
int dist[MAXN * SQRT], ldir[MAXN][SQRT], rdir[MAXN][SQRT], n, m;
vector<pair<int, int> > G[SQRT * MAXN];
inline void add(int u, int v, int w)
{
G[u].push_back({v, w});
}
int dijkstra(int src, int sink)
{
for (int i = 0; i < MAXN * SQRT; i++)
dist[i] = infint;
dist[src] = 0;
set<pair<int, int> > S;
for (int i = 0; i < MAXN * SQRT; i++)
S.insert({dist[i], i});
while(!S.empty())
{
pair<int, int> st = *S.begin();
S.erase(st);
if(st.first == infint)
continue;
for (auto v : G[st.second])
if(st.first + v.second < dist[v.first])
{
S.erase({dist[v.first], v.first});
dist[v.first] = st.first + v.second;
S.insert({dist[v.first], v.first});
}
}
if(dist[sink] == infint)
return -1;
else
return dist[sink];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++)
{
int b, p;
cin >> b >> p;
/*if(p < SQRT)
{
ldir[b][p] = 1;
rdir[b][p] = 1;
add(b, b + n * p, 0);
}
else
{*/
for (int j = 1; b - j * p >= 0; j++)
add(b, b - j * p, j);
for (int j = 1; b + j * p < n; j++)
add(b, b + j * p, j);
//}
}
/*for (int i = 1; i < SQRT; i++)
{
for (int j = i; j < n; j++)
rdir[j][i] |= rdir[j - i][i];
for (int j = n - i - 1; j >= 0; j--)
ldir[j][i] |= ldir[j + i][i];
}
for (int i = 1; i < SQRT; i++)
for (int j = 0; j < n - i; j++)
if(rdir[j][i])
add(j + n * i, j + (n + 1) * i, 1);
for (int i = 1; i < SQRT; i++)
for (int j = i; j < n; j++)
if(ldir[j][i])
add(j + n * i, j + (n - 1) * i, 1);
for (int i = 1; i < SQRT; i++)
for (int j = 0; j < n; j++)
add(j + n * i, j, 0);*/
cout << dijkstra(0, 1);
}
# | 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... |