이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
typedef long long ll;
using namespace std;
const int inf = 1e9 + 5;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<pair<int, int> > > g(n);
vector<set<int> > my(n);
vector<int> b(m), p(m);
for (int i = 0; i < m; i++)
{
cin >> b[i] >> p[i];
my[b[i]].insert(p[i]);
}
for (int i = 0; i < m; i++)
{
for (int j = b[i] + p[i]; j < n; j += p[i])
{
g[b[i]].push_back({ j, (j - b[i]) / p[i] });
if (my[j].count(p[i])) break;
}
for (int j = b[i] - p[i]; j >= 0; j -= p[i])
{
g[b[i]].push_back({ j, (b[i] - j) / p[i] });
if (my[j].count(p[i])) break;
}
}
//cout << "\n";
//for (int i = 0; i < n; i++) for (pair<int, int> j : g[i]) cout << i << " " << j.first << " " << j.second << "\n";
priority_queue<pair<int, int> > pq;
pq.push({ 0, b[0] });
vector<int> dist(n, inf);
while (!pq.empty())
{
int u = pq.top().second, d = -pq.top().first;
//cout << u << endl;
pq.pop();
if (dist[u] != inf) continue;
dist[u] = d;
for (pair<int, int> i : g[u]) if (dist[i.first] == inf)
{
pq.push({ -(d + i.second), i.first });
}
}
cout << (dist[b[1]] == inf ? -1 : dist[b[1]]) << "\n";
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... |