#include <bits/stdc++.h>
#define int long long
#define ll long long
using namespace std;
const int N = 30005;
vector<pair<ll,ll>>G[N];
vector<ll>dist(N,1e18),par(N,-1);
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<pair<int,int>> a(m);
for(int i = 0; i < m; i++) {
cin >> a[i].first >> a[i].second;
}
for(int i = 0; i < m; i++) {
for(int j = 1; j < 1000; j++) {
if(a[i].first - j * a[i].second >= 0) {
G[a[i].first].push_back({a[i].first - j * a[i].second, j});
}
if(a[i].first + j * a[i].second <= n) {
G[a[i].first].push_back({a[i].first + j * a[i].second, j});
}
}
}
priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>>pq;
pq.push({0,a[0].first});
dist[0] = 0;
vector<bool>vis(n+1,false);
while(pq.size()) {
ll d,u;
tie(d,u) = pq.top();
pq.pop();
if(vis[u]) continue;
vis[u] = true;
for(auto X:G[u]) {
if(dist[X.first] > dist[u] + X.second) {
dist[X.first] = dist[u] + X.second;
pq.push({dist[X.first],X.first});
par[X.first] = u;
}
}
}
if(dist[a[1].first] == 1e18) {
cout << - 1;
}
else {
cout << dist[a[1].first];
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1624 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1624 KB |
Output is correct |
6 |
Correct |
1 ms |
1624 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Incorrect |
1 ms |
1880 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Incorrect |
1 ms |
1628 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1624 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Incorrect |
1 ms |
1628 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1624 KB |
Output is correct |
2 |
Correct |
1 ms |
1624 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1624 KB |
Output is correct |
8 |
Incorrect |
1 ms |
1628 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |