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 int
#define sz(x) (int)x.size()
#define ff first
#define ss second
const ll N = 30002;
int T, n, m, dis[N], c[N];
vector <int> v1, v;
pair<int,int> a[N];
int main(){
ios::sync_with_stdio(false); cin.tie(0);
cin >> n >> m;
for(int i = 0; i < m; i++){
cin >> a[i].ff >> a[i].ss;
}
int ind = a[0].ff, p = a[1].ff;
sort (a,a+m);
c[a[0].ff] = 0;
v.push_back(a[0].ff);
for(int i = 1; i < m; i++){
if(a[i].ff != a[i-1].ff) c[a[i].ff] = i, v.push_back(a[i].ff);
}
for(int i = 0; i <= n; i++) dis[i] = 1e9;
dis[ind] = 0;
for(int i = 0; i < m; i++){
if(a[i].ff == ind){
if(sz(v1) > 0 and a[i].ss == v1.back()) continue;
v1.push_back(a[i].ss);
}
}
while(sz(v) > 0){
int mn = 1e9, k = -1, b = 0, cnt = 0;
for(auto i : v1){
for(auto j : v){
cnt++;
if(dis[j] <= mn){
mn = dis[j];
k = j;
b = cnt-1;
}
if(abs(j-ind) % i != 0) continue;
dis[j] = min(dis[j],dis[ind]+abs(j-ind)/i);
if(dis[j] <= mn){
mn = dis[j];
k = j;
b = cnt-1;
}
}
}
v1.clear();
int x = c[k];
ind = k;
for(int i = x; i < m; i++){
if(a[i].ff == ind){
if(sz(v1) > 0 and a[i].ss == v1.back()) continue;
v1.push_back(a[i].ss);
}
else break;
}
v.erase(v.begin() + b);
if(ind == p) break;
}
if(dis[p] == 1e9) dis[p] = -1;
cout << dis[p];
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... |