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;
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fs first
#define sc second
#define pb push_back
void solve() {
int n,m;
cin>>n>>m;
pii a[m];
vector<pii> adj[n*m+n];
for(int i=0;i<m;i++){
int b,p;
cin>>b>>p;
a[i]={b,p};
for(int x=b%p;x<n;x++){
adj[n*i+x].pb({n*m+x,0});
}
adj[n*m+b].pb({n*i+b,0});
for(int x=b%p;x+p<n;x++){
adj[n*i+x].pb({n*i+x+p,1});
adj[n*i+x+p].pb({n*i+x,1});
}
}
int dist[n*m+n];
bool vis[n*m+n];
for(int i=0;i<n*m+n;i++){
dist[i]=1e9;
vis[i]=0;
}
dist[a[0].fs]=0;
priority_queue<pii> pq;
pq.push({0,a[0].fs});
while(!pq.empty()){
int v=pq.top().sc;
pq.pop();
if(vis[v])continue;
vis[v]=1;
for(auto[u,w]:adj[v])if(!vis[u]){
dist[u]=min(dist[u],dist[v]+w);
pq.push({-dist[u],u});
}
}
int ans=dist[n+a[1].fs];
if(ans==1e9)ans=-1;
cout<<ans;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
solve();
}
# | 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... |