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 <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int N = 3e4 + 5;
const int oo = 1e9;
const int SQRT = 175;
#define eb emplace_back
struct node {
int w, u, s;
node() {
w = u = s = 0;
}
node(int w, int u, int s) : w(w), u(u), s(s) {}
friend bool operator > (node a, node b) {
return a.w > b.w;
}
};
struct _data {
int to, w, cur;
_data() {
to = w = cur = 0;
}
_data(int to, int w, int cur) : to(to), w(w), cur(cur) {}
};
int n, m;
int dist[N];
pii a[N];
vector<_data> adj[N];
priority_queue<node, vector<node>, greater<node>> Q;
void update(int u, int v, int w, int cur) {
if(v > n)
return;
if(v < 0)
return;
if(dist[v] > dist[u] + w) {
dist[v] = dist[u] + w;
Q.emplace(dist[v], v, cur);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
for(int j = 1; j < N; j++)
adj[u].eb(j * v, j, v);
a[i] = {u, v};
}
for(int i = 0; i < n; i++)
dist[i] = oo;
dist[a[0].first] = 0;
Q.emplace(0, a[0].first, a[0].second);
while(!Q.empty()) {
node top = Q.top();
Q.pop();
int u = top.u;
if(top.w != dist[u])
continue;
for(auto [v, w, cur] : adj[u]) {
update(u, u + v, w, cur);
update(u, u - v, w, cur);
}
}
cout << (dist[a[1].first] == oo ? -1 : dist[a[1].first]);
}
/*
5 3
0 2
1 1
4 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... |