#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
template<typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p)
{
return os << "(" << p.F << "," << p.S << ")";
}
template<typename T>
void print(const T &v, int lim = 1e9)
{
for(auto x : v)
if(lim-- > 0) cout << x << " ";
cout << endl;
}
template<typename T>
void print(T *begin, const T *end)
{
for(T *it = begin; it < end; it++)
cout << *it << " ";
cout << endl;
}
vector<array<int, 3>> adj[30000][150];
set<int> st[30000];
int32_t main()
{
ios::sync_with_stdio(0); cin.tie(NULL);
int n, m;
cin >> n >> m;
int lim = 1;
for(int i = 0; i < m; i++)
{
int pos, d;
cin >> pos >> d;
if(pos >= n || d == 0)
{
for(;;);
}
if(d < lim) st[pos].insert(d);
else
{
for(int i = 1; pos + i * d < n; i++)
{
adj[pos][lim].push_back({i, pos + i * d, lim});
}
for(int i = 1; pos - i * d >= 0; i++)
{
adj[pos][lim].push_back({i, pos - i * d, lim});
}
}
}
// for(int d = 1; d < lim; d++)
// {
// for(int i = 0; i < n; i++)
// {
// if(i - d >= 0) adj[i][d].push_back({1, i - d, d});
// if(i + d < n) adj[i][d].push_back({1, i + d, d});
// }
// }
// for(int i = 0; i < n; i++)
// {
// int prev = -1;
// for(int d : st[i])
// {
// if(prev != -1)
// {
// adj[i][d].push_back({0, i, prev});
// adj[i][prev].push_back({0, i, d});
// }
// prev = d;
// }
// if(prev != -1)
// {
// adj[i][lim].push_back({0, i, prev});
// }
// for(int d = 1; d < lim; d++)
// adj[i][d].push_back({0, i, lim});
// }
vector<vector<int>> dist(n, vector<int>(lim + 5, 1e9));
using nd = tuple<int, int, int>;
priority_queue<nd, vector<nd>, greater<>> q;
dist[0][lim] = 0;
q.push({0, 0, lim});
while(!q.empty())
{
auto [c, i, d] = q.top(); q.pop();
if(c != dist[i][d]) continue;
//cout << "at " << i << " " << d << " cost " << c << endl;
if(i == 1) break;
for(auto [w, j, d2] : adj[i][d])
{
if(c + w < dist[j][d2])
{
dist[j][d2] = c + w;
q.push({c + w, j, d2});
}
}
}
// for(auto [x, y, z] : adj[2][lim])
// cout << x << ' '<< y << ' ' << z << '\n';
int ans = 1e9;
for(int d = 1; d <= lim; d++)
ans = min(ans, dist[1][d]);
if(ans == 1e9) cout << "-1\n";
else cout << ans << "\n";
}
# | 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... |