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 <vector>
#include <set>
using namespace std;
int main () {
int n, m;
cin >> n >> m;
vector< pair<int, int> > adj[n];
int bi, pi;
for (int i=0; i<m; i++) {
cin >> bi >> pi;
for (int j=1; j<n; j++) {
if (bi-j*pi >= 0) adj[bi].push_back(make_pair(bi-j*pi, j));
if (bi+j*pi < n) adj[bi].push_back(make_pair(bi+j*pi, j));
}
}
set< pair<int, int> > ord;
int dist[n];
bool vis[n];
for (int i=0; i<n; i++) {
dist[i] = 1e9;
vis[i] = false;
}
dist[0] = 0;
ord.insert(make_pair(0, 0));
while (ord.size() > 0) {
pair<int, int> cur = *ord.begin();
ord.erase(ord.begin());
vis[cur.second] = true;
for (auto i: adj[cur.second]) {
if (!vis[i.first] && cur.first+i.second < dist[i.first]) {
ord.erase(make_pair(dist[i.first], i.first));
dist[i.first] = cur.first+i.second;
ord.insert(make_pair(dist[i.first], i.first));
}
}
}
if (dist[1] == 1e9) {
cout << -1 << endl;
} else {
cout << dist[1] << endl;
}
}
# | 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... |