이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define pb push_back
typedef pair<int, int> pii;
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
const int INF = 1e18;
bool cmin(int &a, const int &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
void run() {
int n, m;
cin >> n >> m;
vector<int> b(m), p(m);
vector<vector<int>> who(n);
for (int i = 0; i < m; ++i) {
cin >> b[i] >> p[i];
who[b[i]].pb(i);
}
vector<vector<pii>> g(n);
for (int u = 0; u < n; ++u) {
for (auto x : who[u]) {
int cnt = 0;
for (int i = u; i < n; i += p[x]) {
g[u].pb({i, cnt});
++cnt;
}
cnt = 0;
for (int i = u; i >= 0; i -= p[x]) {
g[u].pb({i, cnt});
++cnt;
}
}
sort(all(g[u]));
g[u].resize(unique(all(g[u])) - g[u].begin());;
}
vector<int> dist(n, INF);
priority_queue<pii> q;
dist[b[0]] = 0;
q.push({0, b[0]});
while (!q.empty()) {
pii pr = q.top();
q.pop();
if (-pr.F != dist[pr.S])
continue;
int u = pr.S;
for (auto edge : g[u]) {
int v = edge.F;
int w = edge.S;
if (cmin(dist[v], dist[u] + w)) {
q.push({-dist[v], v});
}
}
}
if (dist[b[1]] >= INF)
dist[b[1]] = -1;
cout << dist[b[1]] << '\n';
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
run();
}
# | 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... |