#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> 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;
vvii conexiones(n);
for (int i=0;i<n;i++){
pii x=v[i];
for (int j=0;j<n;j++){
pii y=v[j];
if (x==y) continue;
if (y.first%x.second==x.first%x.second) conexiones[i].push_back(pii{abs(x.first-y.first)/x.second,j});
}
}
priority_queue<pii> cola;
cola.push(pii{0,0});
bool T=true;
vi porvisitar(n,true);
while (cola.size()){
pii x=cola.top();
cola.pop();
if (porvisitar[x.second]){
porvisitar[x.second]=false;
if (x.second==1){
T=false;
cout << -x.first << '\n';
break;
}
for (pii y:conexiones[x.second]){
cola.push(pii{x.first-y.first,y.second});
}
}
}
if (T) cout << -1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
2 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |