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], 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;
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);
}
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... |