이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fi first
#define si second
#define int long long
using namespace std;
const int N = 5e4 + 4;
int dist[N],n,m;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
vector<int> g[N];
pair<int,int> a[N];
signed main()
{
cin >> n >> m;
for(int i = 1; i <= m; i++)
{
cin >> a[i].fi >> a[i].si;
a[i].fi++;
g[a[i].fi].push_back(a[i].si);
}
for(int i = 1; i <= n; i++) dist[i] = 1e18;
q.push({0,a[1].fi});
dist[a[1].first] = 0;
while(!q.empty())
{
pair<int,int> node = q.top();q.pop();
int u = node.si, d = node.fi;
//if(d >= 200) {continue;}
if(dist[u] < d) continue;
for(int p : g[u])
{ int t = d;
for(int i = u + p; i <= n; i += p)
{ t++;
if(dist[i] > t)
{
dist[i] = t;
q.push({dist[i],i});
}
}
t = d;
for(int i = u - p; i > 0; i -= p)
{
t++;
if(dist[i] > t)
{
dist[i] = t;
q.push({dist[i],i});
}
}
}
}
if(dist[a[2].first] == 1e18) cout << -1;
else cout << dist[a[2].first];
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... |