#include <bits/stdc++.h>
using namespace std;
int main() {
int n,m;
cin>>n>>m;
vector<vector<int>>dist(m,vector<int>(n,INT_MAX));
for(int i=0;i<m;i++){
int b,p;
cin>>b>>p;
dist[i][b]=0;
if(i==1){
continue;
}
int j=b-p;
while(j>=0){
dist[i][j]=dist[i][j+p]+1;
j-=p;
}
j=b+p;
while(j<n){
dist[i][j]=dist[i][j-p]+1;
j+=p;
}
for(int j=0;j<n;j++){
cerr<<dist[i][j]<<" ";
}
cerr<<"\n";
}
vector<vector<pair<int,int>>>graph(m,vector<pair<int,int>>());
for(int i=0;i<m;i++){
for(int j=i+1;j<m;j++){
for(int k=0;k<n;k++){
if(dist[i][k]!=INT_MAX&&dist[j][k]!=INT_MAX){
graph[i].push_back({j,dist[i][k]+dist[j][k]});
graph[j].push_back({i,dist[i][k]+dist[j][k]});
}
}
}
}
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;
vector<int>distancias(m,INT_MAX);
distancias[0]=0;
pq.push({0,0});
while(!pq.empty()){
auto[peso,nodo]=pq.top();
pq.pop();
for(auto[vecino,w]:graph[nodo]){
if((peso+w)<distancias[vecino]){
distancias[vecino]=peso+w;
pq.push({distancias[vecino],vecino});
}
}
}
cout<<((distancias[1]!=INT_MAX)?distancias[1]:-1)<<"\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
440 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |