이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// author : Murot_06
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ff first
#define ll long long
#define ss second
using namespace std;
const int N=1e6+7, M=1e9+7;
pair <ll,ll> a[N];
vector <pair <ll,ll>> g[N];
set <ll> v[N];
void solve()
{
int n, m;
cin >> n >> m;
for (int i=1;i<=m;i++){
ll x, y;
cin >> x >> y;
a[i].ff=x, a[i].ss=y;
v[x].insert(y);
}
for (int i=0;i<n;i++){
for (auto l:v[i]){
for (int j=i+l;j<n;j+=l){
g[i].push_back({j, (j-i)/l});
if (v[j].count(l)) break;
}
for (int j=i-l;j>=0;j-=l){
g[i].push_back({j, (i-j)/l});
if (v[j].count(l)) break;
}
}
}
// priority_queue<int, vector<int>, greater<int> >
priority_queue <ll, vector <ll> , greater <ll>> q;
vector <ll> d(n+10, 1e18);
d[a[1].ff]=0;
q.push(a[1].ff);
while (!q.empty()){
ll x=q.top();
q.pop();
for (auto l:g[x]){
if (d[l.ff] > d[x]+l.ss){
d[l.ff]=d[x]+l.ss;
q.push(l.ff);
}
}
}
ll ans=d[a[2].ff];
if (ans == 1e18) ans=-1;
cout << ans;
return ;
}
int main()
{
ios;
int t=1;
// cin >> t;
while (t--){
solve();
}
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... |