이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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(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 * v <= n or j * v <= u; 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]) {
if(u + v <= n)
update(u, u + v, w, cur);
if(u - v >= 0)
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... |