This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vii;
typedef vector<vii> vvii;
int main(){
int l,n;
cin >> l >> n;
vii v(n);
for (int i=0;i<n;i++) cin >> v[i].first >> v[i].second;
int inicio=v[0].first,final=v[1].first;
vector<set<int>> pos(l);
for (int i=0;i<n;i++) pos[v[i].first].insert(v[i].second);
vvii conexiones(l);
for (int i=0;i<l;i++){
for (int p:pos[i]){
int a=i-p;
while (a>=0){
if (pos[a].size()) conexiones[i].push_back(pii{(i-a)/p,a});
if (pos[a].count(p)) break;
a-=p;
}
a=i+p;
while (a<l){
if (pos[a].size()) conexiones[i].push_back(pii{(a-i)/p,a});
if (pos[a].count(p)) break;
a+=p;
}
}
}
priority_queue<pii> cola;
cola.push(pii{0,inicio});
bool T=true;
vi porvisitar(l,true);
//for (int i=0;i<n;i++){
// for (pii x:conexiones[i]) cerr << x.second<<' '<<x.first<<' '<<' ';
// cerr<<'\n';
//}
while (cola.size()){
pii x=cola.top();
cola.pop();
if (porvisitar[x.second]){
porvisitar[x.second]=false;
if (x.second==final){
T=false;
cout << -x.first << '\n';
break;
}
for (pii y:conexiones[x.second]){
if (porvisitar[y.second]) cola.push(pii{x.first-y.first,y.second});
}
}
}
if (T) cout << -1<<'\n';
}
# | 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... |