이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/// adil sultanov | vonat1us
#pragma GCC optimize("O3")
//#pragma comment(linker, "/STACK:36777216")
#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define sz(x) (int) x.size()
#define all(z) (z).begin(), (z).end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 1e2;
void fin() {
#ifdef AM
freopen(".in", "r", stdin);
#endif
}
const bool flag = 0;
const int M = 3e4+10;
const int N = 2e3+10;
int d[M], dist[N][N];
vector<pii> adj[M];
void dijkstra(vector<int> b, int n) {
for (int i = 0; i < n; ++i) {
d[i] = INF;
}
d[b[0]] = 0;
priority_queue<pii> q;
q.push({0, b[0]});
while (!q.empty()) {
auto [w, x] = q.top();
q.pop();
if (d[x] != -w) continue;
for (int i = 0; i < n; ++i) {
if (i == x) continue;
if (d[i] > d[x]+dist[x][i]) {
d[i] = d[x]+dist[x][i];
q.push({-d[i], i});
}
}
}
}
void ma1n() {
int n, m;
cin >> n >> m;
vector<int> b(m), p(m);
for (int i = 0; i < m; ++i) {
cin >> b[i] >> p[i];
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i != j) {
dist[i][j] = INF;
}
}
}
for (int i = 0; i < m; ++i) {
for (int j = b[i]; j < n; j += p[i]) {
dist[b[i]][j] = min(dist[b[i]][j], (j-b[i])/p[i]);
}
for (int j = b[i]; j >= 0; j -= p[i]) {
dist[b[i]][j] = min(dist[b[i]][j], (b[i]-j)/p[i]);
}
}
dijkstra(b, n);
cout << (d[b[1]] == INF ? -1 : d[b[1]]);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr), fin();
int ts = 1;
if (flag) {
cin >> ts;
}
while (ts--) {
ma1n();
}
return 0;
}
# | 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... |